feat: accept any audio/video upload, server converts to 6s compressed MP3
Deploy / build-and-deploy (push) Successful in 1m44s

This commit is contained in:
2026-06-15 03:25:44 +03:00
parent 6a4be6b939
commit 3f41d7699c
5 changed files with 54 additions and 57 deletions
+16 -13
View File
@@ -6,9 +6,13 @@ import { writeFile, mkdir, unlink } from "fs/promises";
import path from "path";
import { v4 as uuidv4 } from "uuid";
import { eq } from "drizzle-orm";
import { convertOggToMp3 } from "@/lib/convert";
import { convertToCompressedMp3 } from "@/lib/convert";
export const maxDuration = 120;
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 GET() {
const session = await getServerSession();
@@ -43,37 +47,36 @@ export async function POST(req: NextRequest) {
const results: { url: string; originalName: string; duration: number }[] = [];
for (const file of files) {
if (!file.name.toLowerCase().endsWith(".ogg")) continue;
if (file.size > 2 * 1024 * 1024) continue;
if (!ALLOWED_EXTENSIONS.test(file.name)) continue;
if (file.size > MAX_SIZE) continue;
const baseName = uuidv4();
const oggPath = path.join(UPLOAD_DIR, `${baseName}.ogg`);
const inputPath = path.join(UPLOAD_DIR, `${baseName}.in`);
const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
const bytes = await file.arrayBuffer();
await writeFile(oggPath, Buffer.from(bytes));
const duration = Math.round((file.size / (64 * 128)) * 10) / 10;
await writeFile(inputPath, Buffer.from(bytes));
let storedFilename: string;
if (convertOggToMp3(oggPath, mp3Path)) {
await unlink(oggPath).catch(() => {});
if (convertToCompressedMp3(inputPath, mp3Path)) {
await unlink(inputPath).catch(() => {});
storedFilename = `${baseName}.mp3`;
} else {
storedFilename = `${baseName}.ogg`;
await unlink(inputPath).catch(() => {});
continue;
}
const id = uuidv4();
db.insert(sounds).values({
id,
originalName: file.name.replace(/\.ogg$/i, ".mp3"),
originalName: file.name,
storedFilename,
duration: Math.min(duration, 6),
duration: 6,
uploadedBy: session.id,
createdAt: new Date().toISOString(),
}).run();
results.push({ url: `/uploads/sounds/${storedFilename}`, originalName: file.name.replace(/\.ogg$/i, ".mp3"), duration: Math.min(duration, 6) });
results.push({ url: `/uploads/sounds/${storedFilename}`, originalName: file.name, duration: 6 });
}
return NextResponse.json({ sounds: results });
+27 -28
View File
@@ -5,10 +5,13 @@ 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";
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();
@@ -21,47 +24,43 @@ export async function POST(req: NextRequest) {
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 (!ALLOWED_EXTENSIONS.test(file.name)) {
return NextResponse.json({ error: "Unsupported file format" }, { status: 400 });
}
if (file.size > 2 * 1024 * 1024) {
return NextResponse.json({ error: "File too large (max 2MB)" }, { 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 oggPath = path.join(UPLOAD_DIR, `${baseName}.ogg`);
const inputPath = path.join(UPLOAD_DIR, `${baseName}.in`);
const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
// save temporary OGG
const bytes = await file.arrayBuffer();
await writeFile(oggPath, Buffer.from(bytes));
await writeFile(inputPath, 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(() => {});
if (convertToCompressedMp3(inputPath, mp3Path)) {
await unlink(inputPath).catch(() => {});
storedFilename = `${baseName}.mp3`;
} else {
storedFilename = `${baseName}.ogg`;
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}` });
}
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}` });
await unlink(inputPath).catch(() => {});
return NextResponse.json({ error: "Conversion failed" }, { status: 500 });
} catch {
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
}