2018-05-26 22:44:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
2019-07-25 04:24:27 +00:00
|
|
|
use DB;
|
2018-05-26 22:44:44 +00:00
|
|
|
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
|
2018-08-28 03:07:36 +00:00
|
|
|
use App\Media;
|
|
|
|
use Illuminate\Console\Command;
|
2018-05-26 22:44:44 +00:00
|
|
|
|
|
|
|
class CatchUnoptimizedMedia extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'media:optimize';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Find and optimize media that has not yet been optimized.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Media $media)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-07-25 04:24:27 +00:00
|
|
|
DB::transaction(function() {
|
|
|
|
Media::whereNull('processed_at')
|
2021-01-17 08:45:56 +00:00
|
|
|
->where('skip_optimize', '!=', true)
|
2019-08-21 04:22:25 +00:00
|
|
|
->whereNull('remote_url')
|
2019-07-25 04:24:27 +00:00
|
|
|
->whereNotNull('status_id')
|
|
|
|
->whereNotNull('media_path')
|
2021-01-17 08:45:56 +00:00
|
|
|
->where('created_at', '>', now()->subHours(1))
|
2019-07-25 04:24:27 +00:00
|
|
|
->whereIn('mime', [
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
])
|
|
|
|
->chunk(50, function($medias) {
|
|
|
|
foreach ($medias as $media) {
|
|
|
|
ImageOptimize::dispatch($media);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-05-26 22:44:44 +00:00
|
|
|
}
|
|
|
|
}
|