Skip to content

Commit

Permalink
Update CleanCommand to delete depreciated responsive images (#3003)
Browse files Browse the repository at this point in the history
* Update CleanCommand to delete depreciated responsive images

When running `php artisan media-library:clean` we should be clearing responsive images for conversions that are no longer used, as well as for active conversions that are no longer using responsive images. Fixes  #2397

* Add unit test for active conversions without responsive images

* Remove duplicate test and file
  • Loading branch information
michaellindahl authored Aug 22, 2022
1 parent b3952e7 commit e66c599
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
9 changes: 5 additions & 4 deletions src/MediaCollections/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function deleteFilesGeneratedForDeprecatedConversions(): void
$this->deleteConversionFilesForDeprecatedConversions($media);

if ($media->responsive_images) {
$this->deleteResponsiveImagesForDeprecatedConversions($media);
$this->deleteDeprecatedResponsiveImages($media);
}

if ($this->rateLimit) {
Expand Down Expand Up @@ -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');

Expand All @@ -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();
Expand Down

This file was deleted.

32 changes: 29 additions & 3 deletions tests/Conversions/Commands/CleanConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit e66c599

Please sign in to comment.