Finished work on Catalog::verify

This commit is contained in:
Deon George 2020-07-16 13:40:16 +10:00
parent 18812b51e0
commit 3d5c0cfe9f
No known key found for this signature in database
GPG Key ID: 7670E8DC27415254
2 changed files with 42 additions and 5 deletions

View File

@ -9,11 +9,12 @@ use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use App\Traits\Files;
use App\Traits\Type; use App\Traits\Type;
class CatalogVerify extends Job implements ShouldQueue class CatalogVerify extends Job implements ShouldQueue
{ {
use Dispatchable, Queueable, InteractsWithQueue, SerializesModels, Type; use Dispatchable, Queueable, InteractsWithQueue, SerializesModels, Type, Files;
// What we should verify // What we should verify
private $type = NULL; private $type = NULL;
@ -57,8 +58,44 @@ class CatalogVerify extends Job implements ShouldQueue
$good++; $good++;
}); });
Log::info('DB Media Verify Complete: [%s] Good: [%d], Missing: [%d], Changed: [%d]',$this->type,$good,$bad,$ugly); Log::info(sprintf('DB Media Verify Complete: [%s] Good: [%d], Missing: [%d], Changed: [%d]',$this->type,$good,$bad,$ugly));
// Go through filesystem and see that a record exists in the DB, if not add it. // Go through filesystem and see that a record exists in the DB, if not add it.
$parentdir = config($this->type.'.dir');
$parentdir = 'app';
$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;
} }
} }

View File

@ -16,7 +16,7 @@ trait Files
if (is_null($option['file']) AND is_null($option['dir'])) if (is_null($option['file']) AND is_null($option['dir']))
abort(500,'Missing filename, please use --file= OR --dir='); abort(500,'Missing filename, please use --file= OR --dir=');
Log::info(sprintf('%s: Processing: %s',__METHOD__,($option['file'] ? $option['file'] : $option['dir']))); Log::debug(sprintf('%s: Processing: %s',__METHOD__,($option['file'] ? $option['file'] : $option['dir'])));
$files = collect(); $files = collect();
$dir = ''; $dir = '';
@ -26,7 +26,7 @@ trait Files
$dir = preg_replace('/\/$/','',$option['dir']); $dir = preg_replace('/\/$/','',$option['dir']);
// Exclude . & .. from the path. // Exclude . & .. from the path.
$files = $files->merge(array_diff(scandir($dir),array('.','..'))); $files = $files->merge(array_filter(array_diff(scandir($dir),array('.','..')),function($item) use ($dir) { return is_file($dir.'/'.$item); }));
} elseif ($option['file'] AND ! is_dir($option['file']) AND file_exists($option['file'])) { } elseif ($option['file'] AND ! is_dir($option['file']) AND file_exists($option['file'])) {
$dir = dirname($option['file']); $dir = dirname($option['file']);
@ -43,7 +43,7 @@ trait Files
return sprintf('%s/%s',$dir,$value); return sprintf('%s/%s',$dir,$value);
}); });
Log::info(sprintf('%s: Processing: %s (%s)',__METHOD__,($option['file'] ? $option['file'] : $option['dir']),count($files))); Log::debug(sprintf('%s: Processing: %s (%s)',__METHOD__,($option['file'] ? $option['file'] : $option['dir']),count($files)));
return $files; return $files;
} }