Files
BaraBingo/app/api/upload/sound/route.ts
SlavaVlad 19d7a161c7
All checks were successful
Deploy / build-and-deploy (push) Successful in 2m4s
feat: convert uploaded OGG to MP3 via ffmpeg
2026-06-15 01:25:50 +03:00

69 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 { convertOggToMp3 } from "@/lib/convert";
import fs from "fs";
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 baseName = uuidv4();
const oggPath = path.join(UPLOAD_DIR, `${baseName}.ogg`);
const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
// save temporary OGG
const bytes = await file.arrayBuffer();
await writeFile(oggPath, Buffer.from(bytes));
// duration estimate
const duration = Math.round((file.size / (64 * 128)) * 10) / 10;
// convert to MP3
let storedFilename: string;
if (convertOggToMp3(oggPath, mp3Path)) {
await unlink(oggPath).catch(() => {});
storedFilename = `${baseName}.mp3`;
} else {
storedFilename = `${baseName}.ogg`;
}
const id = uuidv4();
db.insert(sounds).values({
id,
originalName: file.name.replace(/\.ogg$/i, ".mp3"),
storedFilename,
duration: Math.min(duration, 6),
uploadedBy: session.id,
createdAt: new Date().toISOString(),
}).run();
return NextResponse.json({ url: `/uploads/sounds/${storedFilename}` });
} catch {
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
}
}