MediaGarbageCollector.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\{Media, Status};
  5. use Carbon\Carbon;
  6. use App\Services\MediaStorageService;
  7. class MediaGarbageCollector extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'media:gc';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Delete media uploads not attached to any active statuses';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $limit = 20000;
  38. $gc = Media::whereNull('status_id')
  39. ->where('created_at', '<', Carbon::now()->subHours(12)->toDateTimeString())
  40. ->orderBy('created_at','asc')
  41. ->take($limit)
  42. ->get();
  43. $bar = $this->output->createProgressBar($gc->count());
  44. $bar->start();
  45. foreach($gc as $media) {
  46. MediaStorageService::delete($media);
  47. $media->forceDelete();
  48. $bar->advance();
  49. }
  50. $bar->finish();
  51. }
  52. }