This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
@@ -21,7 +21,14 @@ import { parseBuffer } from "music-metadata";
|
||||
|
||||
/** On-disk directory where MP3s live. Fixed (not configurable from UI to
|
||||
* avoid any path traversal risk). */
|
||||
export const SOUNDTRACK_DIR = path.join(process.cwd(), "public", "swf", "dcr", "hof_furni", "mp3");
|
||||
export const SOUNDTRACK_DIR = path.join(
|
||||
process.cwd(),
|
||||
"public",
|
||||
"swf",
|
||||
"dcr",
|
||||
"hof_furni",
|
||||
"mp3",
|
||||
);
|
||||
|
||||
/** Default public URL prefix (with trailing slash). Can be overridden via
|
||||
* the `soundtrack_base_url` CMS setting. */
|
||||
@@ -34,19 +41,19 @@ export const MAX_SOUNDTRACK_SIZE = 10 * 1024 * 1024;
|
||||
|
||||
/** Reject codes that would escape the sounds directory or use unsafe chars. */
|
||||
export function isSafeSoundtrackCode(code: string): boolean {
|
||||
return /^[a-zA-Z0-9_-]{1,32}$/.test(code);
|
||||
return /^[a-zA-Z0-9_-]{1,32}$/.test(code);
|
||||
}
|
||||
|
||||
export function getSoundtrackPath(code: string): string {
|
||||
if (!isSafeSoundtrackCode(code)) {
|
||||
throw new Error(`Unsafe soundtrack code: ${code}`);
|
||||
}
|
||||
return path.join(SOUNDTRACK_DIR, `${code}.mp3`);
|
||||
if (!isSafeSoundtrackCode(code)) {
|
||||
throw new Error(`Unsafe soundtrack code: ${code}`);
|
||||
}
|
||||
return path.join(SOUNDTRACK_DIR, `${code}.mp3`);
|
||||
}
|
||||
|
||||
/** Ensure the sounds directory exists (mkdir -p). */
|
||||
export async function ensureSoundtrackDir(): Promise<void> {
|
||||
await fs.mkdir(SOUNDTRACK_DIR, { recursive: true });
|
||||
await fs.mkdir(SOUNDTRACK_DIR, { recursive: true });
|
||||
}
|
||||
|
||||
// ── Validation ─────────────────────────────────────────────────────
|
||||
@@ -62,18 +69,19 @@ export async function ensureSoundtrackDir(): Promise<void> {
|
||||
* files masquerading as .mp3 (e.g. renamed .exe).
|
||||
*/
|
||||
export function validateMp3Bytes(buf: Buffer): boolean {
|
||||
if (buf.length < 4) return false;
|
||||
if (buf.length < 4) return false;
|
||||
|
||||
// ID3v2 tag header
|
||||
if (buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) return true;
|
||||
// ID3v2 tag header
|
||||
if (buf[0] === 0x49 && buf[1] === 0x44 && buf[2] === 0x33) return true;
|
||||
|
||||
// MPEG sync: 0xFF followed by 0xFA/0xFB/0xFE/0xF3/0xF2
|
||||
if (buf[0] === 0xff) {
|
||||
const b = buf[1];
|
||||
if (b === 0xfa || b === 0xfb || b === 0xfe || b === 0xf3 || b === 0xf2) return true;
|
||||
}
|
||||
// MPEG sync: 0xFF followed by 0xFA/0xFB/0xFE/0xF3/0xF2
|
||||
if (buf[0] === 0xff) {
|
||||
const b = buf[1];
|
||||
if (b === 0xfa || b === 0xfb || b === 0xfe || b === 0xf3 || b === 0xf2)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,14 +89,23 @@ export function validateMp3Bytes(buf: Buffer): boolean {
|
||||
* Returns 0 if metadata cannot be parsed.
|
||||
*/
|
||||
export async function extractMp3Duration(buf: Buffer): Promise<number> {
|
||||
try {
|
||||
const meta = await parseBuffer(buf, { mimeType: "audio/mpeg" }, { duration: true });
|
||||
const seconds = meta.format.duration;
|
||||
if (typeof seconds !== "number" || !Number.isFinite(seconds) || seconds <= 0) return 0;
|
||||
return Math.round(seconds);
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
const meta = await parseBuffer(
|
||||
buf,
|
||||
{ mimeType: "audio/mpeg" },
|
||||
{ duration: true },
|
||||
);
|
||||
const seconds = meta.format.duration;
|
||||
if (
|
||||
typeof seconds !== "number" ||
|
||||
!Number.isFinite(seconds) ||
|
||||
seconds <= 0
|
||||
)
|
||||
return 0;
|
||||
return Math.round(seconds);
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ── File I/O ───────────────────────────────────────────────────────
|
||||
@@ -97,22 +114,25 @@ export async function extractMp3Duration(buf: Buffer): Promise<number> {
|
||||
* Write the MP3 payload to disk under the given code.
|
||||
* Creates the parent directory if missing.
|
||||
*/
|
||||
export async function writeSoundtrackFile(code: string, buf: Buffer): Promise<void> {
|
||||
await ensureSoundtrackDir();
|
||||
await fs.writeFile(getSoundtrackPath(code), buf);
|
||||
export async function writeSoundtrackFile(
|
||||
code: string,
|
||||
buf: Buffer,
|
||||
): Promise<void> {
|
||||
await ensureSoundtrackDir();
|
||||
await fs.writeFile(getSoundtrackPath(code), buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the MP3 file for the given code. No-op if the file is already gone.
|
||||
*/
|
||||
export async function deleteSoundtrackFile(code: string): Promise<void> {
|
||||
try {
|
||||
await fs.unlink(getSoundtrackPath(code));
|
||||
} catch (err) {
|
||||
const errCode = (err as NodeJS.ErrnoException).code;
|
||||
if (errCode === "ENOENT") return;
|
||||
throw err;
|
||||
}
|
||||
try {
|
||||
await fs.unlink(getSoundtrackPath(code));
|
||||
} catch (err) {
|
||||
const errCode = (err as NodeJS.ErrnoException).code;
|
||||
if (errCode === "ENOENT") return;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export type { TraxChannel, TraxEvent } from "@/lib/trax-format";
|
||||
@@ -120,8 +140,8 @@ export type { TraxChannel, TraxEvent } from "@/lib/trax-format";
|
||||
// implementation lives in `@/lib/trax-format` so it can be imported by
|
||||
// client components without pulling in Node-only deps from this file.
|
||||
export {
|
||||
detectSoundtrackKind,
|
||||
parseTrackSamples,
|
||||
parseTraxChannels,
|
||||
TRAX_TICK_SECONDS,
|
||||
detectSoundtrackKind,
|
||||
parseTrackSamples,
|
||||
parseTraxChannels,
|
||||
TRAX_TICK_SECONDS,
|
||||
} from "@/lib/trax-format";
|
||||
Reference in new issue
Block a user