diff --git a/.gitignore b/.gitignore index 83d4758..983249c 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,9 @@ yarn-error.log* /data/*.db-wal /data/*.db-shm +# uploads (user content) +/public/uploads/ + # env files (can opt-in for committing if needed) .env* diff --git a/Dockerfile b/Dockerfile index 96ca0d5..840b398 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,8 @@ WORKDIR /app ENV NODE_ENV=production 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 COPY --from=builder /app/public ./public diff --git a/app/api/sounds/route.ts b/app/api/sounds/route.ts index 1018ab9..f6cb921 100644 --- a/app/api/sounds/route.ts +++ b/app/api/sounds/route.ts @@ -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 }); diff --git a/app/api/upload/sound/route.ts b/app/api/upload/sound/route.ts index 0e602ab..f512a8d 100644 --- a/app/api/upload/sound/route.ts +++ b/app/api/upload/sound/route.ts @@ -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 }); } diff --git a/lib/convert.ts b/lib/convert.ts new file mode 100644 index 0000000..48a0e2c --- /dev/null +++ b/lib/convert.ts @@ -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"); +}