summaryrefslogtreecommitdiff
path: root/lib/techsales-rfq
diff options
context:
space:
mode:
Diffstat (limited to 'lib/techsales-rfq')
-rw-r--r--lib/techsales-rfq/service.ts56
1 files changed, 9 insertions, 47 deletions
diff --git a/lib/techsales-rfq/service.ts b/lib/techsales-rfq/service.ts
index d5cb8efe..14d7a45e 100644
--- a/lib/techsales-rfq/service.ts
+++ b/lib/techsales-rfq/service.ts
@@ -33,6 +33,7 @@ import { sendEmail } from "../mail/sendEmail";
import { formatDate } from "../utils";
import { techVendors, techVendorPossibleItems } from "@/db/schema/techVendors";
import { decryptWithServerAction } from "@/components/drm/drmUtils";
+import { deleteFile, saveDRMFile } from "../file-stroage";
// 정렬 타입 정의
// 의도적으로 any 사용 - drizzle ORM의 orderBy 타입이 복잡함
@@ -1377,24 +1378,9 @@ export async function createTechSalesRfqAttachments(params: {
// 트랜잭션으로 처리
await db.transaction(async (tx) => {
- const path = await import("path");
- const fs = await import("fs/promises");
- const { randomUUID } = await import("crypto");
-
- // 파일 저장 디렉토리 생성
- const rfqDir = path.join(process.cwd(), "public", "techsales-rfq", String(techSalesRfqId));
- await fs.mkdir(rfqDir, { recursive: true });
for (const file of files) {
- const decryptedBuffer = await decryptWithServerAction(file);
-
- // 고유 파일명 생성
- const uniqueName = `${randomUUID()}-${file.name}`;
- const relativePath = path.join("techsales-rfq", String(techSalesRfqId), uniqueName);
- const absolutePath = path.join(process.cwd(), "public", relativePath);
-
- // 파일 저장
- await fs.writeFile(absolutePath, Buffer.from(decryptedBuffer));
+ const saveResult = await saveDRMFile(file, decryptWithServerAction,`techsales-rfq/${techSalesRfqId}` )
// DB에 첨부파일 레코드 생성
const [newAttachment] = await tx.insert(techSalesAttachments).values({
@@ -1402,7 +1388,7 @@ export async function createTechSalesRfqAttachments(params: {
attachmentType,
fileName: uniqueName,
originalFileName: file.name,
- filePath: "/" + relativePath.replace(/\\/g, "/"),
+ filePath: saveResult.publicPath,
fileSize: file.size,
fileType: file.type || undefined,
description: description || undefined,
@@ -1529,11 +1515,8 @@ export async function deleteTechSalesRfqAttachment(attachmentId: number) {
// 파일 시스템에서 파일 삭제
try {
- const path = await import("path");
- const fs = await import("fs/promises");
-
- const absolutePath = path.join(process.cwd(), "public", attachment.filePath);
- await fs.unlink(absolutePath);
+ await deleteFile(`${attachment.filePath}`)
+
} catch (fileError) {
console.warn("파일 삭제 실패:", fileError);
// 파일 삭제 실패는 심각한 오류가 아니므로 계속 진행
@@ -1592,9 +1575,6 @@ export async function processTechSalesRfqAttachments(params: {
};
await db.transaction(async (tx) => {
- const path = await import("path");
- const fs = await import("fs/promises");
- const { randomUUID } = await import("crypto");
// 1. 삭제할 첨부파일 처리
if (deleteAttachmentIds.length > 0) {
@@ -1609,41 +1589,23 @@ export async function processTechSalesRfqAttachments(params: {
.returning();
results.deleted.push(deletedAttachment);
+ await deleteFile(attachment.filePath)
- // 파일 시스템에서 파일 삭제
- try {
- const absolutePath = path.join(process.cwd(), "public", attachment.filePath);
- await fs.unlink(absolutePath);
- } catch (fileError) {
- console.warn("파일 삭제 실패:", fileError);
- }
}
}
// 2. 새 파일 업로드 처리
if (newFiles.length > 0) {
- const rfqDir = path.join(process.cwd(), "public", "techsales-rfq", String(techSalesRfqId));
- await fs.mkdir(rfqDir, { recursive: true });
-
for (const { file, attachmentType, description } of newFiles) {
- // 파일 복호화
- const decryptedBuffer = await decryptWithServerAction(file);
-
- // 고유 파일명 생성
- const uniqueName = `${randomUUID()}-${file.name}`;
- const relativePath = path.join("techsales-rfq", String(techSalesRfqId), uniqueName);
- const absolutePath = path.join(process.cwd(), "public", relativePath);
-
- // 복호화된 파일 저장
- await fs.writeFile(absolutePath, Buffer.from(decryptedBuffer));
+ const saveResult = await saveDRMFile(file, decryptWithServerAction,`techsales-rfq/${techSalesRfqId}` )
// DB에 첨부파일 레코드 생성
const [newAttachment] = await tx.insert(techSalesAttachments).values({
techSalesRfqId,
attachmentType,
- fileName: uniqueName,
+ fileName: saveResult.fileName,
originalFileName: file.name,
- filePath: "/" + relativePath.replace(/\\/g, "/"),
+ filePath: saveResult.publicPath,
fileSize: file.size,
fileType: file.type || undefined,
description: description || undefined,