photo/app/Console/Commands/VideoMove.php

102 lines
2.4 KiB
PHP
Raw Normal View History

2016-07-04 06:00:33 +00:00
<?php
namespace App\Console\Commands;
2019-12-22 11:16:31 +00:00
use Illuminate\Support\Facades\Log;
2016-07-04 06:00:33 +00:00
use Illuminate\Console\Command;
2019-12-22 11:16:31 +00:00
use App\Traits\Files;
use App\Models\Video;
2016-07-04 06:00:33 +00:00
class VideoMove extends Command
{
2019-12-22 11:16:31 +00:00
use Files;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'video:move
2016-07-04 06:00:33 +00:00
{--file= : Video File}
{--id= : Video ID}';
2019-12-22 11:16:31 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Moves Videos to their new location';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
2016-07-04 06:00:33 +00:00
{
2019-12-22 11:16:31 +00:00
parent::__construct();
2016-07-04 06:00:33 +00:00
}
2019-12-22 11:16:31 +00:00
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
2016-07-04 06:00:33 +00:00
{
2019-12-22 11:16:31 +00:00
if ($this->option('file')) {
$vo = Video::notRemove()->notDuplicate()->where('filename',Video::path($this->option('file')));
2016-07-04 06:00:33 +00:00
2019-12-22 11:16:31 +00:00
} elseif ($this->option('id')) {
$vo = Video::notRemove()->notDuplicate()->where('id',$this->option('id'));
} else {
$vo = Video::notRemove()->notDuplicate();
}
if (! $vo)
exit;
2016-07-04 06:00:33 +00:00
2019-12-22 11:16:31 +00:00
// Show a progress bar
$bar = $this->output->createProgressBar($vo->count());
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$bar->setRedrawFrequency(100);
2016-07-04 06:00:33 +00:00
2019-12-22 11:16:31 +00:00
$vo->chunk(1,function($video) use ($bar) {
while ($o = $video->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))) {
// Convert the path to a relative one.
$o->filename = $o->file_path(TRUE,TRUE);
// @todo If the DB update failed, move it back.
if (! $o->save()) # AND ! rename($path,$o->file_path()))
{
$this->error(sprintf('Save after rename failed for (%s)',$o->id));
continue;
}
} else {
$this->error(sprintf('Rename failed for (%s)',$o->id));
2016-07-04 06:00:33 +00:00
continue;
}
2019-12-22 11:16:31 +00:00
chmod($o->file_path(),0444);
} else {
if ($x > 0) {
$this->warn(sprintf('Unable to move (%s) [%s] to [%s], movable returned (%s)',$o->id,$o->file_path(),$o->file_path(FALSE,TRUE),$x));
if ($x == 1 AND $v = Video::where('filename',$o->file_path(TRUE,TRUE))->first())
$this->warn(sprintf('File is id (%s) [%s]',$v->file_path(),$v->id));
}
2019-11-08 10:43:36 +00:00
}
2016-07-04 06:00:33 +00:00
}
2019-12-22 11:16:31 +00:00
$bar->advance();
});
}
}