diff options
| author | joonhoekim <26rote@gmail.com> | 2025-05-29 05:12:36 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-05-29 05:37:04 +0000 |
| commit | e484964b1d78cedabbe182c789a8e4c9b53e29d3 (patch) | |
| tree | d18133dde99e6feb773c95d04f7e79715ab24252 /app/api/tech-sales-rfq-download/route.ts | |
| parent | 37f55540833c2d5894513eca9fc8f7c6233fc2d2 (diff) | |
(김준회) 기술영업 조선 RFQ 파일첨부 및 채팅 기능 구현 / menuConfig 수정 (벤더 기술영업)
Diffstat (limited to 'app/api/tech-sales-rfq-download/route.ts')
| -rw-r--r-- | app/api/tech-sales-rfq-download/route.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/app/api/tech-sales-rfq-download/route.ts b/app/api/tech-sales-rfq-download/route.ts new file mode 100644 index 00000000..b9dd14d1 --- /dev/null +++ b/app/api/tech-sales-rfq-download/route.ts @@ -0,0 +1,85 @@ +import { NextRequest } from "next/server" +import { join } from "path" +import { readFile } from "fs/promises" + +export async function GET(request: NextRequest) { + try { + const searchParams = request.nextUrl.searchParams + const filePath = searchParams.get("path") + + if (!filePath) { + return new Response("File path is required", { status: 400 }) + } + + // 보안: 경로 조작 방지 + if (filePath.includes("..") || !filePath.startsWith("/techsales-rfq/")) { + return new Response("Invalid file path", { status: 400 }) + } + + // 파일 경로 구성 (public 폴더 기준) + const fullPath = join(process.cwd(), "public", filePath) + + try { + // 파일 읽기 + const fileBuffer = await readFile(fullPath) + + // 파일명 추출 + const fileName = filePath.split("/").pop() || "download" + + // MIME 타입 결정 + const ext = fileName.split(".").pop()?.toLowerCase() + let contentType = "application/octet-stream" + + switch (ext) { + case "pdf": + contentType = "application/pdf" + break + case "doc": + contentType = "application/msword" + break + case "docx": + contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" + break + case "xls": + contentType = "application/vnd.ms-excel" + break + case "xlsx": + contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" + break + case "jpg": + case "jpeg": + contentType = "image/jpeg" + break + case "png": + contentType = "image/png" + break + case "gif": + contentType = "image/gif" + break + case "txt": + contentType = "text/plain" + break + case "zip": + contentType = "application/zip" + break + default: + contentType = "application/octet-stream" + } + + // 응답 헤더 설정 + const headers = new Headers({ + "Content-Type": contentType, + "Content-Disposition": `attachment; filename="${encodeURIComponent(fileName)}"`, + "Content-Length": fileBuffer.length.toString(), + }) + + return new Response(fileBuffer, { headers }) + } catch (fileError) { + console.error("File read error:", fileError) + return new Response("File not found", { status: 404 }) + } + } catch (error) { + console.error("Download error:", error) + return new Response("Internal server error", { status: 500 }) + } +}
\ No newline at end of file |
