oc-resizer-plugin/console/ResizeMake.php

88 lines
2.3 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;
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;
use System\Classes\MediaLibrary;
2019-09-17 23:14:29 +02:00
class ResizeMake extends Command
{
public $media = null;
/**
* @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 {
Queue::push(ResizeJob::class, [$item->path]);
}
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');
$this->resize($this->option('folder'));
return;
}
2021-09-06 02:31:27 +02:00
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 [
['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null]
];
2019-09-17 23:14:29 +02:00
}
}