From 8013f66b4219500098bce98c22b78e701e41221e Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 19 Sep 2021 12:38:57 +0200 Subject: [PATCH] Add option to resize a single folder --- console/ResizeMake.php | 16 ++++++++-- tests/DeleteTest.php | 2 +- tests/ImageTagTest.php | 2 +- tests/MoveTest.php | 2 +- tests/ResizeMakeTest.php | 66 ++++++++++++++++++++++++++++++++++++++++ tests/ResizerTest.php | 2 +- 6 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 tests/ResizeMakeTest.php diff --git a/console/ResizeMake.php b/console/ResizeMake.php index 5edcf0b..a3f82cc 100644 --- a/console/ResizeMake.php +++ b/console/ResizeMake.php @@ -3,6 +3,7 @@ use Aweos\Resizer\Classes\CacheManager; use Aweos\Resizer\Classes\ResizeJob; use Aweos\Resizer\Models\Setting; +use Exception; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; use Illuminate\Console\Command; @@ -47,6 +48,13 @@ class ResizeMake extends Command { $this->media = MediaLibrary::instance(); ProgressBar::setFormatDefinition('custom', '%current%/%max% %bar% -- %message% (%size%)'); + + 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; + } + Storage::deleteDirectory('uploads/public/c'); $cacheManager->flush(); @@ -61,7 +69,9 @@ class ResizeMake extends Command */ protected function getArguments() { - return []; + return [ + + ]; } /** @@ -70,6 +80,8 @@ class ResizeMake extends Command */ protected function getOptions() { - return []; + return [ + ['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null] + ]; } } diff --git a/tests/DeleteTest.php b/tests/DeleteTest.php index ac88145..607c332 100644 --- a/tests/DeleteTest.php +++ b/tests/DeleteTest.php @@ -1,6 +1,6 @@ 'pages']]); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + $this->media->put('pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + + Artisan::call('resize:make'); + + $this->assertFileCount(1, 'pages'); + } + + public function testItDeletesOtherFilesBeforeResizingAll() + { + Setting::set('folders', [['folder' => 'pages']]); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + Storage::put('uploads/public/c/otherdir/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + + Artisan::call('resize:make'); + + $this->assertFileCount(0, 'otherdir'); + } + + public function testOnlyResizeASingleFolder() + { + Setting::set('folders', [ + ['folder' => 'pages'], + ['folder' => 'otherdir'], + ]); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + $this->media->put('pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + Storage::put('uploads/public/c/pages/test-100x100.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + $this->media->put('otherdir/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + Storage::put('uploads/public/c/otherdir/test-100x100.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + Storage::put('uploads/public/c/otherdir/test-200x200.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + + Artisan::call('resize:make', ['-f' => 'pages']); + + $this->assertFileCount(2, 'otherdir'); + $this->assertFileCount(1, 'pages'); + } + +} diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index 5d53c37..214f6a8 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -1,6 +1,6 @@