92 lines
3.2 KiB
TypeScript
92 lines
3.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 { eq } from "drizzle-orm";
|
|
|
|
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
|
|
|
|
export async function GET() {
|
|
const session = await getServerSession();
|
|
if (!session || !session.isAdmin) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
try {
|
|
const all = db.select().from(sounds).orderBy(sounds.createdAt).all();
|
|
const list = all.map(s => ({
|
|
id: s.id,
|
|
originalName: s.originalName,
|
|
url: `/uploads/sounds/${s.storedFilename}`,
|
|
duration: s.duration,
|
|
}));
|
|
return NextResponse.json({ sounds: list });
|
|
} catch {
|
|
return NextResponse.json({ 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 files = formData.getAll("files") as File[];
|
|
if (!files.length) return NextResponse.json({ error: "No files" }, { status: 400 });
|
|
|
|
await mkdir(UPLOAD_DIR, { recursive: true });
|
|
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;
|
|
|
|
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));
|
|
|
|
// compute duration from file size (approximate for OGG Vorbis ~64kbps)
|
|
const duration = Math.round((file.size / (64 * 128)) * 10) / 10;
|
|
|
|
const id = uuidv4();
|
|
db.insert(sounds).values({
|
|
id,
|
|
originalName: file.name,
|
|
storedFilename: filename,
|
|
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) });
|
|
}
|
|
|
|
return NextResponse.json({ sounds: results });
|
|
} catch {
|
|
return NextResponse.json({ error: "Upload failed" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function DELETE(req: NextRequest) {
|
|
const session = await getServerSession();
|
|
if (!session || !session.isAdmin) {
|
|
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
}
|
|
const filename = req.nextUrl.searchParams.get("filename");
|
|
if (!filename) return NextResponse.json({ error: "No filename" }, { status: 400 });
|
|
try {
|
|
const filepath = path.join(UPLOAD_DIR, filename);
|
|
await unlink(filepath).catch(() => {});
|
|
db.delete(sounds).where(eq(sounds.storedFilename, filename)).run();
|
|
return NextResponse.json({ success: true });
|
|
} catch {
|
|
return NextResponse.json({ error: "Delete failed" }, { status: 500 });
|
|
}
|
|
}
|