diff --git a/src/MediaCollections/Commands/CleanCommand.php b/src/MediaCollections/Commands/CleanCommand.php index b4e95f14e..fc79d9640 100644 --- a/src/MediaCollections/Commands/CleanCommand.php +++ b/src/MediaCollections/Commands/CleanCommand.php @@ -93,7 +93,7 @@ protected function deleteFilesGeneratedForDeprecatedConversions(): void $this->deleteConversionFilesForDeprecatedConversions($media); if ($media->responsive_images) { - $this->deleteResponsiveImagesForDeprecatedConversions($media); + $this->deleteDeprecatedResponsiveImages($media); } if ($this->rateLimit) { @@ -123,9 +123,10 @@ protected function deleteConversionFilesForDeprecatedConversions(Media $media): }); } - protected function deleteResponsiveImagesForDeprecatedConversions(Media $media): void + protected function deleteDeprecatedResponsiveImages(Media $media): void { - $conversionNames = ConversionCollection::createForMedia($media) + $conversionNamesWithResponsiveImages = ConversionCollection::createForMedia($media) + ->filter(fn (Conversion $conversion) => $conversion->shouldGenerateResponsiveImages()) ->map(fn (Conversion $conversion) => $conversion->getName()) ->push('media_library_original'); @@ -134,7 +135,7 @@ protected function deleteResponsiveImagesForDeprecatedConversions(Media $media): collect($responsiveImagesGeneratedFor) ->map(fn (string $generatedFor) => $media->responsiveImages($generatedFor)) - ->reject(fn (RegisteredResponsiveImages $responsiveImages) => $conversionNames->contains($responsiveImages->generatedFor)) + ->reject(fn (RegisteredResponsiveImages $responsiveImages) => $conversionNamesWithResponsiveImages->contains($responsiveImages->generatedFor)) ->each(function (RegisteredResponsiveImages $responsiveImages) { if (! $this->isDryRun) { $responsiveImages->delete(); diff --git a/tests/Conversions/Commands/CleanCommandTest/CleanResponsiveImagesTest.php b/tests/Conversions/Commands/CleanCommandTest/CleanResponsiveImagesTest.php deleted file mode 100644 index 43ba77165..000000000 --- a/tests/Conversions/Commands/CleanCommandTest/CleanResponsiveImagesTest.php +++ /dev/null @@ -1,28 +0,0 @@ -testModelWithResponsiveImages - ->addMedia($this->getTestJpg()) - ->preservingOriginal() - ->toMediaCollection(); - - $deprecatedResponsiveImageFileName = 'test___deprecatedConversion_50_41.jpg'; - $deprecatedReponsiveImagesPath = $this->getMediaDirectory("1/responsive-images/{$deprecatedResponsiveImageFileName}"); - touch($deprecatedReponsiveImagesPath); - - $originalResponsiveImagesContent = $media->responsive_images; - $newResponsiveImages = $originalResponsiveImagesContent; - $newResponsiveImages['deprecatedConversion'] = $originalResponsiveImagesContent['thumb']; - $newResponsiveImages['deprecatedConversion']['urls'][0] = $deprecatedResponsiveImageFileName; - $media->responsive_images = $newResponsiveImages; - $media->save(); - - $this->artisan('media-library:clean'); - - $media->refresh(); - - expect($media->responsive_images)->toEqual($originalResponsiveImagesContent); - $this->assertFileDoesNotExist($deprecatedReponsiveImagesPath); -}); diff --git a/tests/Conversions/Commands/CleanConversionsTest.php b/tests/Conversions/Commands/CleanConversionsTest.php index 9685b6bc0..0d336eb7f 100644 --- a/tests/Conversions/Commands/CleanConversionsTest.php +++ b/tests/Conversions/Commands/CleanConversionsTest.php @@ -142,14 +142,14 @@ expect($this->getMediaDirectory("{$this->media['model1']['collection2']->id}/test.jpg"))->toBeFile(); }); -it('can clean responsive images', function () { +it('can clean responsive images for deprecated conversions', function () { $media = $this->testModelWithResponsiveImages ->addMedia($this->getTestJpg()) ->preservingOriginal() ->toMediaCollection(); - $deprecatedResponsiveImageFileName = 'test___deprecatedConversion_50_41.jpg'; - $deprecatedReponsiveImagesPath = $this->getMediaDirectory("5/responsive-images/{$deprecatedResponsiveImageFileName}"); + $deprecatedResponsiveImageFileName = "{$media->file_name}___deprecatedConversion_50_41.jpg"; + $deprecatedReponsiveImagesPath = $this->getMediaDirectory("{$media->id}/responsive-images/{$deprecatedResponsiveImageFileName}"); touch($deprecatedReponsiveImagesPath); $originalResponsiveImagesContent = $media->responsive_images; @@ -167,6 +167,32 @@ $this->assertFileDoesNotExist($deprecatedReponsiveImagesPath); }); +it('can clean responsive images for active conversions without responsive images', function () { + $media = $this->testModelWithConversion + ->addMedia($this->getTestJpg()) + ->preservingOriginal() + ->toMediaCollection(); + + $thumbResponsiveImageFileName = "{$media->file_name}___thumb_340_280.jpg"; + $thumbReponsiveImagesPath = $this->getMediaDirectory("{$media->id}/responsive-images/{$thumbResponsiveImageFileName}"); + mkdir($this->getMediaDirectory("{$media->id}/responsive-images")); + touch($thumbReponsiveImagesPath); + + $originalResponsiveImagesContent = $media->responsive_images; + $newResponsiveImages = $originalResponsiveImagesContent; + $newResponsiveImages['thumb']['base64svg'] = "data:image/svg+xml;base64,PCPg=="; + $newResponsiveImages['thumb']['urls'][0] = $thumbResponsiveImageFileName; + $media->responsive_images = $newResponsiveImages; + $media->save(); + + $this->artisan('media-library:clean'); + + $media->refresh(); + + expect($media->responsive_images)->toEqual($originalResponsiveImagesContent); + $this->assertFileDoesNotExist($thumbReponsiveImagesPath); +}); + it('will throw an exception when using a non existing disk', function () { $this->expectException(DiskDoesNotExist::class);