feat: convert uploaded OGG to MP3 via ffmpeg
All checks were successful
Deploy / build-and-deploy (push) Successful in 2m4s

This commit is contained in:
2026-06-15 01:25:50 +03:00
parent e158467c76
commit 19d7a161c7
5 changed files with 70 additions and 20 deletions

3
.gitignore vendored
View File

@@ -35,6 +35,9 @@ yarn-error.log*
/data/*.db-wal /data/*.db-wal
/data/*.db-shm /data/*.db-shm
# uploads (user content)
/public/uploads/
# env files (can opt-in for committing if needed) # env files (can opt-in for committing if needed)
.env* .env*

View File

@@ -13,7 +13,8 @@ WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1 ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs && \ RUN apk add --no-cache ffmpeg && \
addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public COPY --from=builder /app/public ./public

View File

@@ -6,6 +6,7 @@ import { writeFile, mkdir, unlink } from "fs/promises";
import path from "path"; import path from "path";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { convertOggToMp3 } from "@/lib/convert";
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds"); 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.name.toLowerCase().endsWith(".ogg")) continue;
if (file.size > 2 * 1024 * 1024) continue; if (file.size > 2 * 1024 * 1024) continue;
const ext = path.extname(file.name); const baseName = uuidv4();
const filename = `${uuidv4()}${ext}`; const oggPath = path.join(UPLOAD_DIR, `${baseName}.ogg`);
const filepath = path.join(UPLOAD_DIR, filename); const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
const bytes = await file.arrayBuffer();
await writeFile(filepath, Buffer.from(bytes)); 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; 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(); const id = uuidv4();
db.insert(sounds).values({ db.insert(sounds).values({
id, id,
originalName: file.name, originalName: file.name.replace(/\.ogg$/i, ".mp3"),
storedFilename: filename, storedFilename,
duration: Math.min(duration, 6), duration: Math.min(duration, 6),
uploadedBy: session.id, uploadedBy: session.id,
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
}).run(); }).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 }); return NextResponse.json({ sounds: results });

View File

@@ -2,9 +2,11 @@ import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "@/lib/auth"; import { getServerSession } from "@/lib/auth";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { sounds } from "@/lib/db/schema"; import { sounds } from "@/lib/db/schema";
import { writeFile, mkdir } from "fs/promises"; import { writeFile, mkdir, unlink } from "fs/promises";
import path from "path"; import path from "path";
import { v4 as uuidv4 } from "uuid"; 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"); 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 }); 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; 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(); const id = uuidv4();
db.insert(sounds).values({ db.insert(sounds).values({
id, id,
originalName: file.name, originalName: file.name.replace(/\.ogg$/i, ".mp3"),
storedFilename: filename, storedFilename,
duration: Math.min(duration, 6), duration: Math.min(duration, 6),
uploadedBy: session.id, uploadedBy: session.id,
createdAt: new Date().toISOString(), createdAt: new Date().toISOString(),
}).run(); }).run();
return NextResponse.json({ url: `/uploads/sounds/${filename}` }); return NextResponse.json({ url: `/uploads/sounds/${storedFilename}` });
} catch { } catch {
return NextResponse.json({ error: "Upload failed" }, { status: 500 }); return NextResponse.json({ error: "Upload failed" }, { status: 500 });
} }

23
lib/convert.ts Normal file
View File

@@ -0,0 +1,23 @@
import { execSync } from "child_process";
import fs from "fs";
export function convertOggToMp3(inputPath: string, outputPath: string): boolean {
try {
execSync(
`ffmpeg -i "${inputPath}" -codec:a libmp3lame -b:a 128k -y "${outputPath}" 2>/dev/null`,
{ timeout: 15000 }
);
return true;
} catch {
return false;
}
}
export function convertOggToMp3OrCopy(inputPath: string, mp3Path: string): string {
if (convertOggToMp3(inputPath, mp3Path)) {
return mp3Path;
}
// fallback: keep original OGG
fs.copyFileSync(inputPath, mp3Path.replace(/\.mp3$/, ".ogg"));
return mp3Path.replace(/\.mp3$/, ".ogg");
}