CatchUnoptimizedMedia.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Console\Commands;
  3. use DB;
  4. use App\Jobs\ImageOptimizePipeline\ImageOptimize;
  5. use App\Media;
  6. use Illuminate\Console\Command;
  7. class CatchUnoptimizedMedia extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'media:optimize';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Find and optimize media that has not yet been optimized.';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct(Media $media)
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. Media::whereNull('processed_at')
  38. // This is commented out because Instagram imported posts would not get uploaded to remote storage
  39. // ->where('created_at', '>', now()->subHours(1))
  40. ->whereNull('remote_url')
  41. ->whereNotNull('status_id')
  42. ->whereNotNull('media_path')
  43. ->whereIn('mime', [
  44. 'image/jpeg',
  45. 'image/png',
  46. ])
  47. ->chunk(50, function($medias) {
  48. foreach ($medias as $media) {
  49. if ($media->skip_optimize) continue;
  50. ImageOptimize::dispatch($media);
  51. }
  52. });
  53. }
  54. }