File size: 3,235 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | <?php
namespace App\Console\Commands;
use App\Models\SettingTwo;
use App\Models\UserOpenai;
use App\Services\Ai\FalAI;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
class FluxProQueueCheck extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:flux-pro-queue-check';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle(): void
{
self::updateFluxProImages();
}
public static function updateFluxProImages(): void
{
UserOpenai::query()
->where('response', 'FL')
->where('status', 'IN_QUEUE')
->whereNotNull('request_id')
->get()
->each(function ($item) {
$output = FalAI::check($item->request_id);
if ($output) {
$payload = data_get($item, 'payload');
if ($payload && is_array($payload)) {
$payload['size'] = data_get($output, 'size');
}
$image = data_get($output, 'image.url');
$image = (new self)->downloadImageToStorage($image);
$item->update([
'output' => $image ?: $item->output,
'payload' => $payload,
'status' => 'COMPLETED',
]);
}
});
}
public function downloadImageToStorage($url = null, $filename = null)
{
if (! $url) {
return null;
}
// Resmi URL'den indir
$response = Http::get($url);
// Eğer dosya başarıyla indirildiyse devam et
if ($response->successful()) {
// Dosya içeriğini al
$fileContent = $response->body();
// Dosya uzantısını belirleyin
$extension = pathinfo($url, PATHINFO_EXTENSION);
// Eğer dosya adı verilmemişse, bir dosya adı oluşturun
if (! $filename) {
$filename = uniqid('image_') . '.' . $extension;
} else {
$filename .= '.' . $extension;
}
$image_storage = SettingTwo::query()->first()?->ai_image_storage;
if ($image_storage == 'r2') {
Storage::disk('r2')->put($filename, $fileContent);
return Storage::disk('r2')->url($filename);
} elseif ($image_storage == 's3') {
Storage::disk('s3')->put($filename, $fileContent);
return Storage::disk('s3')->url($filename);
}
//save file on local storage or aws s3
Storage::disk('thumbs')->put($filename, $fileContent);
$dump = Storage::disk('public')->put($filename, $fileContent);
if ($dump) {
return '/uploads/' . $filename;
}
return 'error';
}
// return false when fail
return null;
}
}
|