diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/[lng]/partners/(partners)/evaluation/page.tsx | 2 | ||||
| -rw-r--r-- | app/api/upload/basicContract/chunk/route.ts | 117 |
2 files changed, 79 insertions, 40 deletions
diff --git a/app/[lng]/partners/(partners)/evaluation/page.tsx b/app/[lng]/partners/(partners)/evaluation/page.tsx index cbb4696b..be88ef3e 100644 --- a/app/[lng]/partners/(partners)/evaluation/page.tsx +++ b/app/[lng]/partners/(partners)/evaluation/page.tsx @@ -68,8 +68,6 @@ export default async function IndexPage(props: IndexPageProps) { // Validate vendorId (should be a number) const idAsNumber = Number(vendorId) - console.log(idAsNumber) - if (isNaN(idAsNumber)) { // Handle invalid vendor ID (this shouldn't happen if authentication is working properly) return ( diff --git a/app/api/upload/basicContract/chunk/route.ts b/app/api/upload/basicContract/chunk/route.ts index 7100988b..e190fca4 100644 --- a/app/api/upload/basicContract/chunk/route.ts +++ b/app/api/upload/basicContract/chunk/route.ts @@ -1,7 +1,7 @@ import { NextRequest, NextResponse } from 'next/server'; -import { mkdir, writeFile, appendFile } from 'fs/promises'; +import { mkdir, writeFile, appendFile, readFile, rm } from 'fs/promises'; import path from 'path'; -import crypto from 'crypto'; +import { generateHashedFileName, saveBuffer } from '@/lib/file-stroage'; export async function POST(request: NextRequest) { try { @@ -14,58 +14,99 @@ export async function POST(request: NextRequest) { const fileId = formData.get('fileId') as string; if (!chunk || !filename || isNaN(chunkIndex) || isNaN(totalChunks) || !fileId) { - return NextResponse.json({ success: false, error: '필수 매개변수가 누락되었습니다' }, { status: 400 }); + return NextResponse.json({ + success: false, + error: '필수 매개변수가 누락되었습니다' + }, { status: 400 }); } - + // 임시 디렉토리 생성 const tempDir = path.join(process.cwd(), 'temp', fileId); await mkdir(tempDir, { recursive: true }); - + // 청크 파일 저장 const chunkPath = path.join(tempDir, `chunk-${chunkIndex}`); const buffer = Buffer.from(await chunk.arrayBuffer()); await writeFile(chunkPath, buffer); - + + console.log(`📦 청크 저장 완료: ${chunkIndex + 1}/${totalChunks} (${buffer.length} bytes)`); + // 마지막 청크인 경우 모든 청크를 합쳐 최종 파일 생성 if (chunkIndex === totalChunks - 1) { - const uploadDir = path.join(process.cwd(), "public", "basicContract", "template"); - await mkdir(uploadDir, { recursive: true }); + console.log(`🔄 파일 병합 시작: ${filename}`); - // 파일명 생성 - const timestamp = Date.now(); - const randomHash = crypto.createHash('md5') - .update(`${filename}-${timestamp}`) - .digest('hex') - .substring(0, 8); - const hashedFileName = `${timestamp}-${randomHash}${path.extname(filename)}`; - const finalPath = path.join(uploadDir, hashedFileName); - - // 모든 청크 병합 - await writeFile(finalPath, Buffer.alloc(0)); // 빈 파일 생성 - for (let i = 0; i < totalChunks; i++) { - const chunkData = await require('fs/promises').readFile(path.join(tempDir, `chunk-${i}`)); - await appendFile(finalPath, chunkData); + try { + // 모든 청크를 순서대로 읽어서 병합 + const chunks: Buffer[] = []; + let totalSize = 0; + + for (let i = 0; i < totalChunks; i++) { + const chunkData = await readFile(path.join(tempDir, `chunk-${i}`)); + chunks.push(chunkData); + totalSize += chunkData.length; + } + + // 모든 청크를 하나의 Buffer로 병합 + const mergedBuffer = Buffer.concat(chunks, totalSize); + + console.log(`📄 병합 완료: ${filename} (총 ${totalSize} bytes)`); + + // 공용 함수를 사용하여 파일 저장 + const saveResult = await saveBuffer({ + buffer: mergedBuffer, + fileName: filename, + directory: 'basicContract/template', + originalName: filename + }); + + // 임시 파일 정리 (비동기로 처리) + rm(tempDir, { recursive: true, force: true }) + .then(() => console.log(`🗑️ 임시 파일 정리 완료: ${fileId}`)) + .catch((e: unknown) => console.error('청크 정리 오류:', e)); + + if (saveResult.success) { + console.log(`✅ 최종 파일 저장 완료: ${saveResult.fileName}`); + + return NextResponse.json({ + success: true, + fileName: filename, + filePath: saveResult.publicPath, + hashedFileName: saveResult.fileName, + fileSize: totalSize + }); + } else { + console.error('파일 저장 실패:', saveResult.error); + return NextResponse.json({ + success: false, + error: saveResult.error || '파일 저장에 실패했습니다' + }, { status: 500 }); + } + + } catch (mergeError) { + console.error('파일 병합 오류:', mergeError); + + // 오류 발생 시 임시 파일 정리 + rm(tempDir, { recursive: true, force: true }) + .catch((e: unknown) => console.error('임시 파일 정리 오류:', e)); + + return NextResponse.json({ + success: false, + error: '파일 병합 중 오류가 발생했습니다' + }, { status: 500 }); } - - // 임시 파일 정리 (비동기로 처리) - require('fs/promises').rm(tempDir, { recursive: true, force: true }) - .catch((e: unknown) => console.error('청크 정리 오류:', e)); - - return NextResponse.json({ - success: true, - fileName: filename, - filePath: `/basicContract/template/${hashedFileName}` - }); } - - return NextResponse.json({ - success: true, - chunkIndex, - message: `청크 ${chunkIndex + 1}/${totalChunks} 업로드 완료` + + return NextResponse.json({ + success: true, + chunkIndex, + message: `청크 ${chunkIndex + 1}/${totalChunks} 업로드 완료` }); } catch (error) { console.error('청크 업로드 오류:', error); - return NextResponse.json({ success: false, error: '서버 오류' }, { status: 500 }); + return NextResponse.json({ + success: false, + error: '서버 오류' + }, { status: 500 }); } }
\ No newline at end of file |
