All checks were successful
Deploy / build-and-deploy (push) Successful in 1m49s
104 lines
3.5 KiB
TypeScript
104 lines
3.5 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";
|
|
import { convertToCompressedMp3 } from "@/lib/convert";
|
|
|
|
export const maxDuration = 120;
|
|
|
|
const UPLOAD_DIR = path.join(process.cwd(), "data", "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();
|
|
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 (!ALLOWED_EXTENSIONS.test(file.name)) continue;
|
|
if (file.size > MAX_SIZE) continue;
|
|
|
|
const baseName = uuidv4();
|
|
const inputPath = path.join(UPLOAD_DIR, `${baseName}.in`);
|
|
const mp3Path = path.join(UPLOAD_DIR, `${baseName}.mp3`);
|
|
|
|
const bytes = await file.arrayBuffer();
|
|
await writeFile(inputPath, Buffer.from(bytes));
|
|
|
|
let storedFilename: string;
|
|
if (convertToCompressedMp3(inputPath, mp3Path)) {
|
|
await unlink(inputPath).catch(() => {});
|
|
storedFilename = `${baseName}.mp3`;
|
|
} else {
|
|
await unlink(inputPath).catch(() => {});
|
|
continue;
|
|
}
|
|
|
|
const id = uuidv4();
|
|
db.insert(sounds).values({
|
|
id,
|
|
originalName: file.name,
|
|
storedFilename,
|
|
duration: 6,
|
|
uploadedBy: session.id,
|
|
createdAt: new Date().toISOString(),
|
|
}).run();
|
|
|
|
results.push({ url: `/uploads/sounds/${storedFilename}`, originalName: file.name, 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 });
|
|
}
|
|
}
|