diff --git a/compressors/Compressor.php b/compressors/Compressor.php index 2d71ccb..bf36957 100644 --- a/compressors/Compressor.php +++ b/compressors/Compressor.php @@ -14,6 +14,8 @@ abstract class Compressor abstract protected function getExtension(); + abstract public function getExtensionRegex(): string; + public function __construct(MediaPath $media) { $this->media = $media; diff --git a/compressors/DefaultCompressor.php b/compressors/DefaultCompressor.php index dd6610c..522db47 100644 --- a/compressors/DefaultCompressor.php +++ b/compressors/DefaultCompressor.php @@ -9,6 +9,11 @@ class DefaultCompressor extends Compressor { return ''; } + public function getExtensionRegex(): string + { + return '[a-zA-Z0-9]*'; + } + public function make(string $path): array { return []; diff --git a/compressors/Factory.php b/compressors/Factory.php index c991c65..ab5f641 100644 --- a/compressors/Factory.php +++ b/compressors/Factory.php @@ -15,6 +15,12 @@ class Factory 'application/pdf' => PdfCompressor::class, ]; + public array $extensions = [ + 'jpg' => JpgCompressor::class, + 'png' => PngCompressor::class, + 'pdf' => PdfCompressor::class, + ]; + public function fromMedia(string $mediaPath): Compressor { return $this->resolve(new MediaPath($mediaPath)); @@ -22,7 +28,9 @@ class Factory public function resolve(MediaPath $path): Compressor { - $compiler = $this->resolveType($path->type()); + $compiler = is_null($path->type()) + ? $this->resolveExtension($path->extension()) + : $this->resolveType($path->type()); if (is_null($compiler)) { return new $this->default($path); @@ -36,4 +44,9 @@ class Factory return collect($this->types)->get($type); } + private function resolveExtension(string $extension): ?string + { + return collect($this->extensions)->get($extension); + } + } diff --git a/compressors/JpgCompressor.php b/compressors/JpgCompressor.php index 2a96a85..b2851d7 100644 --- a/compressors/JpgCompressor.php +++ b/compressors/JpgCompressor.php @@ -8,6 +8,11 @@ use Storage; class JpgCompressor extends Compressor { + public function getExtensionRegex(): string + { + return 'jpg'; + } + protected function getExtension(): string { return 'jpg'; diff --git a/compressors/PdfCompressor.php b/compressors/PdfCompressor.php index 595a8c4..367911c 100644 --- a/compressors/PdfCompressor.php +++ b/compressors/PdfCompressor.php @@ -6,7 +6,13 @@ use Illuminate\Support\Collection; use Intervention\Image\ImageManager; use Storage; -class PdfCompressor extends Compressor { +class PdfCompressor extends Compressor +{ + + public function getExtensionRegex(): string + { + return 'pdf\.jpg'; + } protected function getExtension(): string { diff --git a/compressors/PngCompressor.php b/compressors/PngCompressor.php index 96a17e5..c41880e 100644 --- a/compressors/PngCompressor.php +++ b/compressors/PngCompressor.php @@ -13,6 +13,11 @@ class PngCompressor extends Compressor { return 'png'; } + public function getExtensionRegex(): string + { + return 'png'; + } + public function make(string $path): array { $output = "/tmp/".str_slug(microtime()); diff --git a/lib/MediaPath.php b/lib/MediaPath.php index 63d97a7..1d5b37e 100644 --- a/lib/MediaPath.php +++ b/lib/MediaPath.php @@ -44,8 +44,12 @@ class MediaPath return pathinfo($this->path, PATHINFO_FILENAME); } - public function type(): string + public function type(): ?string { + if (!file_exists($this->root())) { + return null; + } + return mime_content_type($this->root()); } @@ -86,7 +90,7 @@ class MediaPath $return = collect([]); foreach (Storage::files($this->versionsDirPath()) as $file) { - if (!preg_match_all('|('.preg_quote($this->filename(), '|').')(-[0-9]+x[0-9]+)?(\.[a-zA-Z]+)$|', $file, $matches)) { + if (!preg_match_all('|('.preg_quote($this->filename(), '|').')(-[0-9]+x[0-9]+)?(\.'.$this->compressor()->getExtensionRegex().'+)$|', $file, $matches)) { continue; } diff --git a/tests/DeleteTest.php b/tests/DeleteTest.php index 711954f..ac88145 100644 --- a/tests/DeleteTest.php +++ b/tests/DeleteTest.php @@ -73,4 +73,16 @@ class DeleteTest extends TestCase $this->assertDoesntHaveFile('pages/test-300x500.jpg'); } + public function testItDeletesVersionsOfAPdf() + { + Setting::set('folders', ['pages']); + Setting::set('sizes', []); + Setting::set('breakpoints', []); + UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-100x100.pdf.jpg', 'local'); + + Event::fire('media.file.delete', [null, '/pages/test.pdf', null]); + + $this->assertDoesntHaveFile('pages/test-100x100.pdf.jpg'); + } + } diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index b545d42..911455a 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -135,7 +135,7 @@ class ImageTagTest extends TestCase 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')); - + $output = app(Twig::class)->parse('{{"pages/test.svg" | resize}}'); $this->assertFalse(str_contains($output, 'srcset')); diff --git a/tests/MoveTest.php b/tests/MoveTest.php index aa827e7..cd70d16 100644 --- a/tests/MoveTest.php +++ b/tests/MoveTest.php @@ -24,14 +24,12 @@ class MoveTest extends TestCase Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); - $this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image(100, 100)->get()); - UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages/alt', 'test.jpg', 'local'); - UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages/alt', 'test-200x300.jpg', 'local'); + $this->media->put('/pages/alt/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages/alt', 'test-200x300.jpg', 'local'); Event::fire('media.file.move', [null, '/pages/alt/test.jpg', '/pages/neu/test.jpg']); $this->assertFileCount(0, 'pages/alt'); - $this->assertHasFile('pages/neu/test.jpg'); $this->assertHasFile('pages/neu/test-200x300.jpg'); $this->assertNull(Cache::tags(['resizer', 'resizer.pages/alt/test.jpg'])->get('resizer.original.pages/alt/test.jpg')); } @@ -41,14 +39,12 @@ class MoveTest extends TestCase Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); - $this->media->put('/pages/test.jpg', UploadedFile::fake()->image(100, 100)->get()); - UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local'); + $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local'); Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']); - $this->assertFileCount(2, 'pages'); - $this->assertHasFile('pages/testneu.jpg'); + $this->assertFileCount(1, 'pages'); $this->assertHasFile('pages/testneu-200x300.jpg'); } @@ -58,15 +54,14 @@ class MoveTest extends TestCase Setting::set('folders', ['pages']); Setting::set('sizes', []); Setting::set('breakpoints', []); - $this->media->put('/pages/test.jpg', UploadedFile::fake()->image(100, 100)->get()); - UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test.jpg', 'local'); - UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local'); - UploadedFile::fake()->image(100, 100)->storeAs('uploads/public/c/pages', 'testother.jpg', 'local'); + $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 100, 100)->get()); + UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'test-200x300.jpg', 'local'); + UploadedFile::fake()->image('test.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'testother-200x300.jpg', 'local'); Event::fire('media.file.rename', [null, '/pages/test.jpg', '/pages/testneu.jpg']); - $this->assertFileCount(3, 'pages'); - $this->assertHasFile('pages/testneu.jpg'); + $this->assertFileCount(2, 'pages'); + $this->assertHasFile('pages/testother-200x300.jpg'); $this->assertHasFile('pages/testneu-200x300.jpg'); $this->assertNull(Cache::tags(['resizer', 'resizer.pages/test.jpg'])->get('resizer.original.pages/test.jpg')); } diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index bb440d6..5d53c37 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -16,6 +16,7 @@ class ResizerTest extends TestCase { parent::setUp(); Storage::fake('local'); + $this->media = MediaLibrary::instance(); } public function testDontCopyOriginalFileWhenSettingsAreNotSet(): void @@ -25,8 +26,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', []); $file = UploadedFile::fake()->image('test.jpg', 500, 600); - $media = MediaLibrary::instance(); - $media->put('/pages/test.jpg', $file); + $this->media->put('/pages/test.jpg', $file); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); $this->assertFileCount(0, ''); @@ -39,8 +39,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', []); $file = UploadedFile::fake()->image('test.jpg', 500, 600); - $media = MediaLibrary::instance(); - $media->put('/pages/test.jpg', $file->get()); + $this->media->put('/pages/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); $this->assertFileCount(1, 'pages'); @@ -55,8 +54,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', []); $file = UploadedFile::fake()->image('test.jpg', 500, 600); - $media = MediaLibrary::instance(); - $media->put('/pages/test.jpg', $file->get()); + $this->media->put('/pages/test.jpg', $file->get()); Event::fire('media.file.upload', [null, 'pages/test.jpg', null]); $this->assertFileCount(1, 'pages'); @@ -70,8 +68,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', []); $file = UploadedFile::fake()->image('test.jpg', 500, 600); - $media = MediaLibrary::instance(); - $media->put('/pages-neu/test.jpg', $file->get()); + $this->media->put('/pages-neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages-neu/test.jpg', null]); $this->assertFileCount(0, 'pages'); @@ -85,8 +82,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', []); $file = UploadedFile::fake()->image('test.jpg', 500, 600); - $media = MediaLibrary::instance(); - $media->put('/pages/neu/test.jpg', $file->get()); + $this->media->put('/pages/neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); $this->assertFileCount(1, 'pages/neu'); @@ -99,8 +95,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', ['250']); $file = UploadedFile::fake()->image('test.jpg', 500, 600); - $media = MediaLibrary::instance(); - $media->put('/pages/neu/test.jpg', $file->get()); + $this->media->put('/pages/neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); $this->assertFileCount(2, 'pages/neu'); @@ -115,8 +110,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', ['250']); $file = UploadedFile::fake()->image('test.jpg', 500, 100); - $media = MediaLibrary::instance(); - $media->put('/pages/neu/test.jpg', $file->get()); + $this->media->put('/pages/neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); $this->assertHasFile('pages/neu/test-250x50.jpg'); @@ -129,8 +123,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', ['250']); $file = UploadedFile::fake()->image('test.jpg', 250, 1000); - $media = MediaLibrary::instance(); - $media->put('/pages/neu/test.jpg', $file->get()); + $this->media->put('/pages/neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); $this->assertFileCount(1, 'pages/neu'); @@ -143,8 +136,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', ['250']); $file = UploadedFile::fake()->image('test.jpg', 249, 1000); - $media = MediaLibrary::instance(); - $media->put('/pages/neu/test.jpg', $file->get()); + $this->media->put('/pages/neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); $this->assertFileCount(1, 'pages/neu'); @@ -157,8 +149,7 @@ class ResizerTest extends TestCase Setting::set('breakpoints', ['100']); $file = UploadedFile::fake()->image('test.jpg', 500, 400); - $media = MediaLibrary::instance(); - $media->put('/pages/neu/test.jpg', $file->get()); + $this->media->put('/pages/neu/test.jpg', $file->get()); Event::fire('media.file.upload', [null, '/pages/neu/test.jpg', null]); $this->assertFileCount(4, 'pages/neu'); @@ -174,8 +165,7 @@ class ResizerTest extends TestCase Setting::set('sizes', []); Setting::set('breakpoints', ['100']); - $media = MediaLibrary::instance(); - $media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); + $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); Event::fire('media.file.upload', [null, '/pages/test.svg', null]); $this->assertFileCount(0, 'pages'); @@ -187,8 +177,7 @@ class ResizerTest extends TestCase Setting::set('sizes', []); Setting::set('breakpoints', ['100']); - $media = MediaLibrary::instance(); - $media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get()); + $this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get()); Event::fire('media.file.upload', [null, '/pages/test.png', null]); $this->assertHasFile('pages/test-100x100.png'); @@ -202,8 +191,7 @@ class ResizerTest extends TestCase Setting::set('sizes', []); Setting::set('breakpoints', ['100', '600']); - $media = MediaLibrary::instance(); - $media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); + $this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get()); Event::fire('media.file.upload', [null, '/pages/test.jpg', null]); $this->assertFileCount(2, 'pages'); @@ -217,8 +205,7 @@ class ResizerTest extends TestCase Setting::set('sizes', []); Setting::set('breakpoints', ['100', '600']); - $media = MediaLibrary::instance(); - $media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get()); + $this->media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get()); Event::fire('media.file.upload', [null, '/pages/test.png', null]); $this->assertHasFile('pages/test-100x200.png'); @@ -231,8 +218,7 @@ class ResizerTest extends TestCase Setting::set('sizes', [['name' => 'testas', 'aspect_ratio' => '1x1']]); Setting::set('breakpoints', [200]); - $media = MediaLibrary::instance(); - $media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf')); + $this->media->put('/pages/test.pdf', file_get_contents(__DIR__.'/stub/dummy.pdf')); Event::fire('media.file.upload', [null, '/pages/test.pdf', null]); $this->assertHasFile('pages/test-1275x1650.pdf.jpg');