fix: [bending] Docker 환경 API 호출 시 internal_url 미사용 500 에러 수정
BendingBaseController, BendingProductController, FileViewController, DocumentTemplateController에서 API_INTERNAL_URL 미적용으로 Docker 내부 api.sam.kr 연결 실패하던 문제 수정
This commit is contained in:
57
app/Http/Controllers/FileViewController.php
Normal file
57
app/Http/Controllers/FileViewController.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
/**
|
||||
* R2 파일 Presigned URL 리다이렉트
|
||||
*
|
||||
* API에서 R2 presigned URL을 발급받아 브라우저를 직접 R2로 리다이렉트.
|
||||
* presigned URL은 5분간 캐시하여 동일 이미지 반복 요청 시 API 호출 최소화.
|
||||
*/
|
||||
class FileViewController extends Controller
|
||||
{
|
||||
public function show(int $id)
|
||||
{
|
||||
$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');
|
||||
$internalUrl = config('services.api.internal_url');
|
||||
$apiKey = config('services.api.key');
|
||||
$token = session('api_access_token', '');
|
||||
|
||||
$headers = [
|
||||
'X-API-KEY' => $apiKey,
|
||||
'X-TENANT-ID' => session('selected_tenant_id', 1),
|
||||
];
|
||||
|
||||
if ($internalUrl) {
|
||||
$headers['Host'] = parse_url($baseUrl, PHP_URL_HOST) ?: 'api.sam.kr';
|
||||
$baseUrl = $internalUrl;
|
||||
}
|
||||
|
||||
$response = Http::baseUrl($baseUrl)
|
||||
->withoutVerifying()
|
||||
->withHeaders($headers)
|
||||
->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);
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return redirect($url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user