diff --git a/app/Http/Resources/Api/V1/BendingItemResource.php b/app/Http/Resources/Api/V1/BendingItemResource.php index 14ad47d4..ef5ee98a 100644 --- a/app/Http/Resources/Api/V1/BendingItemResource.php +++ b/app/Http/Resources/Api/V1/BendingItemResource.php @@ -40,6 +40,7 @@ public function toArray(Request $request): array 'registration_date' => $this->getOption('registration_date'), // 이미지 'image_file_id' => $this->getImageFileId(), + 'image_url' => $this->getImageUrl(), // 추적 'legacy_bending_id' => $this->legacy_bending_id, 'legacy_bending_num' => $this->legacy_bending_id, // MNG2 호환 @@ -60,13 +61,21 @@ public function toArray(Request $request): array ]; } - private function getImageFileId(): ?int + private function getImageFile(): ?\App\Models\Commons\File { - $file = $this->files() + return $this->files() ->where('field_key', 'bending_diagram') ->orderByDesc('id') ->first(); + } - return $file?->id; + private function getImageFileId(): ?int + { + return $this->getImageFile()?->id; + } + + private function getImageUrl(): ?string + { + return $this->getImageFile()?->presignedUrl(); } } diff --git a/app/Http/Resources/Api/V1/GuiderailModelResource.php b/app/Http/Resources/Api/V1/GuiderailModelResource.php index d30f404f..ba94ab04 100644 --- a/app/Http/Resources/Api/V1/GuiderailModelResource.php +++ b/app/Http/Resources/Api/V1/GuiderailModelResource.php @@ -48,8 +48,9 @@ public function toArray(Request $request): array 'modified_by' => $this->getOption('modified_by'), // 이미지 'image_file_id' => $this->getImageFileId(), + 'image_url' => $this->getImageUrl(), // 부품 조합 - 'components' => $components, + 'components' => $this->enrichComponentsWithImageUrls($components), 'material_summary' => $materialSummary, 'component_count' => count($components), // 메타 @@ -58,7 +59,7 @@ public function toArray(Request $request): array ]; } - private function getImageFileId(): ?int + private function getImageFile(): ?\App\Models\Commons\File { $file = \App\Models\Commons\File::where('document_id', $this->id) ->where('document_type', 'bending_model') @@ -74,7 +75,40 @@ private function getImageFileId(): ?int ->first(); } - return $file?->id; + return $file; + } + + private function getImageFileId(): ?int + { + return $this->getImageFile()?->id; + } + + private function getImageUrl(): ?string + { + return $this->getImageFile()?->presignedUrl(); + } + + private function enrichComponentsWithImageUrls(array $components): array + { + $fileIds = array_filter(array_column($components, 'image_file_id')); + if (empty($fileIds)) { + return $components; + } + + $files = \App\Models\Commons\File::whereIn('id', $fileIds) + ->whereNull('deleted_at') + ->get() + ->keyBy('id'); + + foreach ($components as &$comp) { + $fileId = $comp['image_file_id'] ?? null; + $comp['image_url'] = $fileId && isset($files[$fileId]) + ? $files[$fileId]->presignedUrl() + : null; + } + unset($comp); + + return $components; } private function calcMaterialSummary(array $components): array diff --git a/app/Models/Commons/File.php b/app/Models/Commons/File.php index 9169bc6d..bd5137e5 100644 --- a/app/Models/Commons/File.php +++ b/app/Models/Commons/File.php @@ -98,6 +98,21 @@ public function fileable() return $this->morphTo(); } + /** + * R2 Presigned URL 생성 (30분 유효) + */ + public function presignedUrl(int $minutes = 30): ?string + { + if (! $this->file_path) { + return null; + } + + return Storage::disk('r2')->temporaryUrl( + $this->file_path, + now()->addMinutes($minutes) + ); + } + /** * Get the full storage path */