v2.0: sound library, image upload, DnD, ResourceBrowser, Barotrauma HUD, captain sanity
Some checks failed
Deploy / build-and-deploy (push) Failing after 52s

This commit is contained in:
2026-06-15 01:07:05 +03:00
parent 9deec9d23e
commit d0cd008f6e
16 changed files with 1039 additions and 231 deletions

View File

@@ -20,7 +20,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ cam
}
try {
const { campaignId } = await params;
const { text, emoji, soundCategory, soundUrl, gridIndex } = await req.json();
const { text, emoji, soundCategory, soundUrl, imageUrl, gridIndex } = await req.json();
if (!text) return NextResponse.json({ error: "Text required" }, { status: 400 });
const existing = db.select().from(bingoItems)
@@ -32,7 +32,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ cam
const id = uuidv4();
const now = new Date().toISOString();
db.insert(bingoItems).values({ id, campaignId, text, emoji: emoji || "💀", soundCategory: soundCategory || "horn", soundUrl: soundUrl || null, gridIndex, createdAt: now }).run();
db.insert(bingoItems).values({ id, campaignId, text, emoji: emoji || "💀", soundCategory: soundCategory || "horn", soundUrl: soundUrl || null, imageUrl: imageUrl || null, gridIndex, createdAt: now }).run();
return NextResponse.json({ id });
} catch {
return NextResponse.json({ error: "Failed to add item" }, { status: 500 });
@@ -49,10 +49,11 @@ export async function PUT(req: NextRequest, { params }: { params: Promise<{ camp
const body = await req.json();
if (!body.id) return NextResponse.json({ error: "Item ID required" }, { status: 400 });
const updates: Record<string, unknown> = {};
if (body.text) updates.text = body.text;
if (body.emoji) updates.emoji = body.emoji;
if (body.soundCategory) updates.soundCategory = body.soundCategory;
if (body.text !== undefined) updates.text = body.text;
if (body.emoji !== undefined) updates.emoji = body.emoji;
if (body.soundCategory !== undefined) updates.soundCategory = body.soundCategory;
if (body.soundUrl !== undefined) updates.soundUrl = body.soundUrl;
if (body.imageUrl !== undefined) updates.imageUrl = body.imageUrl;
if (body.gridIndex !== undefined) updates.gridIndex = body.gridIndex;
db.update(bingoItems).set(updates)
.where(and(eq(bingoItems.id, body.id), eq(bingoItems.campaignId, campaignId)))

93
app/api/images/route.ts Normal file
View File

@@ -0,0 +1,93 @@
import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "@/lib/auth";
import { db } from "@/lib/db";
import { images } 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", "images");
const ALLOWED_TYPES = ["image/png", "image/jpeg", "image/gif", "image/webp"];
const MAX_SIZE = 5 * 1024 * 1024;
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(images).orderBy(images.createdAt).all();
const list = all.map(i => ({
id: i.id,
originalName: i.originalName,
url: `/uploads/images/${i.storedFilename}`,
mimeType: i.mimeType,
fileSize: i.fileSize,
}));
return NextResponse.json({ images: list });
} catch {
return NextResponse.json({ images: [] });
}
}
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; mimeType: string; fileSize: number }[] = [];
for (const file of files) {
if (!ALLOWED_TYPES.includes(file.type)) continue;
if (file.size > MAX_SIZE) continue;
const ext = path.extname(file.name) || ".png";
const filename = `${uuidv4()}${ext}`;
const filepath = path.join(UPLOAD_DIR, filename);
const bytes = await file.arrayBuffer();
await writeFile(filepath, Buffer.from(bytes));
const id = uuidv4();
db.insert(images).values({
id,
originalName: file.name,
storedFilename: filename,
mimeType: file.type,
fileSize: file.size,
uploadedBy: session.id,
createdAt: new Date().toISOString(),
}).run();
results.push({ url: `/uploads/images/${filename}`, originalName: file.name, mimeType: file.type, fileSize: file.size });
}
return NextResponse.json({ images: 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(images).where(eq(images.storedFilename, filename)).run();
return NextResponse.json({ success: true });
} catch {
return NextResponse.json({ error: "Delete failed" }, { status: 500 });
}
}

View File

@@ -0,0 +1,63 @@
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 path from "path";
import { v4 as uuidv4 } from "uuid";
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
export async function POST(req: NextRequest) {
const session = await getServerSession();
if (!session || !session.isAdmin) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
try {
const { name, duration, previewUrl } = await req.json();
if (!previewUrl || !name) {
return NextResponse.json({ error: "Missing name or previewUrl" }, { status: 400 });
}
if (duration > 6) {
return NextResponse.json({ error: "Sound too long (max 6s)" }, { status: 400 });
}
await mkdir(UPLOAD_DIR, { recursive: true });
const res = await fetch(previewUrl);
if (!res.ok) return NextResponse.json({ error: "Failed to download preview" }, { status: 502 });
const buffer = Buffer.from(await res.arrayBuffer());
// trim to 6 seconds: approximate by byte ratio
const maxBytes = Math.floor(buffer.length * Math.min(6 / duration, 1));
const trimmed = buffer.subarray(0, maxBytes);
const ext = ".mp3";
const filename = `${uuidv4()}${ext}`;
const filepath = path.join(UPLOAD_DIR, filename);
await writeFile(filepath, trimmed);
const id = uuidv4();
const originalName = name.replace(/\.[^.]+$/, "") + ext;
db.insert(sounds).values({
id,
originalName,
storedFilename: filename,
duration: Math.min(duration, 6),
uploadedBy: session.id,
createdAt: new Date().toISOString(),
}).run();
return NextResponse.json({
url: `/uploads/sounds/${filename}`,
originalName,
duration: Math.min(duration, 6),
});
} catch {
return NextResponse.json({ error: "Import failed" }, { status: 500 });
}
}

View File

@@ -1,9 +1,13 @@
import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "@/lib/auth";
import { readdir, unlink } from "fs/promises";
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 SOUNDS_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
const UPLOAD_DIR = path.join(process.cwd(), "public", "uploads", "sounds");
export async function GET() {
const session = await getServerSession();
@@ -11,17 +15,64 @@ export async function GET() {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
try {
const files = await readdir(SOUNDS_DIR);
const sounds = files
.filter(f => f.endsWith(".ogg"))
.map(f => ({ filename: f, url: `/uploads/sounds/${f}` }))
.sort((a, b) => a.filename.localeCompare(b.filename));
return NextResponse.json({ sounds });
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) {
@@ -30,8 +81,9 @@ export async function DELETE(req: NextRequest) {
const filename = req.nextUrl.searchParams.get("filename");
if (!filename) return NextResponse.json({ error: "No filename" }, { status: 400 });
try {
const filepath = path.join(SOUNDS_DIR, filename);
await unlink(filepath);
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 });

View File

@@ -0,0 +1,29 @@
import { NextRequest, NextResponse } from "next/server";
const FREESOUND_API = "https://freesound.org/apiv2/search/text/";
const API_KEY = process.env.FREESOUND_API_KEY || "";
export async function GET(req: NextRequest) {
const query = req.nextUrl.searchParams.get("q") || "";
const page = parseInt(req.nextUrl.searchParams.get("page") || "1");
if (!query) return NextResponse.json({ results: [] });
if (!API_KEY) return NextResponse.json({ error: "Freesound API key not configured" }, { status: 500 });
try {
const url = `${FREESOUND_API}?query=${encodeURIComponent(query)}&page=${page}&token=${API_KEY}&page_size=12&fields=id,name,duration,previews`;
const res = await fetch(url);
const data = await res.json();
const results = (data.results || []).map((r: any) => ({
id: r.id,
name: r.name,
duration: r.duration,
previewUrl: r.previews?.["preview-lq-mp3"] || r.previews?.["preview-hq-mp3"] || null,
}));
return NextResponse.json({ results, count: data.count || 0 });
} catch (e) {
return NextResponse.json({ error: "Freesound search failed" }, { status: 500 });
}
}

View File

@@ -1,5 +1,7 @@
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 path from "path";
import { v4 as uuidv4 } from "uuid";
@@ -32,6 +34,19 @@ export async function POST(req: NextRequest) {
const bytes = await file.arrayBuffer();
await writeFile(filepath, Buffer.from(bytes));
// approximate duration
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();
return NextResponse.json({ url: `/uploads/sounds/${filename}` });
} catch {
return NextResponse.json({ error: "Upload failed" }, { status: 500 });