From e39a089e6840c7f47ff46348987798e503638da6 Mon Sep 17 00:00:00 2001 From: Blinko Date: Tue, 19 Nov 2024 13:32:29 +0800 Subject: [PATCH] fix: file upload issue --- app/api/file/upload/route.ts | 3 ++- src/components/Common/Editor/index.tsx | 2 +- src/server/plugins/utils.ts | 6 +----- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/api/file/upload/route.ts b/app/api/file/upload/route.ts index 9834fc6..e3d2410 100644 --- a/app/api/file/upload/route.ts +++ b/app/api/file/upload/route.ts @@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from "next/server"; import path from "path"; import { FileService } from "@/server/plugins/utils"; + export const POST = async (req: Request, res: NextResponse) => { const formData = await req.formData(); const file = formData.getAll('file')[0] @@ -17,6 +18,6 @@ export const POST = async (req: Request, res: NextResponse) => { const originalName = file.name.replaceAll(" ", "_"); const extension = path.extname(originalName); const baseName = path.basename(originalName, extension); - const filePath = await FileService.uploadFile(new File([buffer], originalName)) + const filePath = await FileService.uploadFile(buffer, originalName) return NextResponse.json({ Message: "Success", status: 200, ...filePath }); }; diff --git a/src/components/Common/Editor/index.tsx b/src/components/Common/Editor/index.tsx index 3573ea8..a0db43f 100644 --- a/src/components/Common/Editor/index.tsx +++ b/src/components/Common/Editor/index.tsx @@ -174,7 +174,7 @@ const Editor = observer(({ content, onChange, onSend, isSendLoading, bottomSlot, onRecordingComplete(blob) { const mp3File = new File([blob], Date.now() + '.webm', { type: "audio/webm", - lastModified: Date.now(), // 可以指定最后修改时间为当前时间 + lastModified: Date.now(), }); store.uploadFiles([mp3File]) }, diff --git a/src/server/plugins/utils.ts b/src/server/plugins/utils.ts index 697b5e2..49517d1 100644 --- a/src/server/plugins/utils.ts +++ b/src/server/plugins/utils.ts @@ -54,14 +54,10 @@ export class FileService { } } - static async uploadFile(file: File) { - const buffer = Buffer.from(await file.arrayBuffer()); - const originalName = file.name.replaceAll(" ", "_"); + static async uploadFile(buffer: Buffer, originalName: string) { const extension = path.extname(originalName); const baseName = path.basename(originalName, extension); - const config = await getGlobalConfig(); - if (config.objectStorage === 's3') { const { s3ClientInstance } = await this.getS3Client(); const command = new PutObjectCommand({