Skip to content

Commit

Permalink
Cleanup old w/ folders
Browse files Browse the repository at this point in the history
  • Loading branch information
ilestis committed May 20, 2024
1 parent a37dde8 commit ee532ba
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions app/Console/Commands/Cleanup/CleanupImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Console\Commands\Cleanup;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class CleanupImages extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cleanup:images';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete old images from s3';

protected int $count = 0;

/**
* Execute the console command.
*/
public function handle()
{
$directories = Storage::directories('w/');
$chunks = array_chunk($directories, 200);
foreach ($chunks as $chunk) {
$ids = [];
foreach ($chunk as $path) {
$ids[] = Str::after($path, 'w/');
}
$select = "SELECT id FROM campaigns WHERE id in (?)";
$db = DB::select($select, [implode(',', $ids)]);
foreach ($db as $existingId) {
unset($ids[array_search($existingId->id, $ids)]);
}

if (empty($ids)) {
continue;
}
foreach ($ids as $id) {
if (empty($id)) {
continue;
}
Storage::deleteDirectory('w/' . $id);
$this->count++;
}
}

$this->info('Deleted ' . $this->count . ' images/folders.');
}
}

0 comments on commit ee532ba

Please sign in to comment.