diff options
| author | joonhoekim <26rote@gmail.com> | 2025-09-09 03:19:41 +0000 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-09-09 03:19:41 +0000 |
| commit | e06913008f124ce8e7389fbdc1e57206ce9bbb2b (patch) | |
| tree | 010292c2cd6116065d70893055deba230899d4ca /app/api | |
| parent | 61fd1ff7162390f9ec2480da74840e58bb0b6ebd (diff) | |
(김준회) 협력업체관리 업체분류, 정규업체등록현황 부 수정, 파일다운로드 오류 수정, 업데이트 시트 수정
Diffstat (limited to 'app/api')
| -rw-r--r-- | app/api/vendors/attachments/download-all/route.ts | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/app/api/vendors/attachments/download-all/route.ts b/app/api/vendors/attachments/download-all/route.ts index 23f85786..4520cb2d 100644 --- a/app/api/vendors/attachments/download-all/route.ts +++ b/app/api/vendors/attachments/download-all/route.ts @@ -53,9 +53,19 @@ export async function GET(request: NextRequest) { // 파일 읽기 및 ZIP에 추가 await Promise.all( attachments.map(async (attachment) => { - const filePath = path.join(basePath, attachment.filePath); - try { + // 필수 필드 검증 + if (!attachment.filePath || !attachment.fileName) { + console.warn(`첨부파일 정보가 불완전합니다:`, attachment); + return; + } + + // filePath는 이미 /로 시작하므로 public 폴더에서의 상대 경로로 처리 + const normalizedPath = attachment.filePath.startsWith('/') + ? attachment.filePath.slice(1) // 앞의 '/' 제거 + : attachment.filePath; + const filePath = path.join(basePath, normalizedPath); + // 파일 존재 확인 try { await fs.promises.access(filePath, fs.constants.F_OK); @@ -67,15 +77,29 @@ export async function GET(request: NextRequest) { // 파일 읽기 const fileData = await fs.promises.readFile(filePath); - // ZIP에 파일 추가 - zip.file(attachment.fileName, fileData); + // ZIP에 파일 추가 (파일명 중복 방지) + const safeFileName = `${attachment.id}_${attachment.fileName}`; + zip.file(safeFileName, fileData); + + console.log(`ZIP에 파일 추가됨: ${safeFileName}`); } catch (error) { - console.warn(`파일을 처리할 수 없습니다: ${filePath}`, error); + console.warn(`파일을 처리할 수 없습니다: ${attachment.filePath}`, error); // 오류가 있더라도 계속 진행 } }) ); + // ZIP에 추가된 파일이 있는지 확인 + const zipFiles = Object.keys(zip.files); + if (zipFiles.length === 0) { + return NextResponse.json( + { error: '다운로드 가능한 파일이 없습니다.' }, + { status: 404 } + ); + } + + console.log(`ZIP 파일 생성 시작: ${zipFiles.length}개 파일`); + // ZIP 생성 const zipContent = await zip.generateAsync({ type: 'nodebuffer', |
