Skip to content

Commit

Permalink
fix: file upload issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinko committed Nov 19, 2024
1 parent bbf3a8e commit e39a089
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
3 changes: 2 additions & 1 deletion app/api/file/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 });
};
2 changes: 1 addition & 1 deletion src/components/Common/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
},
Expand Down
6 changes: 1 addition & 5 deletions src/server/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit e39a089

Please sign in to comment.