summaryrefslogtreecommitdiff
path: root/lib/general-contract-template/service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/general-contract-template/service.ts')
-rw-r--r--lib/general-contract-template/service.ts97
1 files changed, 51 insertions, 46 deletions
diff --git a/lib/general-contract-template/service.ts b/lib/general-contract-template/service.ts
index 9b3eda68..37af83ce 100644
--- a/lib/general-contract-template/service.ts
+++ b/lib/general-contract-template/service.ts
@@ -9,7 +9,8 @@ import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { GeneralContractTemplate, generalContractTemplates, users } from "@/db/schema";
-import { deleteFile, saveFile } from "@/lib/file-stroage";
+import { deleteFile, saveFile, saveDRMFile } from "@/lib/file-stroage";
+import { decryptWithServerAction } from "@/components/drm/drmUtils";
import {
selectContractTemplates,
selectContractTemplatesWithUsers,
@@ -223,22 +224,47 @@ export async function saveTemplateFile(templateId: number, formData: FormData) {
return { error: "파일 경로가 없습니다." };
}
- // 파일 저장 로직 (실제 파일 시스템에 저장)
- const { writeFile, mkdir } = await import("fs/promises");
- const { join } = await import("path");
+ // 파일 저장 로직 (DRM 해제 로직 적용)
+ const saveResult = await saveDRMFile(
+ file,
+ decryptWithServerAction,
+ 'general-contract-templates'
+ );
- const bytes = await file.arrayBuffer();
- const buffer = Buffer.from(bytes);
+ if (!saveResult.success) {
+ return { error: saveResult.error || '파일 저장에 실패했습니다.' };
+ }
- // 기존 파일 경로 사용 (덮어쓰기) - general-contract-templates 경로로 통일
- const uploadPath = join(process.cwd(), "public", template.filePath.replace(/^\//, ""));
-
- // 디렉토리 확인 및 생성
- const dirPath = uploadPath.substring(0, uploadPath.lastIndexOf('/'));
- await mkdir(dirPath, { recursive: true });
+ // 기존 파일 경로와 다른 경우에만 파일 경로 업데이트
+ if (template.filePath !== saveResult.publicPath) {
+ // 기존 파일 삭제
+ if (template.filePath) {
+ const deleted = await deleteFile(template.filePath);
+ if (deleted) {
+ console.log(`✅ 기존 파일 삭제됨: ${template.filePath}`);
+ } else {
+ console.log(`⚠️ 기존 파일 삭제 실패: ${template.filePath}`);
+ }
+ }
- // 파일 저장
- await writeFile(uploadPath, buffer);
+ // DB에 새 파일 경로 업데이트
+ await db
+ .update(generalContractTemplates)
+ .set({
+ filePath: saveResult.publicPath,
+ fileName: file.name,
+ updatedAt: new Date(),
+ })
+ .where(eq(generalContractTemplates.id, templateId));
+ } else {
+ // 같은 경로인 경우 수정일시만 업데이트
+ await db
+ .update(generalContractTemplates)
+ .set({
+ updatedAt: new Date(),
+ })
+ .where(eq(generalContractTemplates.id, templateId));
+ }
// 캐시 무효화 (목록/상세 모두 고려)
revalidatePath(`/evcp/general-contract-template/${templateId}`);
@@ -424,40 +450,19 @@ export async function updateTemplate({
let filePath: string | undefined = undefined;
if (file) {
- // 1) 새 파일 저장 (원본 파일명 유지 + 충돌 시 접미사)
- const { mkdir, writeFile, access } = await import('fs/promises');
- const { join, extname, basename } = await import('path');
-
- const ext = extname(file.name);
- const base = basename(file.name, ext)
- .replace(/[<>:"'|?*\\\/]/g, '_')
- .replace(/[\x00-\x1f]/g, '')
- .replace(/\s+/g, ' ')
- .trim()
- .substring(0, 200);
-
- const dirAbs = join(process.cwd(), 'public', 'general-contract-templates');
- await mkdir(dirAbs, { recursive: true });
-
- let candidate = `${base}${ext}`;
- let absPath = join(dirAbs, candidate);
- let counter = 1;
- while (true) {
- try {
- await access(absPath);
- candidate = `${base} (${counter})${ext}`;
- absPath = join(dirAbs, candidate);
- counter += 1;
- } catch {
- break;
- }
+ // 1) 새 파일 저장 (DRM 해제 로직 적용)
+ const saveResult = await saveDRMFile(
+ file,
+ decryptWithServerAction,
+ 'general-contract-templates'
+ );
+
+ if (!saveResult.success) {
+ return { error: saveResult.error || '파일 저장에 실패했습니다.' };
}
- const bytes = await file.arrayBuffer();
- await writeFile(absPath, Buffer.from(bytes));
-
- fileName = candidate;
- filePath = `/general-contract-templates/${candidate}`;
+ fileName = file.name;
+ filePath = saveResult.publicPath;
// 2) 기존 파일 삭제
if (prev.filePath) {