From 30bd3dc25f64a2628bc8610b41d08831537c3847 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Wed, 16 Feb 2022 02:49:19 +0100 Subject: [PATCH] Fixed: Delete images --- .ackrc | 1 + Plugin.php | 20 ++++++++------ classes/CacheManager.php | 3 +- classes/ResizeJob.php | 4 ++- composer.json | 4 ++- jobs/DeleteJob.php | 36 ++++++++++++++++++++++++ jobs/MoveJob.php | 38 ++++++++++++++++++++++++++ jobs/ResizeJob.php | 35 ++++++++++++++++++++++++ lib/MediaPath.php | 38 +++++++++----------------- lib/StorageMediaPath.php | 36 ++++++++++++++++++++++++ lib/TempMediaPath.php | 59 ++++++++++++++++++++++++++++++++++++++++ tests/ImageTagTest.php | 23 ++++------------ tests/MoveTest.php | 10 +++---- 13 files changed, 249 insertions(+), 58 deletions(-) create mode 100644 .ackrc create mode 100644 jobs/DeleteJob.php create mode 100644 jobs/MoveJob.php create mode 100644 jobs/ResizeJob.php create mode 100644 lib/StorageMediaPath.php create mode 100644 lib/TempMediaPath.php diff --git a/.ackrc b/.ackrc new file mode 100644 index 0000000..d2c1607 --- /dev/null +++ b/.ackrc @@ -0,0 +1 @@ +--ignore-dir=/vendor diff --git a/Plugin.php b/Plugin.php index 9a79b8b..c0adcae 100644 --- a/Plugin.php +++ b/Plugin.php @@ -8,7 +8,10 @@ use Aweos\Resizer\Classes\ImageResizer; use Aweos\Resizer\Classes\ResizeJob; use Aweos\Resizer\Console\ResizeMake; use Aweos\Resizer\Console\ResizePurge; +use Aweos\Resizer\Jobs\DeleteJob; +use Aweos\Resizer\Jobs\MoveJob; use Aweos\Resizer\Lib\MediaPath; +use Aweos\Resizer\Lib\StorageMediaPath; use Aweos\Resizer\Models\Setting; use Event; use MediaLibrary; @@ -65,22 +68,23 @@ class Plugin extends PluginBase app()->bind('resize', fn () => app(CacheManager::class)); Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) { - if ((new MediaPath($filePath))->shouldProcess()) { + if ((new StorageMediaPath($filePath))->shouldProcess()) { Queue::push(ResizeJob::class, [$filePath], Setting::get('queue')); } }); Event::listen('media.file.delete', function($widget, $filePath) { - app(FileObserver::class)->delete(new MediaPath($filePath)); - app(CacheManager::class)->delete(new MediaPath($filePath)); + Queue::push(DeleteJob::class, [$filePath], Setting::get('queue')); }); Event::listen('media.file.move', function($widget, $old, $new) { - app(FileObserver::class)->rename(new MediaPath($old), new MediaPath($new)); - app(CacheManager::class)->delete(new MediaPath($old)); + if ((new StorageMediaPath($new))->shouldProcess() || (new StorageMediaPath($old))->shouldProcess()) { + Queue::push(MoveJob::class, [$old, $new], Setting::get('queue')); + } }); Event::listen('media.file.rename', function($widget, $old, $new) { - app(FileObserver::class)->rename(new MediaPath($old), new MediaPath($new)); - app(CacheManager::class)->delete(new MediaPath($old)); + if ((new StorageMediaPath($old))->shouldProcess()) { + Queue::push(MoveJob::class, [$old, $new], Setting::get('queue')); + } }); } @@ -103,7 +107,7 @@ class Plugin extends PluginBase return [ 'filters' => [ 'resize' => fn ($media, $size = 'original', $sizes = null, $options = []) => app(CacheManager::class)->get( - new MediaPath($media), + new StorageMediaPath($media), $size, $sizes, $options, diff --git a/classes/CacheManager.php b/classes/CacheManager.php index 1743f0b..8c5c542 100644 --- a/classes/CacheManager.php +++ b/classes/CacheManager.php @@ -3,6 +3,7 @@ namespace Aweos\Resizer\Classes; use Aweos\Resizer\Lib\MediaPath; +use Aweos\Resizer\Lib\StorageMediaPath; use Cache; class CacheManager @@ -51,7 +52,7 @@ class CacheManager public function biggestVersion(string $path, string $size): string { - $path = new MediaPath($path); + $path = new StorageMediaPath($path); return $this->tagGenerator->singleFile($path, $size); } diff --git a/classes/ResizeJob.php b/classes/ResizeJob.php index a37ba97..28d6b1e 100644 --- a/classes/ResizeJob.php +++ b/classes/ResizeJob.php @@ -4,6 +4,7 @@ namespace Aweos\Resizer\Classes; use Aweos\Resizer\Classes\CacheManager; use Aweos\Resizer\Lib\MediaPath; +use Aweos\Resizer\Lib\TempMediaPath; use Log; use Throwable; @@ -13,9 +14,10 @@ class ResizeJob public function fire($job, $params) { list($file) = $params; - $media = new MediaPath($file); + $media = new TempMediaPath($file); app(ImageResizer::class)->generate($media, $params['update'] ?? false); app(CacheManager::class)->delete($media); + unset ($media); $job->delete(); } diff --git a/composer.json b/composer.json index b23a7fe..6bc049c 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,9 @@ "Aweos\\Resizer\\Compilers\\": "./compilers", "Aweos\\Resizer\\Exceptions\\": "./exceptions", "Aweos\\Resizer\\Compressors\\": "./compressors", - "Aweos\\Resizer\\Lib\\": "./lib" + "Aweos\\Resizer\\Lib\\": "./lib", + "Aweos\\Resizer\\Jobs\\": "./jobs", + "Aweos\\Resizer\\Classes\\": "./classes" } } } diff --git a/jobs/DeleteJob.php b/jobs/DeleteJob.php new file mode 100644 index 0000000..48a9d44 --- /dev/null +++ b/jobs/DeleteJob.php @@ -0,0 +1,36 @@ +delete($media); + app(CacheManager::class)->delete($media); + unset ($media); + + $job->delete(); + } + + public function failed($data, Throwable $e) + { + Log::error("Deleting of image failed", [ + 'message' => $e->getMessage(), + 'exception' => $e, + 'data' => $data, + ]); + + throw $e; + } +} diff --git a/jobs/MoveJob.php b/jobs/MoveJob.php new file mode 100644 index 0000000..4f8f0a1 --- /dev/null +++ b/jobs/MoveJob.php @@ -0,0 +1,38 @@ +rename($old, $new); + app(CacheManager::class)->delete($old); + unset ($old); + unset ($new); + + $job->delete(); + } + + public function failed($data, Throwable $e) + { + Log::error("Deleting of image failed", [ + 'message' => $e->getMessage(), + 'exception' => $e, + 'data' => $data, + ]); + + throw $e; + } +} diff --git a/jobs/ResizeJob.php b/jobs/ResizeJob.php new file mode 100644 index 0000000..d83929a --- /dev/null +++ b/jobs/ResizeJob.php @@ -0,0 +1,35 @@ +generate($media, $params['update'] ?? false); + app(CacheManager::class)->delete($media); + unset ($media); + + $job->delete(); + } + + public function failed($data, Throwable $e) + { + Log::error("Resizing of image failed", [ + 'message' => $e->getMessage(), + 'exception' => $e, + 'data' => $data, + ]); + + throw $e; + } +} diff --git a/lib/MediaPath.php b/lib/MediaPath.php index 6460c92..e34dfbd 100644 --- a/lib/MediaPath.php +++ b/lib/MediaPath.php @@ -9,26 +9,19 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; use MediaLibrary; -class MediaPath +abstract class MediaPath { - private string $path; + protected string $path; + abstract public function get(): string; + abstract public function root(): string; + abstract public function publicUrl(): string; public function __construct(string $path) { $this->path = $path; } - public function root(): string - { - return Storage::path($this->storagePath()); - } - - public function storagePath(): string - { - return "media/{$this->normal()}"; - } - public function normal(): string { return preg_replace('|^/*|', '', $this->path); @@ -41,7 +34,7 @@ class MediaPath public function filename(): string { - return pathinfo($this->path, PATHINFO_FILENAME); + return pathinfo($this->root(), PATHINFO_FILENAME); } public function exists(): bool @@ -60,7 +53,7 @@ class MediaPath public function extension(): string { - return pathinfo($this->path, PATHINFO_EXTENSION); + return pathinfo($this->root(), PATHINFO_EXTENSION); } public function versionsPath(): string @@ -70,17 +63,7 @@ class MediaPath public function versionsDirPath(): string { - return pathinfo("uploads/public/c/{$this->normal()}", PATHINFO_DIRNAME); - } - - public function publicUrl(): string - { - return MediaLibrary::instance()->findFiles($this->path)[0]->publicUrl; - } - - public function get(): string - { - return MediaLibrary::instance()->get($this->path); + return pathinfo($this->versionsPath(), PATHINFO_DIRNAME); } public function shouldProcess(): bool @@ -122,4 +105,9 @@ class MediaPath return $return; } + protected function storagePath(): string + { + return "media/{$this->normal()}"; + } + } diff --git a/lib/StorageMediaPath.php b/lib/StorageMediaPath.php new file mode 100644 index 0000000..e2d0f20 --- /dev/null +++ b/lib/StorageMediaPath.php @@ -0,0 +1,36 @@ +path = $path; + } + + public function root(): string + { + return Storage::path($this->storagePath()); + } + + public function publicUrl(): string + { + return MediaLibrary::instance()->findFiles($this->path)[0]->publicUrl; + } + + public function get(): string + { + return MediaLibrary::instance()->get($this->path); + } + +} diff --git a/lib/TempMediaPath.php b/lib/TempMediaPath.php new file mode 100644 index 0000000..23214a8 --- /dev/null +++ b/lib/TempMediaPath.php @@ -0,0 +1,59 @@ +createTemp(); + } + + public function __destruct() + { + $this->destroyTemp(); + } + + public function root(): string + { + return $this->tempPath; + } + + public function publicUrl(): string + { + return ''; + } + + public function get(): string + { + return file_get_contents($this->tempPath); + } + + private function createTemp(): void + { + $this->tempPath = '/tmp/'.pathinfo($this->path, PATHINFO_FILENAME); + + if (Storage::exists($this->storagePath())) { + $stream = Storage::readStream($this->storagePath()); + $contents = stream_get_contents($stream); + file_put_contents($this->tempPath, $contents); + } + } + + private function destroyTemp(): void + { + @unlink($this->tempPath); + } + +} diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index 26ebfe4..9467e7d 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -6,6 +6,7 @@ use Aweos\Resizer\Classes\CacheManager; use Aweos\Resizer\Classes\TagGenerator; use Aweos\Resizer\Exceptions\ResizerException; use Aweos\Resizer\Lib\MediaPath; +use Aweos\Resizer\Lib\StorageMediaPath; use Aweos\Resizer\Models\Setting; use Aweos\Resizer\Tests\TestCase; use Cache; @@ -21,11 +22,11 @@ class ImageTagTest extends TestCase { parent::setUp(); Storage::fake('local'); + Setting::set('folders', [['folder' => '/pages']]); } public function testItGeneratesAnImageTagWithOriginalSize() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100,200]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); @@ -35,25 +36,23 @@ class ImageTagTest extends TestCase $this->assertEquals( 'width="500" height="500" sizes="(max-width: 100px) 100px, (max-width: 200px) 200px, (max-width: 500px) 500px" srcset="/storage/uploads/public/c/pages/test-100x100.jpg 100w, /storage/uploads/public/c/pages/test-200x200.jpg 200w, /storage/uploads/public/c/pages/test-500x500.jpg 500w" src="/storage/uploads/public/c/pages/test-500x500.jpg"', - app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'original', null), + app(CacheManager::class)->get(new StorageMediaPath('pages/test.jpg'), 'original', null), ); } public function testItThrowsExceptionWhenSizeNotFound() { $this->expectException(ResizerException::class); - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); UploadedFile::fake()->image('test.jpg', 500, 500)->storeAs('uploads/public/c/pages', 'test-500x500.jpg', 'local'); - dd(app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'notfound', null)); + dd(app(CacheManager::class)->get(new StorageMediaPath('pages/test.jpg'), 'notfound', null)); } public function testFetchPdfImage() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100,200]); $this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf')); @@ -68,7 +67,6 @@ class ImageTagTest extends TestCase public function testItGeneratesLazyLoadingTags() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); @@ -76,13 +74,12 @@ class ImageTagTest extends TestCase $this->assertEquals( 'width="100" height="100" sizes="(max-width: 100px) 100px" data-srcset="/storage/uploads/public/c/pages/test-100x100.jpg 100w" data-src="/storage/uploads/public/c/pages/test-100x100.jpg"', - app(CacheManager::class)->get(new MediaPath('pages/test.jpg'), 'original', null, ['lazy' => true]), + app(CacheManager::class)->get(new StorageMediaPath('pages/test.jpg'), 'original', null, ['lazy' => true]), ); } public function testItGeneratesPngImages() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100,200]); $this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get()); @@ -90,13 +87,12 @@ class ImageTagTest extends TestCase $this->assertEquals( 'width="100" height="100" sizes="(max-width: 100px) 100px" srcset="/storage/uploads/public/c/pages/test-100x100.png 100w" src="/storage/uploads/public/c/pages/test-100x100.png"', - app(CacheManager::class)->get(new MediaPath('pages/test.png'), 'original', null), + app(CacheManager::class)->get(new StorageMediaPath('pages/test.png'), 'original', null), ); } public function testItSkipsImagesWithWrongAspectRatio() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100,200]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); @@ -111,7 +107,6 @@ class ImageTagTest extends TestCase public function testItCanGenerateAAspectRatio() { - Setting::set('folders', ['pages']); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1x2']]); Setting::set('breakpoints', [100,200]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); @@ -133,7 +128,6 @@ class ImageTagTest extends TestCase public function testItServesAspectRatiosThatCanBeOnePixelLarger() { - Setting::set('folders', ['pages']); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('breakpoints', [640]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 1920, 899)->get()); @@ -147,7 +141,6 @@ class ImageTagTest extends TestCase public function testGenerateCache() { - Setting::set('folders', ['pages']); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('breakpoints', [640]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 1920, 899)->get()); @@ -159,7 +152,6 @@ class ImageTagTest extends TestCase public function testPickOriginalImageWhenSvgIsGiven() { - Setting::set('folders', ['pages']); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('breakpoints', [640]); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); @@ -174,7 +166,6 @@ class ImageTagTest extends TestCase public function testNormalizeFilePathForCache() { - Setting::set('folders', ['pages']); Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('breakpoints', [640]); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); @@ -185,7 +176,6 @@ class ImageTagTest extends TestCase public function testGenerateSizesAttribute() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100, 200]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); @@ -199,7 +189,6 @@ class ImageTagTest extends TestCase public function testItGeneratesOnlySingleImage() { - Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', [100]); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); diff --git a/tests/MoveTest.php b/tests/MoveTest.php index 0fd20cc..a5f4a46 100644 --- a/tests/MoveTest.php +++ b/tests/MoveTest.php @@ -21,7 +21,7 @@ class MoveTest extends TestCase public function testItMovesAllVersionsOfAFile() { Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->set('resizer.original.pages/alt/test.jpg', 'aa'); - Setting::set('folders', ['pages']); + Setting::set('folders', [['folder' => '/pages']]); Setting::set('sizes', []); Setting::set('breakpoints', []); $this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); @@ -36,7 +36,7 @@ class MoveTest extends TestCase public function testItMovesFilesOnRename() { - Setting::set('folders', ['pages']); + Setting::set('folders', [['folder' => '/pages']]); Setting::set('sizes', []); Setting::set('breakpoints', []); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); @@ -50,7 +50,7 @@ class MoveTest extends TestCase public function testitDeletesOldAndNewCacheWhenMoving() { - Setting::set('folders', ['pages']); + Setting::set('folders', [['folder' => '/pages']]); Setting::set('sizes', []); Setting::set('breakpoints', []); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); @@ -68,7 +68,7 @@ class MoveTest extends TestCase public function testRenamePdfHeaderFile() { - Setting::set('folders', ['pages']); + Setting::set('folders', [['folder' => '/pages']]); Setting::set('sizes', []); Setting::set('breakpoints', []); $this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf')); @@ -83,7 +83,7 @@ class MoveTest extends TestCase public function testItDoesntMoveOtherFilesInTheSameDirectory() { Cache::tags(['resizer', 'resizer.pages/test.jpg'])->set('resizer.original.pages/test.jpg', 'aa'); - Setting::set('folders', ['pages']); + Setting::set('folders', [['folder' => '/pages']]); Setting::set('sizes', []); Setting::set('breakpoints', []); $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get());