photo/app/Console/Commands/PhotoMove.php

105 lines
2.2 KiB
PHP
Raw Normal View History

2016-06-29 04:04:02 +00:00
<?php
namespace App\Console\Commands;
use DB;
use Log;
use Illuminate\Console\Command;
use App\Model\Photo;
2016-07-04 06:00:33 +00:00
class PhotoMove extends Command
2016-06-29 04:04:02 +00:00
{
use \App\Traits\Files;
2016-06-29 04:04:02 +00:00
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'photo:move
{--file= : Photo File}
{--id= : Photo ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Moves Photos to their new location';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if ($this->option('file'))
{
$po = Photo::notRemove()->notDuplicate()->where('filename',Photo::path($this->option('file')));
}
elseif ($this->option('id'))
{
2016-07-01 05:59:22 +00:00
$po = Photo::notRemove()->notDuplicate()->where('id',$this->option('id'));
2016-06-29 04:04:02 +00:00
}
else
{
$po = Photo::notRemove()->notDuplicate();
}
if (! $po)
exit;
2016-06-29 04:04:02 +00:00
// Show a progress bar
$bar = $this->output->createProgressBar($po->count());
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$bar->setRedrawFrequency(100);
$po->chunk(1,function($photo) use ($bar) {
2016-07-04 06:00:33 +00:00
while ($o = $photo->shift())
2016-06-29 04:04:02 +00:00
{
2016-07-04 06:00:33 +00:00
if (($x = $o->moveable()) === TRUE) {
Log::info(sprintf('%s: Moving (%s)[%s]',__METHOD__,$o->id,$o->filename));
2016-06-29 04:04:02 +00:00
2016-07-04 06:00:33 +00:00
if ($this->makeParentDir(dirname($o->file_path(FALSE,TRUE))) AND rename($o->file_path(),$o->file_path(FALSE,TRUE)))
2016-06-29 04:04:02 +00:00
{
// Convert the path to a relative one.
2016-07-04 06:00:33 +00:00
$o->filename = $o->file_path(TRUE,TRUE);
2016-06-29 04:04:02 +00:00
// @todo If the DB update failed, move it back.
2016-07-04 06:00:33 +00:00
if (! $o->save()) # AND ! rename($path,$o->file_path()))
2016-06-29 04:04:02 +00:00
{
2016-07-04 06:00:33 +00:00
$this->error(sprintf('Save after rename failed for (%s)',$o->id));
2016-06-29 04:04:02 +00:00
continue;
}
}
else
{
2016-07-04 06:00:33 +00:00
$this->error(sprintf('Rename failed for (%s)',$o->id));
2016-06-29 04:04:02 +00:00
continue;
}
2016-07-04 06:00:33 +00:00
chmod($o->file_path(),0444);
2016-06-29 04:04:02 +00:00
}
else
{
if ($x > 0)
2016-07-04 06:00:33 +00:00
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], moveable returned (%s)',$o->id,$o->file_path(),$o->file_path(FALSE,TRUE),$x));
2016-06-29 04:04:02 +00:00
}
}
$bar->advance();
});
}
}