photo/app/Jobs/CatalogVerify.php

100 lines
2.4 KiB
PHP
Raw Normal View History

2020-07-16 01:41:17 +00:00
<?php
namespace App\Jobs;
2020-07-16 01:49:20 +00:00
use Illuminate\Bus\Queueable;
2020-07-16 01:41:17 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
2020-07-16 01:49:20 +00:00
use Illuminate\Foundation\Bus\Dispatchable;
2020-07-16 01:41:17 +00:00
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
2020-07-16 03:40:16 +00:00
use App\Traits\Files;
2020-07-16 01:41:17 +00:00
use App\Traits\Type;
class CatalogVerify extends Job implements ShouldQueue
{
2020-07-16 03:40:16 +00:00
use Dispatchable, Queueable, InteractsWithQueue, SerializesModels, Type, Files;
2020-07-16 01:41:17 +00:00
// What we should verify
private $type = NULL;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(string $type) {
$this->type = $type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info(sprintf('%s: Scanning [%s]',__METHOD__,$this->type));
// Go through DB and verify files exist
$class = $this->getModelType($this->type);
$good = $bad = $ugly = 0;
2020-07-16 01:49:20 +00:00
$class::select('*')->each(function($o) use ($good,$bad,$ugly) {
2020-07-16 02:12:26 +00:00
if (! file_exists($o->file_name_current(FALSE))) {
Log::error(sprintf('Media doesnt exist: [%s] (%d:%s)',$this->type,$o->id,$o->file_name_current(FALSE)));
2020-07-16 01:41:17 +00:00
$bad++;
return;
}
2020-07-16 02:12:26 +00:00
if (($x=md5_file($o->file_name_current(FALSE))) !== $o->file_signature) {
Log::error(sprintf('Media MD5 doesnt match DB: [%s] (%d:%s) [%s:%s]',$this->type,$o->id,$o->file_name_current(FALSE),$x,$o->file_signature));
2020-07-16 01:41:17 +00:00
$ugly++;
return;
}
$good++;
});
2020-07-16 03:40:16 +00:00
Log::info(sprintf('DB Media Verify Complete: [%s] Good: [%d], Missing: [%d], Changed: [%d]',$this->type,$good,$bad,$ugly));
2020-07-16 01:41:17 +00:00
// Go through filesystem and see that a record exists in the DB, if not add it.
2020-07-16 03:40:16 +00:00
$parentdir = config($this->type.'.dir');
$good = $bad = 0;
foreach ($this->dirlist($parentdir) as $dir) {
foreach ($this->getFiles(['dir'=>$dir,'file'=>NULL],$this->type) as $file) {
if (! $class::where('filename',$file)->count()) {
$bad++;
Log::error(sprintf('File not in DB: [%s] (%s)',$this->type,$file));
} else {
$good++;
}
}
}
Log::info(sprintf('File Media Verify Complete: [%s] Good: [%d], Not In DB: [%d]',$this->type,$good,$bad));
}
/**
* Recursively get a list of dirs
* @param $path
* @return \Illuminate\Support\Collection
*/
private function dirlist($path)
{
$list = collect();
$list->push($path);
foreach (glob($path.'/*',GLOB_ONLYDIR) as $dir) {
foreach ($this->dirlist($dir) as $subdir)
$list->push($subdir);
}
return $list;
2020-07-16 01:41:17 +00:00
}
}