49 lines
1.7 KiB
PHP
49 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Verifikator\PengajuanPresensi;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Throwable;
|
|
|
|
class WebPresensiDocumentService
|
|
{
|
|
public function __construct(private GoogleDriveService $drive) {}
|
|
|
|
public function upload(PengajuanPresensi $pengajuan): ?string
|
|
{
|
|
$rawFile = (string) $pengajuan->getRawOriginal('file');
|
|
$host = strtolower((string) parse_url($rawFile, PHP_URL_HOST));
|
|
if (in_array($host, ['drive.google.com', 'docs.google.com'], true)) {
|
|
return $rawFile;
|
|
}
|
|
|
|
try {
|
|
if ($pengajuan->bdr_id === null && ! str_starts_with($rawFile, 'http')) {
|
|
$content = Storage::disk('public')->get("{$pengajuan->tipe}/presensi/{$pengajuan->periode}/{$rawFile}");
|
|
} else {
|
|
$content = Http::timeout(15)->get($pengajuan->file)->throw()->body();
|
|
}
|
|
|
|
if (! is_string($content) || ! str_starts_with(ltrim($content), '%PDF-')) {
|
|
throw new \RuntimeException('File sumber bukan PDF yang valid.');
|
|
}
|
|
|
|
return $this->drive->uploadPdf(
|
|
$content,
|
|
$pengajuan->nip,
|
|
$pengajuan->pegawai?->nama ?? $pengajuan->nip,
|
|
$pengajuan->tanggal_mulai,
|
|
$pengajuan->keterangan ?: 'Pengajuan absen',
|
|
'pengajuan-web-'.$pengajuan->id.'.pdf'
|
|
) ?: null;
|
|
} catch (Throwable $error) {
|
|
Log::error("Unggah dokumen presensi web #{$pengajuan->id}: ".$error->getMessage());
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|