Skip to content

Commit

Permalink
feat: media-library:clean make use of media prefix (#3704)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustToNy authored Sep 2, 2024
1 parent 5486261 commit ff589ea
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/MediaCollections/Commands/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,24 @@ protected function deleteOrphanedDirectories(): void
throw DiskDoesNotExist::create($diskName);
}

$prefix = config('media-library.prefix', '');

if ($prefix !== '') {
$prefix = trim($prefix, '/').'/';
}

$mediaIds = $this->mediaRepository->allIds();

/** @var array<int, string> */
$directories = $this->fileSystem->disk($diskName)->directories();
$directories = $this->fileSystem->disk($diskName)->directories($prefix);

collect($directories)
->map(fn (string $directory) => str_replace($prefix, '', $directory))
->filter(fn (string $directory) => is_numeric($directory))
->reject(fn (string $directory) => $mediaIds->contains((int) $directory))
->each(function (string $directory) use ($diskName) {
->each(function (string $directory) use ($diskName, $prefix) {
$directory = $prefix.$directory;

if (! $this->isDryRun) {
$this->fileSystem->disk($diskName)->deleteDirectory($directory);
}
Expand Down

0 comments on commit ff589ea

Please sign in to comment.