diff options
Diffstat (limited to 'app/api/upload/basicContract/chunk/route.ts')
| -rw-r--r-- | app/api/upload/basicContract/chunk/route.ts | 156 |
1 files changed, 85 insertions, 71 deletions
diff --git a/app/api/upload/basicContract/chunk/route.ts b/app/api/upload/basicContract/chunk/route.ts index 383a8f36..db3d591e 100644 --- a/app/api/upload/basicContract/chunk/route.ts +++ b/app/api/upload/basicContract/chunk/route.ts @@ -33,83 +33,97 @@ export async function POST(request: NextRequest) { 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); - - const mergedFile = new File([mergedBuffer], filename, { - type: chunk.type || 'application/octet-stream', - lastModified: Date.now(), - }); - - const saveResult = await saveDRMFile( - mergedFile, - decryptWithServerAction, // 볡νΈν ν¨μ - 'basicContract/template', // μ μ₯ λλ ν 리 - // userId // μ ν - ); +// λ§μ§λ§ μ²ν¬μΈ κ²½μ° λͺ¨λ μ²ν¬λ₯Ό ν©μ³ μ΅μ’
νμΌ μμ± +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; - - console.log(`π λ³ν© μλ£: ${filename} (μ΄ ${totalSize} bytes)`); + if (isProduction) { + // Production: DRM νμΌ μ²λ¦¬ + console.log(`π Production νκ²½ - DRM νμΌ μ²λ¦¬: ${filename}`); + + const mergedFile = new File([mergedBuffer], filename, { + type: chunk.type || 'application/octet-stream', + lastModified: Date.now(), + }); - // κ³΅μ© ν¨μλ₯Ό μ¬μ©νμ¬ νμΌ μ μ₯ - // const saveResult = await saveBuffer({ - // buffer: mergedBuffer, - // fileName: filename, - // directory: 'basicContract/template', - // originalName: filename - // }); + 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)); + // μμ νμΌ μ 리 (λΉλκΈ°λ‘ μ²λ¦¬) + 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 }); - } + 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, |
