diff options
| author | joonhoekim <26rote@gmail.com> | 2025-11-26 18:43:10 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-11-26 18:43:10 +0900 |
| commit | 5a370de52d6947d35aa07f9545c4f6a4d83eadd7 (patch) | |
| tree | e66741491771ef941560c34e75b604c5f7dfc14f /lib/dolce-v2/sync-service.ts | |
| parent | 8547034e6d82e4d1184f35af2dbff67180d89dc8 (diff) | |
(김준회) dolce 메모리 효율적 동기화 처리(Streaming), 로컬 건 Date 정보 formatting, Uploader 정보 네임 표시
Diffstat (limited to 'lib/dolce-v2/sync-service.ts')
| -rw-r--r-- | lib/dolce-v2/sync-service.ts | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/lib/dolce-v2/sync-service.ts b/lib/dolce-v2/sync-service.ts index ea56b239..7f866955 100644 --- a/lib/dolce-v2/sync-service.ts +++ b/lib/dolce-v2/sync-service.ts @@ -1,6 +1,9 @@ "use server"; import fs from "fs/promises"; +import { createReadStream, createWriteStream } from "fs"; +import { Readable } from "stream"; +import { pipeline } from "stream/promises"; import path from "path"; import { v4 as uuidv4 } from "uuid"; import db from "@/db/db"; @@ -41,11 +44,15 @@ async function ensureUploadDir() { async function saveFileToLocal(file: File): Promise<SavedFile> { await ensureUploadDir(); - const buffer = Buffer.from(await file.arrayBuffer()); const uniqueName = `${uuidv4()}_${file.name}`; const localPath = path.join(LOCAL_UPLOAD_DIR, uniqueName); - await fs.writeFile(localPath, buffer); + // Stream: Web Stream (file.stream()) -> Node Writable Stream (fs.createWriteStream) + // Readable.fromWeb requires Node 18+ + const readable = Readable.fromWeb(file.stream() as any); + const writable = createWriteStream(localPath); + + await pipeline(readable, writable); return { originalName: file.name, @@ -254,16 +261,20 @@ async function uploadLocalFiles(uploadId: string, userId: string, files: SavedFi const file = files[i]; const fileId = uuidv4(); - // 로컬 파일 읽기 - const fileBuffer = await fs.readFile(file.localPath); + // 로컬 파일 읽기 (Stream) + const fileStream = createReadStream(file.localPath); // 업로드 API 호출 const uploadUrl = `${DOLCE_API_URL}/PWPUploadService.ashx?UploadId=${uploadId}&FileId=${fileId}`; const uploadResponse = await fetch(uploadUrl, { method: "POST", - headers: { "Content-Type": "application/octet-stream" }, - body: fileBuffer, - }); + headers: { + "Content-Type": "application/octet-stream", + "Content-Length": file.size.toString() + }, + body: fileStream as any, // Node stream as body + duplex: "half", // Required for Node streams + } as RequestInit & { duplex?: string }); if (!uploadResponse.ok) throw new Error(`File upload failed: ${uploadResponse.status}`); |
