-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |