2026-03-19 20:05:38 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2026-03-20 00:36:52 +09:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2026-03-19 20:05:38 +09:00
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-20 00:36:52 +09:00
|
|
|
* R2 파일 Presigned URL 리다이렉트
|
2026-03-19 20:05:38 +09:00
|
|
|
*
|
2026-03-20 00:36:52 +09:00
|
|
|
* API에서 R2 presigned URL을 발급받아 브라우저를 직접 R2로 리다이렉트.
|
|
|
|
|
* presigned URL은 5분간 캐시하여 동일 이미지 반복 요청 시 API 호출 최소화.
|
2026-03-19 20:05:38 +09:00
|
|
|
*/
|
|
|
|
|
class FileViewController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function show(int $id)
|
|
|
|
|
{
|
2026-03-20 00:36:52 +09:00
|
|
|
$cacheKey = "file_presigned_url:{$id}";
|
|
|
|
|
|
|
|
|
|
$url = Cache::remember($cacheKey, now()->addMinutes(5), function () use ($id) {
|
|
|
|
|
$baseUrl = config('services.api.base_url', 'https://api.sam.kr');
|
|
|
|
|
$apiKey = config('services.api.key');
|
|
|
|
|
$token = session('api_access_token', '');
|
|
|
|
|
|
|
|
|
|
$response = Http::baseUrl($baseUrl)
|
|
|
|
|
->withoutVerifying()
|
|
|
|
|
->withHeaders([
|
|
|
|
|
'X-API-KEY' => $apiKey,
|
|
|
|
|
'X-TENANT-ID' => session('selected_tenant_id', 1),
|
|
|
|
|
])
|
|
|
|
|
->withToken($token)
|
|
|
|
|
->timeout(10)
|
|
|
|
|
->get("/api/v1/files/{$id}/presigned-url");
|
|
|
|
|
|
|
|
|
|
if (! $response->successful()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response->json('data.url');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (! $url) {
|
|
|
|
|
Cache::forget($cacheKey);
|
2026-03-19 20:05:38 +09:00
|
|
|
abort(404);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 00:36:52 +09:00
|
|
|
return redirect($url);
|
2026-03-19 20:05:38 +09:00
|
|
|
}
|
|
|
|
|
}
|