feat: [document] 문서양식/문서 이미지를 R2 presigned URL로 전환

- 업로드 응답에 image_url 추가
- prepareTemplateData에서 file_id/image_path 기반 presigned URL 발급
- edit/preview에서 image_url 우선 사용
- show/print에서 api.sam.kr 하드코딩 제거
This commit is contained in:
2026-03-20 11:24:26 +09:00
parent 3995355617
commit 3e35ed1145
6 changed files with 74 additions and 19 deletions

View File

@@ -590,10 +590,20 @@ public function uploadImage(Request $request): JsonResponse
$filePath = $response->json('data.file_path');
$fileId = $response->json('data.id');
// presigned URL 발급
$imageUrl = null;
if ($fileId) {
$presignedResp = Http::withToken($tokenResult['data']['access_token'])
->withHeaders(['X-API-KEY' => $apiKey, 'X-TENANT-ID' => $tenantId])
->get("{$apiBaseUrl}/api/v1/files/{$fileId}/presigned-url");
$imageUrl = $presignedResp->json('data.url');
}
return response()->json([
'success' => true,
'path' => $filePath,
'file_id' => $fileId,
'image_url' => $imageUrl,
]);
}

View File

@@ -130,6 +130,47 @@ public function blockEdit(int $id): View
/**
* 현재 선택된 테넌트 조회
*/
/**
* API에서 presigned URL 발급
*/
private function getPresignedUrlFromApi(int $fileId): ?string
{
$baseUrl = config('services.api.base_url', 'https://api.sam.kr');
$apiKey = config('services.api.key');
$token = session('api_access_token', '');
$response = \Illuminate\Support\Facades\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/{$fileId}/presigned-url");
return $response->successful() ? $response->json('data.url') : null;
}
private function getPresignedUrlByPath(string $path): ?string
{
$baseUrl = config('services.api.base_url', 'https://api.sam.kr');
$apiKey = config('services.api.key');
$token = session('api_access_token', '');
$response = \Illuminate\Support\Facades\Http::baseUrl($baseUrl)
->withoutVerifying()
->withHeaders([
'X-API-KEY' => $apiKey,
'X-TENANT-ID' => session('selected_tenant_id', 1),
])
->withToken($token)
->timeout(10)
->post('/api/v1/files/presigned-url-by-path', ['path' => $path]);
return $response->successful() ? $response->json('data.url') : null;
}
private function getCurrentTenant(): ?Tenant
{
$tenantId = session('selected_tenant_id');
@@ -244,10 +285,19 @@ private function prepareTemplateData(DocumentTemplate $template): array
];
})->toArray(),
'sections' => $template->sections->map(function ($s) {
$imageUrl = null;
if ($s->file_id) {
$imageUrl = $this->getPresignedUrlFromApi($s->file_id);
} elseif ($s->image_path) {
$imageUrl = $this->getPresignedUrlByPath($s->image_path);
}
return [
'id' => $s->id,
'title' => $s->title,
'image_path' => $s->image_path,
'file_id' => $s->file_id,
'image_url' => $imageUrl,
'items' => $s->items->map(function ($i) {
$fv = $i->field_values ?? [];