photo/app/Console/Commands/Move.php

105 lines
2.3 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;
class Move extends Command
{
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'))
{
$po = Photo::notRemove()->notDuplicate()->where('id',Photo::path($this->option('id')));
}
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) {
while ($po = $photo->shift())
{
if (($x = $po->moveable()) === TRUE) {
Log::info(sprintf('%s: Moving (%s)[%s]',__METHOD__,$po->id,$po->filename));
2016-06-29 04:04:02 +00:00
if ($this->makeParentDir(dirname($po->file_path(FALSE,TRUE))) AND rename($po->file_path(),$po->file_path(FALSE,TRUE)))
2016-06-29 04:04:02 +00:00
{
// Convert the path to a relative one.
$po->filename = $po->file_path(TRUE,TRUE);
// @todo If the DB update failed, move it back.
if (! $po->save()) # AND ! rename($path,$po->file_path()))
{
$this->error(sprintf('Save after rename failed for (%s)',$po->id));
continue;
}
}
else
{
$this->error(sprintf('Rename failed for (%s)',$po->id));
continue;
}
chmod($po->file_path(),0444);
}
else
{
if ($x > 0)
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], moveable returned (%s)',$po->id,$po->file_path(),$po->file_path(FALSE,TRUE),$x));
}
}
$bar->advance();
});
}
}