This commit is contained in:
deploy-bot committed 2026-12-04 17:15:56 +00:00
commit 217b733bf2
286 files changed
+19180

No files matched your search

+104
View File
@@ -0,0 +1,104 @@
<?php
namespace IriksIT\Manager\Console;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use IriksIT\Manager\Models\Media;
use IriksIT\Manager\Modules\Media\MediaLibrary;
use Throwable;
/**
* Finds files of the media library that nothing uses any more (no post, page or other registered source holds their address),
* and with `--delete` removes them. Nothing is deleted without `--delete`, and only files older than `--older-than` days.
* `--orphans` looks for the opposite: files on a disk that no row knows (only for a disk that can list its files).
*
* php artisan manager:media-prune --older-than=30
* php artisan manager:media-prune --older-than=30 --delete
* php artisan manager:media-prune --orphans --disk=public
*/
class MediaPruneCommand extends Command
{
protected $signature = 'manager:media-prune
{--older-than=30 : Only files that were added more than this many days ago}
{--delete : Really delete (without this it only lists)}
{--orphans : List (and with --delete, remove) files on the disk that no media row knows}
{--disk= : For --orphans: the disk to look at (default: the disk of the media library)}';
protected $description = 'Find media files that nothing uses any more, and delete them on request';
public function handle(MediaLibrary $library): int
{
return $this->option('orphans') ? $this->orphans() : $this->unused($library);
}
private function unused(MediaLibrary $library): int
{
$days = max(0, (int) $this->option('older-than'));
$candidates = Media::query()->where('created_at', '<', now()->subDays($days))->orderBy('id')->get();
$counts = $library->usageCounts($candidates);
$found = $deleted = $failed = 0;
foreach ($candidates as $media) {
if (($counts[$media->id] ?? 0) > 0) {
continue;
}
$found++;
$label = "#{$media->id} {$media->filename} (".round($media->size / 1024).' KB)';
if (! $this->option('delete')) {
$this->line("{$label}: not used.");
continue;
}
try {
$library->delete($media);
$this->line("{$label}: deleted.");
$deleted++;
} catch (Throwable $e) {
$this->error("{$label}: could not be deleted ({$e->getMessage()})");
$failed++;
}
}
$this->info($this->option('delete') ? "Deleted {$deleted}, failed {$failed}." : "{$found} not used. Run again with --delete to remove them.");
return $failed === 0 ? self::SUCCESS : self::FAILURE;
}
private function orphans(): int
{
$name = (string) ($this->option('disk') ?: config('manager.media.disk'));
$known = Media::query()->where('disk', $name)->pluck('location')->flip();
$disk = Storage::disk($name);
try {
$files = $disk->allFiles('media');
} catch (Throwable) {
$files = [];
}
if ($files === [] && Media::query()->where('disk', $name)->exists()) {
$this->warn("The disk `{$name}` cannot list its files (or has none), so there is nothing to compare.");
return self::SUCCESS;
}
$found = 0;
foreach ($files as $file) {
if ($known->has($file)) {
continue;
}
$found++;
$this->line($this->option('delete') ? "{$file}: ".($disk->delete($file) ? 'deleted.' : 'could not be deleted.') : "{$file}: no row knows it.");
}
$this->info($this->option('delete') ? "Looked at {$found} unknown files." : "{$found} files that no row knows. Run again with --delete to remove them.");
return self::SUCCESS;
}
}