'城市详情'
This commit is contained in:
39
app/api/upload-media/route.ts
Normal file
39
app/api/upload-media/route.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { uploadBuffer, generateObjectKey } from "@/app/lib/minio";
|
||||
|
||||
const MAX_SIZE = 20 * 1024 * 1024; // 20MB
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const form = await request.formData();
|
||||
const file = form.get("file");
|
||||
const fileObj =
|
||||
file != null && typeof file === "object" && "size" in file
|
||||
? (file as File | Blob)
|
||||
: null;
|
||||
|
||||
if (!fileObj || fileObj.size === 0) {
|
||||
return NextResponse.json({ ok: false, error: "请选择文件" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (fileObj.size > MAX_SIZE) {
|
||||
return NextResponse.json({ ok: false, error: "文件大小不能超过 20MB" }, { status: 400 });
|
||||
}
|
||||
|
||||
const ext =
|
||||
(fileObj instanceof File ? fileObj.name : "").split(".").pop() ||
|
||||
(fileObj.type?.startsWith("video/") ? "mp4" : "jpg");
|
||||
const objectKey = generateObjectKey(ext);
|
||||
|
||||
const buffer = Buffer.from(await fileObj.arrayBuffer());
|
||||
const contentType = (fileObj as File).type || "application/octet-stream";
|
||||
|
||||
const { url } = await uploadBuffer(buffer, objectKey, contentType);
|
||||
|
||||
return NextResponse.json({ ok: true, url });
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
console.error("Upload media error:", e);
|
||||
return NextResponse.json({ ok: false, error: `上传失败: ${msg}` }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user