MediaGarbageCollector.php 1.0 KB

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