CatchUnoptimizedMedia.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. $hasLimit = (bool) config('media.image_optimize.catch_unoptimized_media_hour_limit');
  38. Media::whereNull('processed_at')
  39. ->when($hasLimit, function($q, $hasLimit) {
  40. $q->where('created_at', '>', now()->subHours(1));
  41. })->whereNull('remote_url')
  42. ->whereNotNull('status_id')
  43. ->whereNotNull('media_path')
  44. ->whereIn('mime', [
  45. 'image/jpg',
  46. 'image/jpeg',
  47. 'image/png',
  48. ])
  49. ->chunk(50, function($medias) {
  50. foreach ($medias as $media) {
  51. if ($media->skip_optimize) continue;
  52. ImageOptimize::dispatch($media);
  53. }
  54. });
  55. }
  56. }