diff --git a/app/Services/PreviewLaporanService.php b/app/Services/PreviewLaporanService.php index 6a2f525..9a5b795 100644 --- a/app/Services/PreviewLaporanService.php +++ b/app/Services/PreviewLaporanService.php @@ -205,16 +205,47 @@ class PreviewLaporanService $statusLpj = 1; if($tipeLaporan->status === 'memo' && $permohonan->mig_permohonan){ - $path = $formFoto['upload_foto'][0]['path'] ?? null; - if (!$path) { - return response()->json(['error' => 'File path not found'], 404); + $paths = $formFoto['upload_foto'] ?? null; + + if (!is_array($paths) || empty($paths)) { + return response()->json(['error' => 'No files to download'], 404); } - $fullPath = storage_path('app/public/' . $path); - if (!file_exists($fullPath)) { - return response()->json(['error' => 'File not found on disk'], 404); + + $files = []; + foreach ($paths as $path) { + if (!$path['path']) { + continue; + } + $fullPath = storage_path('app/public/' . $path['path']); + if (!file_exists($fullPath)) { + continue; + } + $files[] = $fullPath; } - //surveyor/2025/SEPTEMBER/02092025/252485/252485_148913105540_1.pdf - return response()->download($fullPath); + + if (empty($files)) { + return response()->json(['error' => 'No valid files found'], 404); + } + + // For single file, download directly + if (count($files) === 1) { + return response()->download($files[0]); + } + + // For multiple files, create zip and download + $zipName = 'photos_' . time() . '.zip'; + $zipPath = storage_path('app/public/' . $zipName); + + $zip = new \ZipArchive(); + if ($zip->open($zipPath, \ZipArchive::CREATE) === true) { + foreach ($files as $file) { + $zip->addFile($file, basename($file)); + } + $zip->close(); + return response()->download($zipPath)->deleteFileAfterSend(true); + } + + return response()->json(['error' => 'Failed to create zip file'], 500); } try { $pdf = $this->generatePDF($viewLaporan, compact(