Files
BaraBingo/app/api/upload/sound/route.ts
SlavaVlad 05677924b5
Some checks failed
Deploy / build-and-deploy (push) Failing after 2m53s
V1 bingo
2026-06-14 21:29:43 +03:00

40 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "@/lib/auth";
import { writeFile, mkdir } from "fs/promises";
import path from "path";
import { v4 as uuidv4 } from "uuid";
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
export async function POST(req: NextRequest) {
const session = await getServerSession();
if (!session || !session.isAdmin) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
try {
const formData = await req.formData();
const file = formData.get("file") as File | null;
if (!file) return NextResponse.json({ error: "No file" }, { status: 400 });
if (!file.name.toLowerCase().endsWith(".ogg")) {
return NextResponse.json({ error: "Only OGG files allowed" }, { status: 400 });
}
if (file.size > 2 * 1024 * 1024) {
return NextResponse.json({ error: "File too large (max 2MB)" }, { status: 400 });
}
await mkdir(UPLOAD_DIR, { recursive: true });
const ext = path.extname(file.name);
const filename = `${uuidv4()}${ext}`;
const filepath = path.join(UPLOAD_DIR, filename);
const bytes = await file.arrayBuffer();
await writeFile(filepath, Buffer.from(bytes));
return NextResponse.json({ url: `/uploads/sounds/${filename}` });
} catch {
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
}
}