oc-resizer-plugin/console/ResizeMake.php

94 lines
2.6 KiB
PHP
Raw Normal View History

2019-09-17 23:14:29 +02:00
<?php namespace Aweos\Resizer\Console;
use Aweos\Resizer\Classes\CacheManager;
2021-09-06 02:31:27 +02:00
use Aweos\Resizer\Classes\ResizeJob;
2019-09-17 23:14:29 +02:00
use Aweos\Resizer\Models\Setting;
2021-09-19 12:38:57 +02:00
use Exception;
2021-09-06 02:31:27 +02:00
use GuzzleHttp\Client;
2019-09-17 23:14:29 +02:00
use GuzzleHttp\Exception\ClientException;
2021-09-06 02:31:27 +02:00
use Illuminate\Console\Command;
2021-12-17 01:29:15 +01:00
use MediaLibrary;
2019-09-17 23:14:29 +02:00
use October\Rain\Database\Attach\Resizer;
2021-09-06 02:31:27 +02:00
use Queue;
use Storage;
2019-09-17 23:14:29 +02:00
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
2021-09-06 02:31:27 +02:00
use Symfony\Component\Console\Input\InputOption;
2019-09-17 23:14:29 +02:00
class ResizeMake extends Command
{
2022-02-15 18:06:51 +01:00
public MediaLibrary $media;
2019-09-17 23:14:29 +02:00
/**
* @var string The console command name.
*/
protected $name = 'resize:make';
/**
* @var string The console command description.
*/
protected $description = 'Resizes configured images';
2021-09-06 02:31:27 +02:00
public function resize(string $folder): void
{
foreach ($this->media->listFolderContents($folder) as $item) {
if ($item->type === 'folder') {
$this->resize($item->path);
} else {
2021-11-01 12:52:45 +01:00
Queue::push(ResizeJob::class, [$item->path, 'update' => $this->option('update', false)], Setting::get('queue'));
2021-09-06 02:31:27 +02:00
}
2019-09-17 23:14:29 +02:00
}
}
/**
* Execute the console command.
* @return void
*/
public function handle(CacheManager $cacheManager)
2019-09-17 23:14:29 +02:00
{
$this->media = MediaLibrary::instance();
ProgressBar::setFormatDefinition('custom', '%current%/%max% %bar% -- %message% (%size%)');
2021-09-19 12:38:57 +02:00
if ($this->option('folder')) {
throw_unless(in_array($this->option('folder'), array_column(Setting::get('folders'), 'folder')), Exception::class, 'Folder not found');
2021-10-31 23:58:16 +01:00
if (!$this->option('update')) {
Storage::deleteDirectory('uploads/public/c'.$this->option('folder'));
}
2021-09-19 12:38:57 +02:00
$this->resize($this->option('folder'));
return;
}
2021-10-31 23:58:16 +01:00
if (!$this->option('update')) {
Storage::deleteDirectory('uploads/public/c');
}
$cacheManager->flush();
2019-09-17 23:14:29 +02:00
2021-09-06 02:31:27 +02:00
foreach (Setting::get('folders') as $folder) {
$this->resize($folder['folder']);
2019-09-17 23:14:29 +02:00
}
}
/**
* Get the console command arguments.
* @return array
*/
protected function getArguments()
{
2021-09-19 12:38:57 +02:00
return [
];
2019-09-17 23:14:29 +02:00
}
/**
* Get the console command options.
* @return array
*/
protected function getOptions()
{
2021-09-19 12:38:57 +02:00
return [
2021-10-31 23:58:16 +01:00
['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null],
['update', 'u', InputOption::VALUE_NONE, 'Just update missing files', null],
2021-09-19 12:38:57 +02:00
];
2019-09-17 23:14:29 +02:00
}
}