diff options
Diffstat (limited to 'app/api/vendors/attachments/download/route.ts')
| -rw-r--r-- | app/api/vendors/attachments/download/route.ts | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/app/api/vendors/attachments/download/route.ts b/app/api/vendors/attachments/download/route.ts index 0151a699..1c9e5ddf 100644 --- a/app/api/vendors/attachments/download/route.ts +++ b/app/api/vendors/attachments/download/route.ts @@ -31,9 +31,37 @@ export async function GET(request: NextRequest) { ); } - // 파일 경로 구성 - const basePath = process.env.UPLOAD_DIR || path.join(process.cwd(), 'public'); - const filePath = path.join(basePath, attachment.filePath); + // 환경에 따른 기본 경로 설정 (/api/files/ 와 동일한 로직) + const nasPath = process.env.NAS_PATH || "/evcp_nas"; + const basePath = process.env.NODE_ENV === 'production' + ? nasPath + : path.join(process.cwd(), 'public'); + + // DB에 저장된 경로가 /api/files/vendors/[id]/[filename] 형태인 경우 처리 + let normalizedPath: string; + + if (attachment.filePath.startsWith('/api/files/')) { + // /api/files/vendors/[id]/[filename] -> vendors/[id]/[filename] + normalizedPath = attachment.filePath.replace('/api/files/', ''); + } else if (attachment.filePath.startsWith('/')) { + // 기존 방식: /vendors/[id]/[filename] -> vendors/[id]/[filename] + normalizedPath = attachment.filePath.slice(1); + } else { + // 상대 경로 그대로 사용 + normalizedPath = attachment.filePath; + } + + // 보안 검증: 경로 탐색 공격 방지 + if (normalizedPath.includes('..') || normalizedPath.includes('~')) { + return NextResponse.json( + { error: "안전하지 않은 파일 경로입니다." }, + { status: 400 } + ); + } + + const filePath = path.join(basePath, normalizedPath); + + console.log(`파일 경로 확인: ${attachment.filePath} -> ${filePath}`); // 파일 존재 확인 try { |
