diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-07-07 01:44:45 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-07-07 01:44:45 +0000 |
| commit | 90f79a7a691943a496f67f01c1e493256070e4de (patch) | |
| tree | 37275fde3ae08c2bca384fbbc8eb378de7e39230 /lib/project-gtc/service.ts | |
| parent | fbb3b7f05737f9571b04b0a8f4f15c0928de8545 (diff) | |
(대표님) 변경사항 20250707 10시 43분 - unstaged 변경사항 추가
Diffstat (limited to 'lib/project-gtc/service.ts')
| -rw-r--r-- | lib/project-gtc/service.ts | 67 |
1 files changed, 11 insertions, 56 deletions
diff --git a/lib/project-gtc/service.ts b/lib/project-gtc/service.ts index c65d9364..7ae09635 100644 --- a/lib/project-gtc/service.ts +++ b/lib/project-gtc/service.ts @@ -14,6 +14,7 @@ import { promises as fs } from "fs"; import path from "path"; import crypto from "crypto"; import { revalidatePath } from 'next/cache'; +import { deleteFile, saveFile } from "../file-stroage"; // Project GTC 목록 조회 export async function getProjectGtcList( @@ -119,52 +120,11 @@ export async function uploadProjectGtcFile( return { success: false, error: "파일은 필수입니다." }; } - // 허용된 파일 타입 검사 - const allowedTypes = [ - 'application/pdf', - 'application/msword', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - 'text/plain' - ]; - - if (!allowedTypes.includes(file.type)) { - return { success: false, error: "PDF, Word, 또는 텍스트 파일만 업로드 가능합니다." }; - } - - // 원본 파일 이름과 확장자 분리 - const originalFileName = file.name; - const fileExtension = path.extname(originalFileName); - const fileNameWithoutExt = path.basename(originalFileName, fileExtension); - - // 해시된 파일 이름 생성 - const timestamp = Date.now(); - const randomHash = crypto.createHash('md5') - .update(`${fileNameWithoutExt}-${timestamp}-${Math.random()}`) - .digest('hex') - .substring(0, 8); - - const hashedFileName = `${timestamp}-${randomHash}${fileExtension}`; - - // 저장 디렉토리 설정 - const uploadDir = path.join(process.cwd(), "public", "project-gtc"); - - // 디렉토리가 없으면 생성 - try { - await fs.mkdir(uploadDir, { recursive: true }); - } catch (err) { - console.log("Directory already exists or creation failed:", err); + const saveResult = await saveFile(file, 'proejctGTC'); + if (!saveResult.success) { + return { success: false, error: saveResult.error }; } - // 파일 경로 설정 - const filePath = path.join(uploadDir, hashedFileName); - const publicFilePath = `/project-gtc/${hashedFileName}`; - - // 파일을 ArrayBuffer로 변환 - const arrayBuffer = await file.arrayBuffer(); - const buffer = Buffer.from(arrayBuffer); - - // 파일 저장 - await fs.writeFile(filePath, buffer); // 기존 파일이 있으면 삭제 const existingFile = await db.query.projectGtcFiles.findFirst({ @@ -172,14 +132,9 @@ export async function uploadProjectGtcFile( }); if (existingFile) { - // 기존 파일 삭제 - try { - const filePath = path.join(process.cwd(), "public", existingFile.filePath); - await fs.unlink(filePath); - } catch { - console.error("파일 삭제 실패"); - } + const deleted = await deleteFile(existingFile.filePath); + // DB에서 기존 파일 정보 삭제 await db.delete(projectGtcFiles) .where(eq(projectGtcFiles.id, existingFile.id)); @@ -188,9 +143,9 @@ export async function uploadProjectGtcFile( // DB에 새 파일 정보 저장 const newFile = await db.insert(projectGtcFiles).values({ projectId, - fileName: hashedFileName, - filePath: publicFilePath, - originalFileName, + fileName: saveResult.fileName!, + filePath: saveResult.publicPath!, + originalFileName:file.name, fileSize: file.size, mimeType: file.type, }).returning(); @@ -225,8 +180,8 @@ export async function deleteProjectGtcFile( // 파일 시스템에서 파일 삭제 try { - const filePath = path.join(process.cwd(), "public", existingFile.filePath); - await fs.unlink(filePath); + + const deleted = await deleteFile(existingFile.filePath); } catch (error) { console.error("파일 시스템에서 파일 삭제 실패:", error); throw new Error("파일 시스템에서 파일 삭제에 실패했습니다."); |
