import { NextRequest, NextResponse } from 'next/server'; import { mkdir, writeFile, appendFile, readFile, rm } from 'fs/promises'; import path from 'path'; import { generateHashedFileName, saveBuffer, saveDRMFile } from '@/lib/file-stroage'; import { decryptWithServerAction } from '@/components/drm/drmUtils'; export async function POST(request: NextRequest) { try { const formData = await request.formData(); const chunk = formData.get('chunk') as File; const filename = formData.get('filename') as string; const chunkIndex = parseInt(formData.get('chunkIndex') as string); const totalChunks = parseInt(formData.get('totalChunks') as string); const fileId = formData.get('fileId') as string; if (!chunk || !filename || isNaN(chunkIndex) || isNaN(totalChunks) || !fileId) { 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) { console.log(`🔄 파일 병합 시작: ${filename}`); 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 isProduction = process.env.NODE_ENV === 'production'; let saveResult; if (isProduction) { // Production: DRM 파일 처리 console.log(`🔐 Production 환경 - DRM 파일 처리: ${filename}`); const mergedFile = new File([mergedBuffer], filename, { type: chunk.type || 'application/octet-stream', lastModified: Date.now(), }); saveResult = await saveDRMFile( mergedFile, decryptWithServerAction, // 복호화 함수 'basicContract/template', // 저장 디렉토리 // userId // 선택사항 ); } else { // Development: 일반 파일 저장 console.log(`🛠️ Development 환경 - 일반 파일 저장: ${filename}`); 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) { const envPrefix = isProduction ? '🔐' : '🛠️'; console.log(`${envPrefix} 최종 파일 저장 완료: ${saveResult.fileName}`); return NextResponse.json({ success: true, fileName: filename, filePath: saveResult.publicPath, hashedFileName: saveResult.fileName, fileSize: totalSize, environment: isProduction ? 'production' : 'development', processingType: isProduction ? 'DRM' : 'standard' }); } else { const envPrefix = isProduction ? '🔐' : '🛠️'; console.error(`${envPrefix} 파일 저장 실패:`, saveResult.error); return NextResponse.json({ success: false, error: saveResult.error || '파일 저장에 실패했습니다', environment: isProduction ? 'production' : 'development' }, { 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 }); } } return NextResponse.json({ success: true, chunkIndex, message: `청크 ${chunkIndex + 1}/${totalChunks} 업로드 완료` }); } catch (error) { console.error('청크 업로드 오류:', error); return NextResponse.json({ success: false, error: '서버 오류' }, { status: 500 }); } }