All checks were successful
Deploy / build-and-deploy (push) Successful in 1m44s
68 lines
2.2 KiB
TypeScript
68 lines
2.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getServerSession } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
import { sounds } from "@/lib/db/schema";
|
|
import { writeFile, mkdir, unlink } from "fs/promises";
|
|
import path from "path";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
import { convertToCompressedMp3 } from "@/lib/convert";
|
|
|
|
export const maxDuration = 60;
|
|
|
|
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
|
|
const ALLOWED_EXTENSIONS = /\.(mp3|ogg|wav|flac|aac|m4a|wma|opus|webm|mp4|avi|mov|mkv|webm|wmv|flv)$/i;
|
|
const MAX_SIZE = 50 * 1024 * 1024; // 50 MB
|
|
|
|
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 (!ALLOWED_EXTENSIONS.test(file.name)) {
|
|
return NextResponse.json({ error: "Unsupported file format" }, { status: 400 });
|
|
}
|
|
|
|
if (file.size > MAX_SIZE) {
|
|
return NextResponse.json({ error: "File too large (max 50MB)" }, { status: 400 });
|
|
}
|
|
|
|
await mkdir(UPLOAD_DIR, { recursive: true });
|
|
|
|
const baseName = uuidv4();
|
|
const inputPath = path.join(UPLOAD_DIR, `${baseName}.in`);
|
|
const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
|
|
|
|
const bytes = await file.arrayBuffer();
|
|
await writeFile(inputPath, Buffer.from(bytes));
|
|
|
|
let storedFilename: string;
|
|
if (convertToCompressedMp3(inputPath, mp3Path)) {
|
|
await unlink(inputPath).catch(() => {});
|
|
storedFilename = `${baseName}.mp3`;
|
|
|
|
const id = uuidv4();
|
|
db.insert(sounds).values({
|
|
id,
|
|
originalName: file.name,
|
|
storedFilename,
|
|
duration: 6,
|
|
uploadedBy: session.id,
|
|
createdAt: new Date().toISOString(),
|
|
}).run();
|
|
|
|
return NextResponse.json({ url: `/uploads/sounds/${storedFilename}` });
|
|
}
|
|
|
|
await unlink(inputPath).catch(() => {});
|
|
return NextResponse.json({ error: "Conversion failed" }, { status: 500 });
|
|
} catch {
|
|
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
|
|
}
|
|
}
|