Initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/auth";
|
||||
import { uploadFile } from "@/lib/storage";
|
||||
|
||||
export const config = { api: { bodyParser: false } };
|
||||
|
||||
// Max 2 GB
|
||||
export const maxDuration = 60;
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const session = await auth();
|
||||
if (!session?.user) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const formData = await req.formData();
|
||||
const file = formData.get("file") as File | null;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "No file provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!file.type.match(/video\//)) {
|
||||
return NextResponse.json({ error: "Only video files are accepted" }, { status: 400 });
|
||||
}
|
||||
|
||||
if (file.size > 2 * 1024 * 1024 * 1024) {
|
||||
return NextResponse.json({ error: "File too large (max 2 GB)" }, { status: 413 });
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
const result = await uploadFile(buffer, file.name, file.type, "videos");
|
||||
|
||||
return NextResponse.json({ url: result.url, key: result.key });
|
||||
} catch (err) {
|
||||
console.error("[local-upload]", err);
|
||||
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user