Files
hstream/app/Jobs/GetFileSizeFromCDN.php
2025-09-18 15:31:27 +02:00

72 lines
2.3 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\Downloads;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class GetFileSizeFromCDN implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $downloadId;
/**
* Create a new job instance.
*/
public function __construct(int $id)
{
$this->downloadId = $id;
}
/**
* Execute the job.
*/
public function handle(): void
{
// Retrieve the download record, return if not found
$download = Downloads::find($this->downloadId);
if (!$download) {
Log::error("Download not found for ID: {$this->downloadId}");
return;
}
// Generate encrypted file path and expiration
$endpoint = config('hstream.download_domain_4k')[0];
$serverPath = $download->type === 'UHD' ? 'hentai/' : 'hentai-1080p/';
$file = Crypt::encryptString($serverPath.$download->url);
$expire = Crypt::encryptString(Carbon::now()->addHours(6)->toDateTimeString());
try {
// Send HTTP request to the endpoint
$response = Http::get($endpoint . '/getSize/' . $file . '/' . $expire);
// Check if response is successful
if ($response->successful()) {
$size = $response->json()['size'];
// Update the download record with the retrieved size
$download->size = $size;
$download->save();
Log::info("Updated size for download ID: {$this->downloadId}");
} else {
Log::error("Failed to retrieve size for download ID: {$this->downloadId}, HTTP status: {$response->status()}");
}
} catch (RequestException $e) {
Log::error("HTTP request failed for download ID: {$this->downloadId}, error: " . $e->getMessage());
} catch (\Exception $e) {
Log::error("An error occurred for download ID: {$this->downloadId}, error: " . $e->getMessage());
}
}
}