From 9dc017715600fa2cbd7aab45e2f50034ac326cd7 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Sat, 7 Sep 2024 14:57:54 +0800 Subject: [PATCH] feat(api): add `common.generateUploadPath()` --- packages/engine-chronocat-api/src/common/file.ts | 9 ++++++--- packages/engine-chronocat-api/src/common/index.ts | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/engine-chronocat-api/src/common/file.ts b/packages/engine-chronocat-api/src/common/file.ts index 78bb3ce..df1843a 100644 --- a/packages/engine-chronocat-api/src/common/file.ts +++ b/packages/engine-chronocat-api/src/common/file.ts @@ -177,7 +177,7 @@ async function saveFile( }, fileName: string, ) { - const filePath = await generateFilePath(ctx, fileName) + const filePath = await commonGenerateUploadPath(ctx, fileName) await finished(file.pipe(createWriteStream(filePath))) return filePath } @@ -187,12 +187,15 @@ async function saveBuffer( buffer: Buffer, fileName: string, ) { - const filePath = await generateFilePath(ctx, fileName) + const filePath = await commonGenerateUploadPath(ctx, fileName) await writeFile(filePath, buffer) return filePath } -async function generateFilePath(ctx: ChronocatContext, fileName: string) { +export async function commonGenerateUploadPath( + ctx: ChronocatContext, + fileName: string, +) { const dir = join(ctx.chronocat.baseDir, 'tmp/upload') await mkdir(dir, { recursive: true, diff --git a/packages/engine-chronocat-api/src/common/index.ts b/packages/engine-chronocat-api/src/common/index.ts index e347788..a78192b 100644 --- a/packages/engine-chronocat-api/src/common/index.ts +++ b/packages/engine-chronocat-api/src/common/index.ts @@ -1,4 +1,4 @@ -import { commonFile } from './file' +import { commonFile, commonGenerateUploadPath } from './file' import { commonSave } from './save' import { commonSend, commonSendForward } from './send' @@ -7,6 +7,7 @@ export const common = { sendForward: commonSendForward, save: commonSave, file: commonFile, + generateUploadPath: commonGenerateUploadPath, } as const export type Common = typeof common