Skip to content

Commit

Permalink
feat: add ability to cleanup images that do not correspond to any shot
Browse files Browse the repository at this point in the history
  • Loading branch information
mdshack committed Jan 21, 2024
1 parent a1bdb88 commit 0521298
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions app/Console/Commands/CleanImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Console\Commands;

use App\Models\Shot;
use App\Models\ShotReaction;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

Expand All @@ -13,7 +14,7 @@ class CleanImages extends Command
*
* @var string
*/
protected $signature = 'shotshare:clean-images';
protected $signature = 'shotshare:clean-images {--only-missing : Only clean up files that don\'t have corresponding database records}';

/**
* The console command description.
Expand All @@ -27,10 +28,21 @@ class CleanImages extends Command
*/
public function handle()
{
// Cleanup Database Records
Shot::query()->truncate();
if ($this->option('only-missing')) {
$existingPaths = Shot::select('path')->get()->pluck('path')->toArray();

// Cleanup Filesystem
Storage::deleteDirectory('uploads');
foreach (Storage::allFiles('uploads') as $uploadedFile) {
if (! in_array($uploadedFile, $existingPaths)) {
Storage::delete($uploadedFile);
}
}
} else {
// Cleanup Database Records
ShotReaction::where('id', '>=', 0)->delete();
Shot::where('id', '>=', 0)->delete();

// Cleanup Filesystem
Storage::deleteDirectory('uploads');
}
}
}

0 comments on commit 0521298

Please sign in to comment.