Update photo import to laravel 6.4

This commit is contained in:
Deon George 2019-11-09 13:52:04 +11:00
parent f6b456aa3d
commit 83d6d1e055
18 changed files with 274 additions and 253 deletions

View File

@ -3,7 +3,6 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CatalogScan extends Command
{
@ -31,15 +30,14 @@ class CatalogScan extends Command
*/
public function handle()
{
$class = 'App\Model\\'.$this->argument('type');
$class = 'App\Models\\'.$this->argument('type');
if (! class_exists($class))
abort(500,sprintf('No class [%s]',$this->argument('type')));
$o = $class::findOrFail($this->argument('id'));
if (! is_readable($o->file_path()))
{
if (! is_readable($o->file_path())) {
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
exit;
}
@ -55,18 +53,14 @@ class CatalogScan extends Command
// If this is a duplicate
$x = $class::whereIN('id',$o->list_duplicate())->get();
if (count($x)) {
$break = FALSE;
foreach ($x as $oo) {
// And that photo is not marked as a duplicate
if (! $oo->duplicate)
{
if (! $oo->duplicate) {
$o->duplicate = '1';
$this->warn(sprintf('Image [%s] marked as a duplicate',$o->file_path()));
// If the file signature also matches, we'll mark it for deletion
if ($oo->file_signature AND $o->file_signature == $oo->file_signature)
{
if ($oo->file_signature AND $o->file_signature == $oo->file_signature) {
$this->warn(sprintf('Image [%s] marked for deletion',$o->file_path()));
$o->remove = '1';
}

View File

@ -2,19 +2,17 @@
namespace App\Console\Commands;
use Log;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Model\Person;
use App\Model\PhotoPerson;
use App\Model\Photo;
use App\Model\PhotoTag;
use App\Model\Tag;
use App\Models\{Person,Photo,Tag};
use App\Traits\Files;
class PhotoImport extends Command
{
use DispatchesJobs;
use \App\Traits\Files;
use Files;
/**
* The name and signature of the console command.
@ -53,7 +51,11 @@ class PhotoImport extends Command
*/
public function handle()
{
$files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')],'photo');
$files = $this->getFiles([
'dir'=>$this->option('dir'),
'file'=>$this->option('file')
],'photo');
if (! count($files))
exit;
@ -65,22 +67,21 @@ class PhotoImport extends Command
$t = $p = array();
// Tags
if ($this->option('tags'))
{
if ($this->option('tags')) {
$tags = explode(',',$this->option('tags'));
$t = Tag::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $t OR count($t) != count($tags))
{
if (! $t OR count($t) != count($tags)) {
$this->error(sprintf('Tag [%s] dont exist',join('|',$tags)));
abort(501);
}
}
// People
if ($this->option('people'))
{
if ($this->option('people')) {
$tags = explode(',',$this->option('people'));
$p = Person::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $p OR count($p) != count($tags))
{
$this->error(sprintf('People [%s] dont exist',join('|',$tags)));
@ -89,18 +90,15 @@ class PhotoImport extends Command
}
$c = 0;
foreach ($files as $file)
{
foreach ($files as $file) {
$bar->advance();
if (preg_match('/@__thumb/',$file) OR preg_match('/\/._/',$file))
{
if (preg_match('/@__thumb/',$file) OR preg_match('/\/._/',$file)) {
$this->warn(sprintf('Ignoring file [%s]',$file));
continue;
}
if (! in_array(strtolower(pathinfo($file,PATHINFO_EXTENSION)),config('photo.import.accepted')))
{
if (! in_array(strtolower(pathinfo($file,PATHINFO_EXTENSION)),config('photo.import.accepted'))) {
$this->warn(sprintf('Ignoring [%s]',$file));
continue;
}
@ -112,21 +110,21 @@ class PhotoImport extends Command
$o = Photo::where('filename',$file)->first();
if (is_null($o))
{
// The photo doesnt exist
if (is_null($o)) {
$o = new Photo;
$o->filename = $file;
}
if (! is_readable($o->file_path()))
{
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
// Make sure we can read the photo.
if (! is_readable($o->file_path())) {
$this->warn(sprintf('Ignoring [%s], it is not readable',$o->file_path()));
continue;
}
if ($o->exists)
$this->warn(sprintf('%s [%s] already in DB: %s',$o->objectType(),$file,$o->id));
$o->save();
if ($o->wasRecentlyCreated)

View File

@ -2,14 +2,15 @@
namespace App\Console\Commands;
use DB;
use Log;
use Illuminate\Support\Facades\Log;
use Illuminate\Console\Command;
use App\Model\Photo;
use App\Traits\Files;
use App\Models\Photo;
class PhotoMove extends Command
{
use \App\Traits\Files;
use Files;
/**
* The name and signature of the console command.
@ -44,16 +45,13 @@ class PhotoMove extends Command
*/
public function handle()
{
if ($this->option('file'))
{
if ($this->option('file')) {
$po = Photo::notRemove()->notDuplicate()->where('filename',Photo::path($this->option('file')));
}
elseif ($this->option('id'))
{
} elseif ($this->option('id')) {
$po = Photo::notRemove()->notDuplicate()->where('id',$this->option('id'));
}
else
{
} else {
$po = Photo::notRemove()->notDuplicate();
}
@ -66,13 +64,11 @@ class PhotoMove extends Command
$bar->setRedrawFrequency(100);
$po->chunk(1,function($photo) use ($bar) {
while ($o = $photo->shift())
{
while ($o = $photo->shift()) {
if (($x = $o->moveable()) === TRUE) {
Log::info(sprintf('%s: Moving (%s)[%s]',__METHOD__,$o->id,$o->filename));
if ($this->makeParentDir(dirname($o->file_path(FALSE,TRUE))) AND rename($o->file_path(),$o->file_path(FALSE,TRUE)))
{
if ($this->makeParentDir(dirname($o->file_path(FALSE,TRUE))) AND rename($o->file_path(),$o->file_path(FALSE,TRUE))) {
// Convert the path to a relative one.
$o->filename = $o->file_path(TRUE,TRUE);
@ -82,17 +78,15 @@ class PhotoMove extends Command
$this->error(sprintf('Save after rename failed for (%s)',$o->id));
continue;
}
}
else
{
} else {
$this->error(sprintf('Rename failed for (%s)',$o->id));
continue;
}
chmod($o->file_path(),0444);
}
else
{
} else {
if ($x > 0)
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], moveable returned (%s)',$o->id,$o->file_path(),$o->file_path(FALSE,TRUE),$x));
}

View File

@ -2,11 +2,9 @@
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Model\Photo;
use App\Models\Photo;
use App\Jobs\PhotoDelete;
class PhotoController extends Controller
@ -54,7 +52,10 @@ class PhotoController extends Controller
public function duplicates($id=NULL)
{
return view('catalog.duplicatereview',['return'=>url('/p/duplicates'),'catalog'=>is_null($id) ? Photo::notRemove()->where('duplicate',1)->paginate(50) : Photo::where('id',$id)->paginate(1)]);
return view('catalog.duplicatereview',[
'return'=>url('/p/duplicates'),
'catalog'=>is_null($id) ? Photo::notRemove()->where('duplicate',1)->paginate(50) : Photo::where('id',$id)->paginate(1)
]);
}
public function duplicatesUpdate(Request $request)

View File

@ -2,12 +2,13 @@
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Artisan;
use App\Models\Abstracted\Catalog;
class CatalogScan extends Job implements ShouldQueue
{
@ -21,8 +22,7 @@ class CatalogScan extends Job implements ShouldQueue
*
* @return void
*/
public function __construct(\App\Model\Abstracted\Catalog $o)
{
public function __construct(Catalog $o) {
$this->o = $o;
}
@ -34,6 +34,7 @@ class CatalogScan extends Job implements ShouldQueue
public function handle()
{
Log::info(sprintf('%s: Scanning [%s|%s]',__METHOD__,$this->o->objecttype(),$this->o->id));
Artisan::call('catalog:scan',['type'=>$this->o->objecttype(),'id'=>$this->o->id]);
}
}

View File

@ -2,12 +2,12 @@
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Model\Photo;
use App\Models\Photo;
class PhotoDelete extends Job implements ShouldQueue
{

View File

@ -2,13 +2,13 @@
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Model\Photo;
use Artisan;
use Illuminate\Support\Facades\Artisan;
use App\Models\Photo;
class PhotoMove extends Job implements ShouldQueue
{

View File

@ -2,12 +2,12 @@
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Model\Video;
use App\Models\Video;
class VideoDelete extends Job implements ShouldQueue
{

View File

@ -2,13 +2,13 @@
namespace App\Jobs;
use Log;
use App\Jobs\Job;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Model\Video;
use Artisan;
use Illuminate\Support\Facades\Artisan;
use App\Models\Video;
class VideoMove extends Job implements ShouldQueue
{

View File

@ -282,7 +282,7 @@ abstract class Catalog extends Model
{
return DB::table($this->getTable())
->where('id','<',$this->id)
->orderby('id','DEC')
->orderby('id','DESC')
->first();
}

View File

@ -2,6 +2,7 @@
namespace App\Providers;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Bus\DispatchesJobs;
@ -31,18 +32,18 @@ class AppServiceProvider extends ServiceProvider
{
// Any photo saved, queue it to be moved.
Photo::saved(function($photo) {
if ($photo->scanned AND ! $photo->duplicate AND ! $photo->remove AND ($x=$photo->moveable()) === TRUE)
{
if ($photo->scanned AND ! $photo->duplicate AND ! $photo->remove AND ($x=$photo->moveable()) === TRUE) {
Log::info(sprintf('%s: Need to Move [%s]',__METHOD__,$photo->id.'|'.serialize($x)));
$this->dispatch((new PhotoMove($photo))->onQueue('move'));
}
});
// Any video saved, queue it to be moved.
Video::saved(function($video) {
if ($video->scanned AND ! $video->duplicate AND ! $video->remove AND ($x=$video->moveable()) === TRUE)
{
if ($video->scanned AND ! $video->duplicate AND ! $video->remove AND ($x=$video->moveable()) === TRUE) {
Log::info(sprintf('%s: Need to Move [%s]',__METHOD__,$video->id.'|'.serialize($x)));
$this->dispatch((new VideoMove($video))->onQueue('move'));
}
});

View File

@ -2,15 +2,15 @@
namespace App\Traits;
use Log;
use App\Model\Photo;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
trait Files
{
/**
* Get a list of files
*/
public function getFiles(array $option,$type)
public function getFiles(array $option,string $type): Collection
{
// Make sure we got a directory or a file to import
if (is_null($option['file']) AND is_null($option['dir']))
@ -18,38 +18,42 @@ trait Files
Log::info(sprintf('%s: Processing: %s',__METHOD__,($option['file'] ? $option['file'] : $option['dir'])));
$files = [];
$files = collect();
$dir = '';
if ($option['dir'])
{
if ($option['dir'] AND is_dir($option['dir'])) {
// Remove our trailing slash from the directory.
$dir = preg_replace('/\/$/','',$option['dir']);
// Exclude . & .. from the path.
$files = array_diff(scandir($dir),array('.','..'));
$files = $files->merge(array_diff(scandir($dir),array('.','..')));
}
elseif ($option['file'] AND file_exists($option['file']))
{
} elseif ($option['file'] AND ! is_dir($option['file']) AND file_exists($option['file'])) {
$dir = dirname($option['file']);
$files = array(basename($option['file']));
$files->push(basename($option['file']));
}
// Determine if our dir is releative to where we store data
// Determine if our dir is relative to where we store data
$dir = static::path($dir,$type);
// Add our path
if ($dir)
array_walk($files,function(&$value,$key,$path='') {
if ($path) {
$value = sprintf('%s/%s',$path,$value);
}
},$dir);
$files->transform(function($value) use ($dir) {
return sprintf('%s/%s',$dir,$value);
});
Log::info(sprintf('%s: Processing: %s (%s)',__METHOD__,($option['file'] ? $option['file'] : $option['dir']),count($files)));
return $files;
}
/**
* Recursively make a parent dir to hold files.
*
* @param $dir
* @return bool
*/
public function makeParentDir($dir)
{
if (is_dir($dir))
@ -61,7 +65,14 @@ trait Files
return is_writable(dirname($dir)) AND mkdir($dir);
}
public static function path($path,$type)
/**
* Return if the dir is a sub dir of our config.
*
* @param $path
* @param $type
* @return string
*/
public static function path($path,$type): string
{
return (strpos($path,config($type.'.dir').'/') === 0) ? str_replace(config($type.'.dir').'/','',$path) : $path;
}

View File

@ -1,7 +1,8 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
@ -13,9 +14,10 @@ class CreateUsersTable extends Migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
@ -29,6 +31,6 @@ class CreateUsersTable extends Migration
*/
public function down()
{
Schema::drop('users');
Schema::dropIfExists('users');
}
}

View File

@ -1,7 +1,8 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePasswordResetsTable extends Migration
{
@ -14,8 +15,8 @@ class CreatePasswordResetsTable extends Migration
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
@ -26,6 +27,6 @@ class CreatePasswordResetsTable extends Migration
*/
public function down()
{
Schema::drop('password_resets');
Schema::dropIfExists('password_resets');
}
}

View File

@ -1,4 +1,4 @@
@extends('layouts.app')
@extends('adminlte::layouts.app')
<?php $data = [
'ID'=>['id','idlink'],
@ -13,7 +13,7 @@
'Delete'=>['remove','removecheckbox'],
];?>
@section('content')
@section('main-content')
<div class="container">
<div class="row">
<div class="col-md-11 col-md-offset-1">

View File

@ -1,6 +1,6 @@
@extends('layouts.app')
@extends('adminlte::layouts.app')
@section('content')
@section('main-content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">

View File

@ -1 +0,0 @@

View File

@ -11,6 +11,25 @@
|
*/
Route::get('/', function () {
return view('welcome');
});
#Route::get('/', function () {
# return view('welcome');
#});
Route::get('/p/deletes/{id?}','PhotoController@deletes')->where('id','[0-9]+');
Route::get('/v/deletes/{id?}','VideoController@deletes')->where('id','[0-9]+');
Route::get('/p/duplicates/{id?}','PhotoController@duplicates')->where('id','[0-9]+');
Route::get('/v/duplicates/{id?}','VideoController@duplicates')->where('id','[0-9]+');
Route::get('/p/info/{id}','PhotoController@info')->where('id','[0-9]+');
Route::get('/v/info/{id}','VideoController@info')->where('id','[0-9]+');
Route::get('/p/thumbnail/{id}','PhotoController@thumbnail')->where('id','[0-9]+');
Route::get('/p/view/{id}','PhotoController@view')->where('id','[0-9]+');
Route::get('/v/view/{id}','VideoController@view')->where('id','[0-9]+');
Route::post('/p/delete/{id}','PhotoController@delete')->where('id','[0-9]+');
Route::post('/v/delete/{id}','VideoController@delete')->where('id','[0-9]+');
Route::post('/p/duplicates','PhotoController@duplicatesUpdate');
Route::post('/v/duplicates','VideoController@duplicatesUpdate');
Route::post('/p/deletes','PhotoController@deletesUpdate');
Route::post('/v/deletes','VideoController@deletesUpdate');
Route::post('/p/undelete/{id}','PhotoController@undelete')->where('id','[0-9]+');
Route::post('/v/undelete/{id}','VideoController@undelete')->where('id','[0-9]+');