diff --git a/app/Http/Controllers/Api/Admin/DocumentTemplateApiController.php b/app/Http/Controllers/Api/Admin/DocumentTemplateApiController.php index f49a2c3c..cd257a14 100644 --- a/app/Http/Controllers/Api/Admin/DocumentTemplateApiController.php +++ b/app/Http/Controllers/Api/Admin/DocumentTemplateApiController.php @@ -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, ]); } diff --git a/app/Http/Controllers/DocumentTemplateController.php b/app/Http/Controllers/DocumentTemplateController.php index 44688194..d38fcb24 100644 --- a/app/Http/Controllers/DocumentTemplateController.php +++ b/app/Http/Controllers/DocumentTemplateController.php @@ -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 ?? []; diff --git a/resources/views/document-templates/edit.blade.php b/resources/views/document-templates/edit.blade.php index 6e3f0533..2b456bf0 100644 --- a/resources/views/document-templates/edit.blade.php +++ b/resources/views/document-templates/edit.blade.php @@ -1440,6 +1440,7 @@ function uploadSectionImage(sectionId, input) { if (section) { section.image_path = result.path; section.file_id = result.file_id; + section.image_url = result.image_url || null; renderSections(); showToast('이미지가 업로드되었습니다.', 'success'); } @@ -2173,15 +2174,11 @@ class="w-full px-2 py-1 border border-gray-200 rounded text-xs">`; // ===== 유틸리티 ===== function getSectionImageUrl(section) { - // file_id 기반 (R2 프록시) - if (section.file_id) { - return '{{ rtrim(config("app.api_url", "http://api.sam.kr"), "/") }}/files/' + section.file_id + '/view'; - } - // fallback: image_path 직접 사용 (레거시) - const imagePath = section.image_path; - if (!imagePath) return ''; - if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) return imagePath; - return '{{ rtrim(config("app.api_url", "http://api.sam.kr"), "/") }}/storage/tenants/' + imagePath; + if (section.image_url) return section.image_url; + if (section.file_id) return '/files/' + section.file_id + '/view'; + if (!section.image_path) return ''; + if (section.image_path.startsWith('http')) return section.image_path; + return ''; } function escapeHtml(text) { diff --git a/resources/views/document-templates/partials/preview-modal.blade.php b/resources/views/document-templates/partials/preview-modal.blade.php index b128aeb0..fb3f186e 100644 --- a/resources/views/document-templates/partials/preview-modal.blade.php +++ b/resources/views/document-templates/partials/preview-modal.blade.php @@ -37,13 +37,11 @@ function closePreviewModal() { // 이미지 URL 헬퍼 (file_id 기반 R2 프록시) function _previewImageUrl(section) { - if (section.file_id) { - return '{{ rtrim(config("app.api_url", "http://api.sam.kr"), "/") }}/files/' + section.file_id + '/view'; - } - const imagePath = section.image_path; - if (!imagePath) return ''; - if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) return imagePath; - return '{{ rtrim(config("app.api_url", "http://api.sam.kr"), "/") }}/storage/tenants/' + imagePath; + if (section.image_url) return section.image_url; + if (section.file_id) return '/files/' + section.file_id + '/view'; + if (!section.image_path) return ''; + if (section.image_path.startsWith('http')) return section.image_path; + return ''; } // HTML 이스케이프 diff --git a/resources/views/documents/print.blade.php b/resources/views/documents/print.blade.php index 0200420c..18971477 100644 --- a/resources/views/documents/print.blade.php +++ b/resources/views/documents/print.blade.php @@ -69,7 +69,7 @@ class="bg-gray-200 hover:bg-gray-300 text-gray-700 px-4 py-2 rounded-lg transiti @if($section->file_id || $section->image_path) @php $sectionImgUrl = $section->file_id - ? rtrim(config('app.api_url', 'http://api.sam.kr'), '/') . '/files/' . $section->file_id . '/view' + ? route('files.view', $section->file_id) : asset('storage/' . $section->image_path); @endphp
diff --git a/resources/views/documents/show.blade.php b/resources/views/documents/show.blade.php index d2a484dc..5205b192 100644 --- a/resources/views/documents/show.blade.php +++ b/resources/views/documents/show.blade.php @@ -430,7 +430,7 @@ class="px-2 py-2 text-center text-xs font-medium text-gray-600 border border-gra

{{ $section->title }}

@php $sectionImgUrl = $section->file_id - ? rtrim(config('app.api_url', 'http://api.sam.kr'), '/') . '/files/' . $section->file_id . '/view' + ? route('files.view', $section->file_id) : asset('storage/' . $section->image_path); @endphp {{ $section->title }} @@ -449,7 +449,7 @@ class="px-2 py-2 text-center text-xs font-medium text-gray-600 border border-gra @if($section->file_id || $section->image_path) @php $sectionImgUrl = $section->file_id - ? rtrim(config('app.api_url', 'http://api.sam.kr'), '/') . '/files/' . $section->file_id . '/view' + ? route('files.view', $section->file_id) : asset('storage/' . $section->image_path); @endphp {{ $section->title }}