feat: convert uploaded OGG to MP3 via ffmpeg
All checks were successful
Deploy / build-and-deploy (push) Successful in 2m4s
All checks were successful
Deploy / build-and-deploy (push) Successful in 2m4s
This commit is contained in:
@@ -6,6 +6,7 @@ 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";
|
||||
|
||||
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
|
||||
|
||||
@@ -45,26 +46,34 @@ export async function POST(req: NextRequest) {
|
||||
if (!file.name.toLowerCase().endsWith(".ogg")) continue;
|
||||
if (file.size > 2 * 1024 * 1024) continue;
|
||||
|
||||
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));
|
||||
const baseName = uuidv4();
|
||||
const oggPath = path.join(UPLOAD_DIR, `${baseName}.ogg`);
|
||||
const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
|
||||
|
||||
const bytes = await file.arrayBuffer();
|
||||
await writeFile(oggPath, Buffer.from(bytes));
|
||||
|
||||
// compute duration from file size (approximate for OGG Vorbis ~64kbps)
|
||||
const duration = Math.round((file.size / (64 * 128)) * 10) / 10;
|
||||
|
||||
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,
|
||||
storedFilename: filename,
|
||||
originalName: file.name.replace(/\.ogg$/i, ".mp3"),
|
||||
storedFilename,
|
||||
duration: Math.min(duration, 6),
|
||||
uploadedBy: session.id,
|
||||
createdAt: new Date().toISOString(),
|
||||
}).run();
|
||||
|
||||
results.push({ url: `/uploads/sounds/${filename}`, originalName: file.name, duration: Math.min(duration, 6) });
|
||||
results.push({ url: `/uploads/sounds/${storedFilename}`, originalName: file.name.replace(/\.ogg$/i, ".mp3"), duration: Math.min(duration, 6) });
|
||||
}
|
||||
|
||||
return NextResponse.json({ sounds: results });
|
||||
|
||||
@@ -2,9 +2,11 @@ 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 } from "fs/promises";
|
||||
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");
|
||||
|
||||
@@ -28,26 +30,38 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
// approximate duration
|
||||
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,
|
||||
storedFilename: filename,
|
||||
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/${filename}` });
|
||||
return NextResponse.json({ url: `/uploads/sounds/${storedFilename}` });
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user