From 0777ff7901d6a233c5efc450637a54c779313662 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sun, 31 Oct 2021 23:58:16 +0100 Subject: [PATCH] Add update option --- classes/ImageResizer.php | 20 ++++++++++--------- classes/ResizeJob.php | 2 +- compressors/JpgCompressor.php | 7 ++++++- compressors/PdfCompressor.php | 3 ++- compressors/PngCompressor.php | 3 ++- console/ResizeMake.php | 13 ++++++++---- tests/ResizeMakeTest.php | 37 +++++++++++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 17 deletions(-) diff --git a/classes/ImageResizer.php b/classes/ImageResizer.php index c5d461f..8197556 100644 --- a/classes/ImageResizer.php +++ b/classes/ImageResizer.php @@ -18,6 +18,7 @@ class ImageResizer private string $uploadDir; private MediaLibrary $media; private MediaPath $file; + private bool $update; public function __construct(FilesystemAdapter $disk, string $uploadDir, MediaLibrary $media) { @@ -26,9 +27,10 @@ class ImageResizer $this->media = $media; } - public function generate(MediaPath $file): void + public function generate(MediaPath $file, bool $update): void { $this->file = $file; + $this->update = $update; if (!$file->exists()) { throw new ResizerException('File versions cannot be generated. Root file "'.$file->root().'" doesnt exist.'); @@ -91,14 +93,14 @@ class ImageResizer private function generateVersions(): void { foreach ($this->possibleSizes() as $size) { - $this->file->compressor()->resize($size); - } - - foreach ($this->file->versions() as $version) { - if (!$this->disk->exists($version->get('path'))) { - throw new ResizerException('File versions cannot be generated. Version file "'.$version->get('path').'" of "'.$this->file->root().'" doesnt exist.'); - } - $this->file->compressor()->make($this->disk->path($version->get('path'))); + $this->file->compressor()->resize($size, $this->update, function($media, $file) { + if (!file_exists($file)) { + throw new ResizerException('File versions cannot be generated. Version file "'.$file.'" of "'.$this->file->root().'" doesnt exist.'); + } + if (file_exists($file) || !$this->update) { + $this->file->compressor()->make($file); + } + }); } } diff --git a/classes/ResizeJob.php b/classes/ResizeJob.php index 855e99a..38bfd09 100644 --- a/classes/ResizeJob.php +++ b/classes/ResizeJob.php @@ -13,7 +13,7 @@ class ResizeJob { try { list($file) = $params; - app(ImageResizer::class)->generate(new MediaPath($file)); + app(ImageResizer::class)->generate(new MediaPath($file), $params['update'] ?? false); } catch (Throwable $e) { Log::error('Resizing of image failed. Message: "'.$e->getMessage().'" in file "'.$e->getFile().'" on line '.$e->getLine()); throw $e; diff --git a/compressors/JpgCompressor.php b/compressors/JpgCompressor.php index ee44fc9..ba80b48 100644 --- a/compressors/JpgCompressor.php +++ b/compressors/JpgCompressor.php @@ -41,7 +41,7 @@ class JpgCompressor extends Compressor { return true; } - public function resize(Collection $size): void + public function resize(Collection $size, bool $update, callable $callback): void { $extension = $this->media->extension(); $temp = microtime().'.'.$extension; @@ -54,7 +54,12 @@ class JpgCompressor extends Compressor { $versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight); + if ($update && Storage::exists($versionFilename)) { + return; + } + $this->moveTo($temp, $versionFilename); + call_user_func($callback, $this->media, Storage::path($versionFilename)); } public function start(): void diff --git a/compressors/PdfCompressor.php b/compressors/PdfCompressor.php index 8afe14d..de004a8 100644 --- a/compressors/PdfCompressor.php +++ b/compressors/PdfCompressor.php @@ -76,7 +76,7 @@ class PdfCompressor extends Compressor return true; } - public function resize(Collection $size): void + public function resize(Collection $size, bool $update, callable $callback): void { $temp = $this->extractImage(); $tempBefore = PATHINFO($temp, PATHINFO_FILENAME).'compiled.'.pathinfo($temp, PATHINFO_EXTENSION); @@ -90,6 +90,7 @@ class PdfCompressor extends Compressor Storage::put($versionFilename, file_get_contents($tempBefore)); unlink($tempBefore); + call_user_func($callback, $this->media, Storage::path($versionFilename)); } } diff --git a/compressors/PngCompressor.php b/compressors/PngCompressor.php index 277acf3..e7cb673 100644 --- a/compressors/PngCompressor.php +++ b/compressors/PngCompressor.php @@ -51,7 +51,7 @@ class PngCompressor extends Compressor { // } - public function resize(Collection $size): void + public function resize(Collection $size, bool $update, callable $callback): void { $extension = $this->media->extension(); $temp = microtime().'.'.$extension; @@ -66,6 +66,7 @@ class PngCompressor extends Compressor { $versionFilename = $this->media->versionsDirPath().'/'.$this->versionFilename($destWidth, $destHeight); $this->moveTo($temp, $versionFilename); + call_user_func($callback, $this->media, Storage::path($versionFilename)); } } diff --git a/console/ResizeMake.php b/console/ResizeMake.php index 04c7bdd..fee1bc4 100644 --- a/console/ResizeMake.php +++ b/console/ResizeMake.php @@ -35,7 +35,7 @@ class ResizeMake extends Command if ($item->type === 'folder') { $this->resize($item->path); } else { - Queue::push(ResizeJob::class, [$item->path]); + Queue::push(ResizeJob::class, [$item->path, 'update' => $this->option('update', false)]); } } } @@ -51,12 +51,16 @@ class ResizeMake extends Command if ($this->option('folder')) { throw_unless(in_array($this->option('folder'), array_column(Setting::get('folders'), 'folder')), Exception::class, 'Folder not found'); - Storage::deleteDirectory('uploads/public/c'.$this->option('folder')); + if (!$this->option('update')) { + Storage::deleteDirectory('uploads/public/c'.$this->option('folder')); + } $this->resize($this->option('folder')); return; } - Storage::deleteDirectory('uploads/public/c'); + if (!$this->option('update')) { + Storage::deleteDirectory('uploads/public/c'); + } $cacheManager->flush(); foreach (Setting::get('folders') as $folder) { @@ -82,7 +86,8 @@ class ResizeMake extends Command protected function getOptions() { return [ - ['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null] + ['folder', 'f', InputOption::VALUE_OPTIONAL, 'Just resize for specific subfolders', null], + ['update', 'u', InputOption::VALUE_NONE, 'Just update missing files', null], ]; } } diff --git a/tests/ResizeMakeTest.php b/tests/ResizeMakeTest.php index 37a2ddb..6e12822 100644 --- a/tests/ResizeMakeTest.php +++ b/tests/ResizeMakeTest.php @@ -78,4 +78,41 @@ class ResizeMakeTest extends TestCase $this->assertFileCount(0, 'pages'); } + public function testOnlyResizeMissingBreakpointsIfUpdateGiven() + { + Setting::set('folders', [ + ['folder' => '/pages'], + ['folder' => 'otherdir'], + ]); + Setting::set('sizes', []); + Setting::set('breakpoints', [100, 200]); + $this->media->put('pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); + Storage::put('uploads/public/c/pages/test-200x200.jpg', UploadedFile::fake()->image('test.jpg', 200, 200)->get()); + $lastModified = Storage::lastModified('uploads/public/c/pages/test-200x200.jpg'); + sleep(1); + + Artisan::call('resize:make', ['-u' => true]); + + $this->assertFileCount(3, 'pages'); + $this->assertEquals($lastModified, Storage::lastModified('uploads/public/c/pages/test-200x200.jpg')); + } + + public function testOnlyResizeMissingVersionsIfUpdateGiven() + { + Setting::set('folders', [ + ['folder' => '/pages'], + ['folder' => 'otherdir'], + ]); + Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x2']]); + Setting::set('breakpoints', [100, 200]); + $this->media->put('pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); + Storage::put('uploads/public/c/pages/test-100x200.jpg', UploadedFile::fake()->image('test.jpg', 100, 200)->get()); + $lastModified = Storage::lastModified('uploads/public/c/pages/test-100x200.jpg'); + sleep(1); + + Artisan::call('resize:make', ['-u' => true]); + + $this->assertEquals($lastModified, Storage::lastModified('uploads/public/c/pages/test-100x200.jpg')); + } + }