diff options
Diffstat (limited to 'lib/vendors/service.ts')
| -rw-r--r-- | lib/vendors/service.ts | 47 |
1 files changed, 9 insertions, 38 deletions
diff --git a/lib/vendors/service.ts b/lib/vendors/service.ts index 7c6ac15d..2328752b 100644 --- a/lib/vendors/service.ts +++ b/lib/vendors/service.ts @@ -50,10 +50,6 @@ import type { import { asc, desc, ilike, inArray, and, or, gte, lte, eq, isNull, count, sql } from "drizzle-orm"; import { rfqItems, rfqs, vendorRfqView } from "@/db/schema/rfq"; import path from "path"; -import fs from "fs/promises"; -import { randomUUID } from "crypto"; -import JSZip from 'jszip'; -import { promises as fsPromises } from 'fs'; import { sendEmail } from "../mail/sendEmail"; import { PgTransaction } from "drizzle-orm/pg-core"; import { items, materials } from "@/db/schema/items"; @@ -61,7 +57,7 @@ import { roles, userRoles, users } from "@/db/schema/users"; import { getServerSession } from "next-auth"; import { authOptions } from "@/app/api/auth/[...nextauth]/route"; import { contracts, contractsDetailView, projects, vendorPQSubmissions, vendorProjectPQs, vendorsLogs } from "@/db/schema"; -import { Hospital } from "lucide-react"; +import { deleteFile, saveFile } from "../file-stroage"; /* ----------------------------------------------------- @@ -235,32 +231,17 @@ async function storeVendorFiles( files: File[], attachmentType: string ) { - const vendorDir = path.join( - process.cwd(), - "public", - "vendors", - String(vendorId) - ) - await fs.mkdir(vendorDir, { recursive: true }) + for (const file of files) { - // Convert file to buffer - const ab = await file.arrayBuffer() - const buffer = Buffer.from(ab) - // Generate a unique filename - const uniqueName = `${randomUUID()}-${file.name}` - const relativePath = path.join("vendors", String(vendorId), uniqueName) - const absolutePath = path.join(process.cwd(), "public", relativePath) - - // Write to disk - await fs.writeFile(absolutePath, buffer) + const saveResult = await saveFile({file, directory:`vendors/${vendorId}` }) // Insert attachment record await tx.insert(vendorAttachments).values({ vendorId, fileName: file.name, - filePath: "/" + relativePath.replace(/\\/g, "/"), + filePath: saveResult.publicPath, attachmentType, // "GENERAL", "CREDIT_RATING", "CASH_FLOW_RATING", ... }) } @@ -1440,17 +1421,8 @@ export async function cleanupTempFiles(fileName: string) { 'use server'; try { - const tempDir = path.join(process.cwd(), 'tmp'); - const filePath = path.join(tempDir, fileName); - - try { - // 파일 존재 확인 - await fsPromises.access(filePath, fs.constants.F_OK); - // 파일 삭제 - await fsPromises.unlink(filePath); - } catch { - // 파일이 없으면 무시 - } + + await deleteFile(`tmp/${fileName}`) return { success: true }; } catch (error) { @@ -2318,10 +2290,9 @@ export async function updateVendorInfo(params: { // 3-2. 파일 시스템에서 파일 삭제 for (const attachment of attachmentsToDelete) { try { - // 파일 경로는 /public 기준이므로 process.cwd()/public을 앞에 붙임 - const filePath = path.join(process.cwd(), 'public', attachment.filePath.replace(/^\//, '')) - await fs.access(filePath, fs.constants.F_OK) // 파일 존재 확인 - await fs.unlink(filePath) // 파일 삭제 + + await deleteFile(attachment.filePath) + } catch (error) { console.warn(`Failed to delete file for attachment ${attachment.id}:`, error) // 파일 삭제 실패해도 DB에서는 삭제 진행 |
