70 lines
2.0 KiB
PHP
70 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Upload;
|
|
use App\Services\GoogleDrive\GoogleDriveService;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class ProcessEventUpload implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
|
|
public function __construct(
|
|
public Upload $upload,
|
|
public string $tempFilePath
|
|
) {}
|
|
|
|
public function handle(GoogleDriveService $googleDrive): void
|
|
{
|
|
$upload = $this->upload;
|
|
$event = $upload->event;
|
|
$user = $event->user;
|
|
|
|
$upload->update(['status' => 'uploading', 'upload_started_at' => now()]);
|
|
|
|
try {
|
|
if (! $event->google_drive_folder_id) {
|
|
throw new \RuntimeException('Event has no Google Drive folder configured.');
|
|
}
|
|
|
|
if (! file_exists($this->tempFilePath)) {
|
|
throw new \RuntimeException('Temporary file not found.');
|
|
}
|
|
|
|
$result = $googleDrive->uploadFile(
|
|
$user,
|
|
$this->tempFilePath,
|
|
$event->google_drive_folder_id,
|
|
$upload->original_filename,
|
|
$upload->mime_type
|
|
);
|
|
|
|
$upload->update([
|
|
'google_drive_file_id' => $result['id'],
|
|
'google_drive_web_link' => $result['webViewLink'] ?? null,
|
|
'status' => 'completed',
|
|
'upload_completed_at' => now(),
|
|
'error_message' => null,
|
|
]);
|
|
} catch (\Throwable $e) {
|
|
$upload->update([
|
|
'status' => 'failed',
|
|
'error_message' => $e->getMessage(),
|
|
'upload_completed_at' => now(),
|
|
]);
|
|
throw $e;
|
|
} finally {
|
|
if (file_exists($this->tempFilePath)) {
|
|
@unlink($this->tempFilePath);
|
|
}
|
|
}
|
|
}
|
|
}
|