- Multi-provider AI analysis (Anthropic, OpenAI, Ollama, Algorithmic) - server-only guards on all provider files; client bundle fix - /settings page with provider status, Ollama model picker, preferences - Song search box on /analyze replacing raw MBID input (debounced, keyboard nav) - Auto-register song via MusicBrainz on POST /api/tracks (no more 404) - Fix WAV duration bug: last section songEnd was double-counting elapsed time - Registry sync comment updated for self-hosted HTTPS git servers Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { getTempoMapsForSong, getSongByMbid, insertTempoMap, upsertSong } from "@/lib/db/client";
|
|
import { validateCTP } from "@/lib/ctp/validate";
|
|
import { lookupRecording, formatArtistCredit, mbDurationToSeconds } from "@/lib/musicbrainz/client";
|
|
|
|
// ─── GET /api/tracks?mbid=<uuid> ─────────────────────────────────────────────
|
|
|
|
const GetSchema = z.object({
|
|
mbid: z.string().uuid(),
|
|
});
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const params = Object.fromEntries(req.nextUrl.searchParams);
|
|
const parsed = GetSchema.safeParse(params);
|
|
|
|
if (!parsed.success) {
|
|
return NextResponse.json(
|
|
{ error: "mbid (UUID) is required" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const maps = await getTempoMapsForSong(parsed.data.mbid);
|
|
return NextResponse.json({ maps });
|
|
}
|
|
|
|
// ─── POST /api/tracks ─────────────────────────────────────────────────────────
|
|
// Body: a raw CTP document JSON
|
|
|
|
export async function POST(req: NextRequest) {
|
|
let body: unknown;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
|
|
}
|
|
|
|
const validation = validateCTP(body);
|
|
if (!validation.success) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "CTP document validation failed",
|
|
details: validation.errors.flatten(),
|
|
},
|
|
{ status: 422 }
|
|
);
|
|
}
|
|
|
|
const doc = validation.data;
|
|
|
|
if (!doc.metadata.mbid) {
|
|
return NextResponse.json(
|
|
{ error: "CTP document must include a metadata.mbid to be stored" },
|
|
{ status: 422 }
|
|
);
|
|
}
|
|
|
|
// Ensure the song exists — auto-register it if not
|
|
const existing = await getSongByMbid(doc.metadata.mbid);
|
|
|
|
if (!existing) {
|
|
try {
|
|
const rec = await lookupRecording(doc.metadata.mbid);
|
|
await upsertSong({
|
|
mbid: doc.metadata.mbid,
|
|
title: rec.title,
|
|
artist: formatArtistCredit(rec["artist-credit"]),
|
|
duration_seconds: mbDurationToSeconds(rec.length),
|
|
acousticbrainz_bpm: null,
|
|
acousticbrainz_time_sig_num: null,
|
|
source: "musicbrainz",
|
|
});
|
|
} catch {
|
|
// MusicBrainz unreachable — fall back to CTP metadata
|
|
await upsertSong({
|
|
mbid: doc.metadata.mbid,
|
|
title: doc.metadata.title,
|
|
artist: doc.metadata.artist,
|
|
duration_seconds: doc.metadata.duration_seconds,
|
|
acousticbrainz_bpm: null,
|
|
acousticbrainz_time_sig_num: null,
|
|
source: "manual",
|
|
});
|
|
}
|
|
}
|
|
|
|
const map = await insertTempoMap({
|
|
song_mbid: doc.metadata.mbid,
|
|
ctp_data: body as Record<string, unknown>,
|
|
contributed_by: doc.metadata.contributed_by,
|
|
});
|
|
|
|
return NextResponse.json({ map }, { status: 201 });
|
|
}
|