Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix media-library:clean command when using custom path generators #2925

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/MediaCollections/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Spatie\MediaLibrary\MediaCollections\MediaRepository;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\ResponsiveImages\RegisteredResponsiveImages;
use Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator;
use Spatie\MediaLibrary\Support\PathGenerator\PathGeneratorFactory;

class CleanCommand extends Command
{
Expand All @@ -34,8 +34,6 @@ class CleanCommand extends Command

protected Factory $fileSystem;

protected DefaultPathGenerator $basePathGenerator;

protected bool $isDryRun = false;

protected int $rateLimit = 0;
Expand All @@ -44,12 +42,10 @@ public function handle(
MediaRepository $mediaRepository,
FileManipulator $fileManipulator,
Factory $fileSystem,
DefaultPathGenerator $basePathGenerator
) {
$this->mediaRepository = $mediaRepository;
$this->fileManipulator = $fileManipulator;
$this->fileSystem = $fileSystem;
$this->basePathGenerator = $basePathGenerator;

if (! $this->confirmToProceed()) {
return;
Expand Down Expand Up @@ -110,11 +106,12 @@ protected function deleteConversionFilesForDeprecatedConversions(Media $media):
{
$conversionFilePaths = ConversionCollection::createForMedia($media)->getConversionsFiles($media->collection_name);

$conversionPath = $this->basePathGenerator->getPathForConversions($media);
$conversionPath = PathGeneratorFactory::create($media)->getPathForConversions($media);
$currentFilePaths = $this->fileSystem->disk($media->disk)->files($conversionPath);

collect($currentFilePaths)
->reject(fn (string $currentFilePath) => $conversionFilePaths->contains(basename($currentFilePath)))
->reject(fn (string $currentFilePath) => $media->file_name === basename($currentFilePath))
->each(function (string $currentFilePath) use ($media) {
if (! $this->isDryRun) {
$this->fileSystem->disk($media->disk)->delete($currentFilePath);
Expand Down
62 changes: 62 additions & 0 deletions tests/Conversions/Commands/CleanConversionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
use Illuminate\Support\Facades\DB;
use Spatie\MediaLibrary\MediaCollections\Exceptions\DiskDoesNotExist;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator;
use Spatie\MediaLibrary\Tests\Support\PathGenerator\CustomPathGenerator;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModel;
use Spatie\MediaLibrary\Tests\TestSupport\TestModels\TestModelWithConversion;
use Spatie\MediaLibrary\Tests\TestSupport\TestPathGenerators\TestPathGeneratorConversionsInOriginalImageDirectory;

beforeEach(function () {
$this->media['model1']['collection1'] = $this->testModel
Expand Down Expand Up @@ -172,3 +175,62 @@
$this->artisan('media-library:clean')
->assertExitCode(1);
});

it('can clean deprecated conversion files in custom path', function () {
$this->config = app('config');

$this->urlGenerator = new DefaultUrlGenerator($this->config);

$this->pathGenerator = new CustomPathGenerator();

$this->urlGenerator->setPathGenerator($this->pathGenerator);

config()->set('media-library.custom_path_generators', [
TestModelWithConversion::class => CustomPathGenerator::class,
]);

$media = $this->testModelWithConversion
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection();

$deprecatedImage = $this->getMediaDirectory(md5($media->id) . "/c/test-deprecated.jpg");

touch($deprecatedImage);
expect($deprecatedImage)->toBeFile();

$this->artisan('media-library:clean');

$this->assertFileDoesNotExist($deprecatedImage);
expect($this->getMediaDirectory(md5($media->id) . "/c/test-thumb.jpg"))->toBeFile();
});

it('can clean deprecated conversion files in same path as original image', function () {
$this->config = app('config');

$this->urlGenerator = new DefaultUrlGenerator($this->config);

$this->pathGenerator = new TestPathGeneratorConversionsInOriginalImageDirectory();

$this->urlGenerator->setPathGenerator($this->pathGenerator);

config()->set('media-library.custom_path_generators', [
TestModelWithConversion::class => TestPathGeneratorConversionsInOriginalImageDirectory::class,
]);

$media = $this->testModelWithConversion
->addMedia($this->getTestJpg())
->preservingOriginal()
->toMediaCollection();

$deprecatedImage = $this->getMediaDirectory("{$media->id}/test-deprecated.jpg");

touch($deprecatedImage);
expect($deprecatedImage)->toBeFile();

$this->artisan('media-library:clean');

$this->assertFileDoesNotExist($deprecatedImage);
expect($this->getMediaDirectory("{$media->id}/test-thumb.jpg"))->toBeFile();
expect($this->getMediaDirectory("{$media->id}/test.jpg"))->toBeFile();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\MediaLibrary\Tests\TestSupport\TestPathGenerators;

use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\DefaultPathGenerator;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;

class TestPathGeneratorConversionsInOriginalImageDirectory extends DefaultPathGenerator implements PathGenerator
{
public function getPathForConversions(Media $media): string
{
return $this->getPath($media);
}
}