From fb6c24ff6cb7d476276ea74ae8039e5dfdda89d8 Mon Sep 17 00:00:00 2001 From: Michel Bardelmeijer Date: Tue, 24 Oct 2023 12:16:38 +0200 Subject: [PATCH] Add collection name option, fix phpstan, add extra test --- .../Commands/CleanCommand.php | 14 ++++++++- src/MediaCollections/MediaRepository.php | 8 +++++ .../Conversions/Commands/CleanCommandTest.php | 31 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/MediaCollections/Commands/CleanCommand.php b/src/MediaCollections/Commands/CleanCommand.php index c6213de39..5441dd751 100644 --- a/src/MediaCollections/Commands/CleanCommand.php +++ b/src/MediaCollections/Commands/CleanCommand.php @@ -94,7 +94,7 @@ public function getMediaItems(): Collection protected function deleteOrphanedMediaItems(): void { - $this->mediaRepository->getOrphans()->each(function (Media $media) { + $this->getOrphanedMediaItems()->each(function (Media $media): void { if ($this->isDryRun) { $this->info("Orphaned Media[id={$media->id}] found"); @@ -107,6 +107,18 @@ protected function deleteOrphanedMediaItems(): void }); } + /** @return Collection */ + protected function getOrphanedMediaItems(): Collection + { + $collectionName = $this->argument('collectionName'); + + if (is_string($collectionName)) { + return $this->mediaRepository->getOrphansByCollectionName($collectionName); + } + + return $this->mediaRepository->getOrphans(); + } + protected function deleteFilesGeneratedForDeprecatedConversions(): void { $this->getMediaItems()->each(function (Media $media) { diff --git a/src/MediaCollections/MediaRepository.php b/src/MediaCollections/MediaRepository.php index 770396599..cc499eb96 100644 --- a/src/MediaCollections/MediaRepository.php +++ b/src/MediaCollections/MediaRepository.php @@ -95,6 +95,14 @@ public function getOrphans(): DbCollection ->get(); } + public function getOrphansByCollectionName(string $collectionName): DbCollection + { + return $this->query() + ->whereDoesntHave('model') + ->where('collection_name', $collectionName) + ->get(); + } + protected function query(): Builder { return $this->model->newQuery(); diff --git a/tests/Conversions/Commands/CleanCommandTest.php b/tests/Conversions/Commands/CleanCommandTest.php index 66a76cf44..9f1f162d4 100644 --- a/tests/Conversions/Commands/CleanCommandTest.php +++ b/tests/Conversions/Commands/CleanCommandTest.php @@ -304,6 +304,37 @@ ]); }); +it('can clean orphaned media items when enabled for specific collections', function () { + $mediaToClean = TestModel::create(['name' => 'test.jpg']) + ->addMedia($this->getTestJpg()) + ->preservingOriginal() + ->toMediaCollection('collection-to-clean'); + + $mediaToKeep = TestModel::create(['name' => 'test.jpg']) + ->addMedia($this->getTestJpg()) + ->preservingOriginal() + ->toMediaCollection('collection-to-keep'); + + // Delete quietly to avoid deleting the related media file. + $mediaToClean->model->deleteQuietly(); + $mediaToKeep->model->deleteQuietly(); + + $this->artisan('media-library:clean', [ + '--delete-orphaned' => 'true', + 'collectionName' => 'collection-to-clean', + ]); + + // Media should be deleted from the database. + $this->assertDatabaseMissing('media', [ + 'id' => $mediaToClean->id, + ]); + + // This media should still exist. + $this->assertDatabaseHas('media', [ + 'id' => $mediaToKeep->id, + ]); +}); + it('can won\'t clean orphaned media items when disabled', function () { $media = TestModel::create(['name' => 'test.jpg']) ->addMedia($this->getTestJpg())