summaryrefslogtreecommitdiff
path: root/lib/project-gtc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/project-gtc')
-rw-r--r--lib/project-gtc/service.ts67
-rw-r--r--lib/project-gtc/table/project-gtc-table-columns.tsx50
2 files changed, 17 insertions, 100 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("파일 시스템에서 파일 삭제에 실패했습니다.");
diff --git a/lib/project-gtc/table/project-gtc-table-columns.tsx b/lib/project-gtc/table/project-gtc-table-columns.tsx
index dfdf1921..141d5737 100644
--- a/lib/project-gtc/table/project-gtc-table-columns.tsx
+++ b/lib/project-gtc/table/project-gtc-table-columns.tsx
@@ -21,48 +21,13 @@ import {
import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"
import { ProjectGtcView } from "@/db/schema"
+import { FileActionsDropdown } from "@/components/ui/file-actions"
interface GetColumnsProps {
setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<ProjectGtcView> | null>>
}
/**
- * 파일 다운로드 함수
- */
-const handleFileDownload = async (projectId: number, fileName: string) => {
- try {
- // API를 통해 파일 다운로드
- const response = await fetch(`/api/project-gtc?action=download&projectId=${projectId}`, {
- method: 'GET',
- });
-
- if (!response.ok) {
- throw new Error('파일 다운로드에 실패했습니다.');
- }
-
- // 파일 blob 생성
- const blob = await response.blob();
-
- // 다운로드 링크 생성
- const url = window.URL.createObjectURL(blob);
- const link = document.createElement('a');
- link.href = url;
- link.download = fileName;
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
-
- // 메모리 정리
- window.URL.revokeObjectURL(url);
-
- toast.success("파일 다운로드를 시작합니다.");
- } catch (error) {
- console.error("파일 다운로드 오류:", error);
- toast.error("파일 다운로드 중 오류가 발생했습니다.");
- }
-};
-
-/**
* tanstack table 컬럼 정의 (중첩 헤더 버전)
*/
export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<ProjectGtcView>[] {
@@ -108,17 +73,14 @@ export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<Project
return null;
}
+
return (
- <Button
+ <FileActionsDropdown
+ filePath={project.filePath}
+ fileName={project.originalFileName}
variant="ghost"
size="icon"
- onClick={() => handleFileDownload(project.id, project.originalFileName!)}
- title={`${project.originalFileName} 다운로드`}
- className="hover:bg-muted"
- >
- <Paperclip className="h-4 w-4" />
- <span className="sr-only">다운로드</span>
- </Button>
+ />
);
},
maxSize: 30,