Initial commit

This commit is contained in:
mxdyeah committed 2026-07-08 01:15:18 +08:00
commit acadc92e68
780 files changed
+468285

No files matched your search

+95
View File
@@ -0,0 +1,95 @@
import { createHash, randomBytes } from 'node:crypto';
import { createError, deleteCookie, getCookie, setCookie } from 'h3';
import db from '../db';
const SESSION_TTL_MS = 1000 * 60 * 60 * 24 * 7;
function hashToken(token: string) {
return createHash('sha256').update(token).digest('hex');
}
export function cleanupExpiredSessions() {
db.prepare('DELETE FROM sessions WHERE expires_at <= ?').run(Date.now());
}
export function createSession(event: any, userId: number) {
const token = randomBytes(32).toString('hex');
const tokenHash = hashToken(token);
const expiresAt = Date.now() + SESSION_TTL_MS;
db.prepare(
'INSERT INTO sessions (user_id, token_hash, expires_at) VALUES (?, ?, ?)'
).run(userId, tokenHash, expiresAt);
setCookie(event, 'session', token, {
httpOnly: true,
sameSite: 'lax',
path: '/',
maxAge: Math.floor(SESSION_TTL_MS / 1000)
});
}
export function clearAuthSession(event: any) {
const token = getCookie(event, 'session');
if (token) {
const tokenHash = hashToken(token);
db.prepare('DELETE FROM sessions WHERE token_hash = ?').run(tokenHash);
}
deleteCookie(event, 'session', { path: '/' });
}
export type SessionUser = {
id: number;
username: string;
musicList: Array<{ fn: string; id: number }>;
};
export function getSessionUser(event: any): SessionUser | null {
const token = getCookie(event, 'session');
if (!token) return null;
const tokenHash = hashToken(token);
const now = Date.now();
const session = db
.prepare('SELECT user_id FROM sessions WHERE token_hash = ? AND expires_at > ?')
.get(tokenHash, now) as { user_id: number } | undefined;
if (!session) return null;
const user = db
.prepare('SELECT id, username FROM users WHERE id = ?')
.get(session.user_id) as
| { id: number; username: string }
| undefined;
if (!user) {
return null;
}
const favRows = db.prepare(`
SELECT s.id, s.filename as fn
FROM favorites f
JOIN songs s ON f.song_id = s.id
WHERE f.user_id = ?
ORDER BY f.created_at DESC
`).all(user.id) as Array<{ id: number; fn: string }>;
const musicList = favRows.map((row) => ({
fn: row.fn,
id: row.id
}));
return {
id: user.id,
username: user.username,
musicList
};
}
export function requireUser(event: any) {
const user = getSessionUser(event);
if (!user) {
throw createError({ statusCode: 401, statusMessage: 'Unauthorized' });
}
return user;
}
+667
View File
@@ -0,0 +1,667 @@
import { open, stat } from 'node:fs/promises';
import { basename } from 'node:path';
export interface ParsedMetadata {
title: string;
artist: string | null;
trackerFormat: string | null;
channels: number | null;
trackerName: string | null;
message: string | null;
instruments: string[] | null;
metadata: Record<string, any>;
}
// 辅助函数:清理二进制字符串,去除尾部空字符、空白及不可见字符
function cleanString(buffer: Buffer): string {
let str = buffer.toString('utf8');
str = str.replace(/\0+$/, '').trim();
str = str.replace(/[\x00-\x1F\x7F-\x9F]/g, '');
return str;
}
interface Reader {
read(offset: number, length: number): Buffer;
readUInt8(offset: number): number;
readUInt16LE(offset: number): number;
readUInt32LE(offset: number): number;
}
function createSyncReader(scanBuf: Buffer, fileLength: number): Reader {
const bufSize = scanBuf.length;
const read = (offset: number, length: number): Buffer => {
if (offset >= bufSize) {
return Buffer.alloc(0);
}
const realLength = Math.min(length, bufSize - offset);
if (realLength <= 0) {
return Buffer.alloc(0);
}
return scanBuf.subarray(offset, offset + realLength);
};
return {
read,
readUInt8(offset: number): number {
const b = read(offset, 1);
return b.length >= 1 ? b.readUInt8(0) : 0;
},
readUInt16LE(offset: number): number {
const b = read(offset, 2);
return b.length >= 2 ? b.readUInt16LE(0) : 0;
},
readUInt32LE(offset: number): number {
const b = read(offset, 4);
return b.length >= 4 ? b.readUInt32LE(0) : 0;
}
};
}
// 针对不同格式的解析函数
function parseXm(reader: Reader, fileLength: number): ParsedMetadata {
// 17-36字节为 Song Name
const titleBuf = reader.read(17, 20);
const title = cleanString(titleBuf);
// 37-56字节为 Tracker Name
const trackerNameBuf = reader.read(37, 20);
const trackerName = cleanString(trackerNameBuf);
// 68-69字节为 Channels
const channels = reader.readUInt16LE(68);
const numPatterns = reader.readUInt16LE(70);
const numInstruments = reader.readUInt16LE(72);
const headerSize = reader.readUInt32LE(60);
const instruments: string[] = [];
let message: string | null = null;
try {
let offset = 60 + headerSize;
// 1. 跳过所有 patterns
for (let p = 0; p < numPatterns; p++) {
if (offset + 9 > fileLength) break;
const patHeaderLen = reader.readUInt32LE(offset);
const patDataSize = reader.readUInt16LE(offset + 7);
offset += (patHeaderLen > 0 ? patHeaderLen : 9) + patDataSize;
}
// 2. 依次读取每个 Instrument Header 并提取 Name
for (let i = 0; i < numInstruments; i++) {
if (offset + 29 > fileLength) break;
const insSize = reader.readUInt32LE(offset);
const nameBuf = reader.read(offset + 4, 22);
const name = cleanString(nameBuf);
const numSamples = reader.readUInt16LE(offset + 27);
if (name) {
instruments.push(name);
}
// 精准计算并跳过这个 Instrument(含所有 sample headers 和 sample data)
let nextOffset = offset + insSize;
if (numSamples > 0 && insSize > 29) {
const sampleHeaderSize = reader.readUInt32LE(offset + 29);
let totalSampleDataSize = 0;
for (let s = 0; s < numSamples; s++) {
const shOffset = offset + insSize + s * sampleHeaderSize;
if (shOffset + 4 <= fileLength) {
const sampleLength = reader.readUInt32LE(shOffset);
totalSampleDataSize += sampleLength;
}
}
nextOffset = offset + insSize + numSamples * sampleHeaderSize + totalSampleDataSize;
}
offset = nextOffset > offset ? nextOffset : offset + 29;
}
// 3. 提取尾部 OpenMPT 扩展 Chunks (如 text, CMSG, CNAM, INAM)
while (offset + 8 <= fileLength) {
const tagBuf = reader.read(offset, 4);
if (tagBuf.length < 4) break;
const tag = tagBuf.toString('ascii');
// 校验 tag 是否是合法的 4 字符 ASCII(通常为大写字母,或者是 'text')
if (!/^[A-Z0-9]{4}$/.test(tag) && tag !== 'text') {
offset++;
continue;
}
const len = reader.readUInt32LE(offset + 4);
if (offset + 8 + len > fileLength || len < 0 || len > 1048576) {
offset++;
continue;
}
const chunkData = reader.read(offset + 8, len);
if (tag === 'text' || tag === 'CMSG') {
const msg = cleanString(chunkData);
if (msg) {
message = msg;
}
}
offset += 8 + len;
}
} catch (e) {
console.error('Failed to parse XM instruments', e);
}
return {
title,
artist: null,
trackerFormat: 'XM',
channels,
trackerName,
message,
instruments,
metadata: {}
};
}
function parseMod(reader: Reader, fileLength: number): ParsedMetadata {
const titleBuf = reader.read(0, 20);
const title = cleanString(titleBuf);
let signature = '';
if (fileLength >= 1084) {
const sigBuf = reader.read(1080, 4);
signature = sigBuf.toString('ascii');
}
let channels = 4;
if (['M.K.', 'M!K!', '4CHN', 'FLT4'].includes(signature)) {
channels = 4;
} else if (signature === '6CHN') {
channels = 6;
} else if (['8CHN', 'FLT8'].includes(signature)) {
channels = 8;
} else if (signature.endsWith('CHN')) {
const ch = parseInt(signature.substring(0, signature.length - 3), 10);
if (!isNaN(ch)) channels = ch;
} else if (signature.endsWith('CH')) {
const ch = parseInt(signature.substring(0, signature.length - 2), 10);
if (!isNaN(ch)) channels = ch;
}
const instruments: string[] = [];
try {
for (let i = 0; i < 31; i++) {
const offset = 20 + i * 30;
if (offset + 22 > fileLength) break;
const nameBuf = reader.read(offset, 22);
const name = cleanString(nameBuf);
if (name) {
instruments.push(name);
}
}
} catch (e) {
console.error('Failed to parse MOD instruments', e);
}
return {
title,
artist: null,
trackerFormat: 'MOD',
channels,
trackerName: null,
message: null,
instruments,
metadata: { signature }
};
}
async function parseIt(reader: Reader, fileLength: number): Promise<ParsedMetadata> {
const titleBuf = reader.read(4, 26);
const title = cleanString(titleBuf);
const channels = fileLength >= 33 ? reader.readUInt8(32) : 0;
const insNum = fileLength >= 36 ? reader.readUInt16LE(34) : 0;
const smpNum = fileLength >= 38 ? reader.readUInt16LE(36) : 0;
const itOrdNum = fileLength >= 34 ? reader.readUInt16LE(32) : 0;
// 提取 Song Message (留言)
let message: string | null = null;
try {
if (fileLength >= 60) {
const special = reader.readUInt16LE(0x2c);
if (special & 1) {
const msgLength = reader.readUInt16LE(0x36);
const msgOffset = reader.readUInt32LE(0x38);
if (msgOffset > 0 && msgLength > 0 && msgOffset + msgLength <= fileLength) {
const msgBuf = reader.read(msgOffset, msgLength);
message = cleanString(msgBuf);
}
}
}
} catch (e) {
console.error('Failed to parse IT song message', e);
}
const instruments: string[] = [];
try {
const insPtrOffset = 192 + itOrdNum;
const smpPtrOffset = insPtrOffset + insNum * 4;
if (insNum > 0) {
for (let i = 0; i < insNum; i++) {
const ptrIdx = insPtrOffset + i * 4;
if (ptrIdx + 4 > fileLength) break;
const offset = reader.readUInt32LE(ptrIdx);
if (offset > 0 && offset + 64 <= fileLength) {
const nameBuf = reader.read(offset + 32, 32);
const name = cleanString(nameBuf);
if (name) instruments.push(name);
}
}
}
for (let i = 0; i < smpNum; i++) {
const ptrIdx = smpPtrOffset + i * 4;
if (ptrIdx + 4 > fileLength) break;
const offset = reader.readUInt32LE(ptrIdx);
if (offset > 0 && offset + 52 <= fileLength) {
const nameBuf = reader.read(offset + 20, 32);
const name = cleanString(nameBuf);
if (name && !instruments.includes(name)) {
instruments.push(name);
}
}
}
} catch (e) {
console.error('Failed to parse IT instruments', e);
}
return {
title,
artist: null,
trackerFormat: 'IT',
channels,
trackerName: 'Impulse Tracker',
message,
instruments,
metadata: {}
};
}
function parseS3m(reader: Reader, fileLength: number): ParsedMetadata {
const titleBuf = reader.read(0, 28);
const title = cleanString(titleBuf);
let signature = '';
if (fileLength >= 48) {
const sigBuf = reader.read(44, 4);
signature = sigBuf.toString('ascii');
}
const insNum = fileLength >= 36 ? reader.readUInt16LE(34) : 0;
const ordNum = fileLength >= 34 ? reader.readUInt16LE(32) : 0;
const instruments: string[] = [];
try {
const insPtrOffset = 96 + ordNum;
for (let i = 0; i < insNum; i++) {
const ptrIdx = insPtrOffset + i * 2;
if (ptrIdx + 2 > fileLength) break;
const parapointer = reader.readUInt16LE(ptrIdx);
const realOffset = parapointer * 16;
if (realOffset > 0 && realOffset + 76 <= fileLength) {
const nameBuf = reader.read(realOffset + 48, 28);
const name = cleanString(nameBuf);
if (name) instruments.push(name);
}
}
} catch (e) {
console.error('Failed to parse S3M instruments', e);
}
return {
title,
artist: null,
trackerFormat: 'S3M',
channels: null,
trackerName: signature === 'SCRM' ? 'Scream Tracker 3' : null,
message: null,
instruments,
metadata: { signature }
};
}
// 提取 MP3 元数据 (ID3v1 和 ID3v2 标题/注释简易解析)
async function parseMp3(fd: any, fileLength: number): Promise<ParsedMetadata> {
let title = '';
let artist = '';
let message: string | null = null;
const metadata: Record<string, any> = {};
const headerSize = Math.min(8192, fileLength);
if (headerSize > 10) {
const header = Buffer.alloc(headerSize);
await fd.read(header, 0, headerSize, 0);
if (header.subarray(0, 3).toString('ascii') === 'ID3') {
metadata.hasId3v2 = true;
// 1. 查找标题 TIT2 帧
const tit2Idx = header.indexOf('TIT2');
if (tit2Idx !== -1 && tit2Idx + 10 < headerSize) {
const frameSize = header.readUInt32BE(tit2Idx + 4);
const encoding = header.readUInt8(tit2Idx + 10);
const textStart = tit2Idx + 11;
if (textStart + frameSize - 1 <= headerSize && frameSize > 1) {
const textBuf = header.subarray(textStart, textStart + frameSize - 1);
if (encoding === 0) {
title = cleanString(textBuf);
} else if (encoding === 1 || encoding === 2) {
title = textBuf.toString('utf16le');
title = title.replace(/^\ufeff/, '').replace(/\0+$/, '').trim();
} else if (encoding === 3) {
title = cleanString(textBuf);
}
}
}
// 2. 查找作者 TPE1 帧
const tpe1Idx = header.indexOf('TPE1');
if (tpe1Idx !== -1 && tpe1Idx + 10 < headerSize) {
const frameSize = header.readUInt32BE(tpe1Idx + 4);
const encoding = header.readUInt8(tpe1Idx + 10);
const textStart = tpe1Idx + 11;
if (textStart + frameSize - 1 <= headerSize && frameSize > 1) {
const textBuf = header.subarray(textStart, textStart + frameSize - 1);
if (encoding === 0) {
artist = cleanString(textBuf);
} else if (encoding === 1 || encoding === 2) {
artist = textBuf.toString('utf16le');
artist = artist.replace(/^\ufeff/, '').replace(/\0+$/, '').trim();
} else if (encoding === 3) {
artist = cleanString(textBuf);
}
}
}
// 3. 查找留言 COMM 帧
const commIdx = header.indexOf('COMM');
if (commIdx !== -1 && commIdx + 10 < headerSize) {
const frameSize = header.readUInt32BE(commIdx + 4);
const encoding = header.readUInt8(commIdx + 10);
const textStart = commIdx + 11;
if (textStart + frameSize - 1 <= headerSize && frameSize > 4) {
const textBuf = header.subarray(textStart + 3, textStart + frameSize - 1);
let actualStart = 0;
if (encoding === 0 || encoding === 3) {
const descEnd = textBuf.indexOf(0);
if (descEnd !== -1) actualStart = descEnd + 1;
message = cleanString(textBuf.subarray(actualStart));
} else {
let descEnd = -1;
for (let j = 0; j < textBuf.length - 1; j += 2) {
if (textBuf[j] === 0 && textBuf[j+1] === 0) {
descEnd = j;
break;
}
}
if (descEnd !== -1) actualStart = descEnd + 2;
message = textBuf.subarray(actualStart).toString('utf16le');
message = message.replace(/^\ufeff/, '').replace(/\0+$/, '').trim();
}
}
}
}
}
if (fileLength > 128) {
const footer = Buffer.alloc(128);
await fd.read(footer, 0, 128, fileLength - 128);
if (footer.subarray(0, 3).toString('ascii') === 'TAG') {
metadata.hasId3v1 = true;
if (!title) {
const titleBuf = footer.subarray(3, 33);
title = cleanString(titleBuf);
}
const artistBuf = footer.subarray(33, 63);
const artistV1 = cleanString(artistBuf);
if (artistV1) {
metadata.artist = artistV1;
if (!artist) {
artist = artistV1;
}
}
// ID3v1 留言在偏移 97 到 126
if (!message) {
const commentBuf = footer.subarray(97, 127);
message = cleanString(commentBuf);
}
}
}
return {
title,
artist: artist || null,
trackerFormat: 'MP3',
channels: null,
trackerName: null,
message,
instruments: null,
metadata
};
}
// 提取 OGG/FLAC/WAV 元数据 (检索前 8KB)
async function parseGenericAudio(fd: any, fileLength: number, format: string): Promise<ParsedMetadata> {
let title = '';
let artist = '';
let message: string | null = null;
const scanSize = Math.min(8192, fileLength);
if (scanSize > 10) {
const buf = Buffer.alloc(scanSize);
await fd.read(buf, 0, scanSize, 0);
// 1. WAV INFO List INAM / IART / ICMT 查找
if (format === 'WAV') {
const inamIdx = buf.indexOf('INAM');
if (inamIdx !== -1 && inamIdx + 8 < scanSize) {
const size = buf.readUInt32LE(inamIdx + 4);
if (inamIdx + 8 + size <= scanSize && size > 0) {
title = cleanString(buf.subarray(inamIdx + 8, inamIdx + 8 + size));
}
}
const iartIdx = buf.indexOf('IART');
if (iartIdx !== -1 && iartIdx + 8 < scanSize) {
const size = buf.readUInt32LE(iartIdx + 4);
if (iartIdx + 8 + size <= scanSize && size > 0) {
artist = cleanString(buf.subarray(iartIdx + 8, iartIdx + 8 + size));
}
}
const icmtIdx = buf.indexOf('ICMT');
if (icmtIdx !== -1 && icmtIdx + 8 < scanSize) {
const size = buf.readUInt32LE(icmtIdx + 4);
if (icmtIdx + 8 + size <= scanSize && size > 0) {
message = cleanString(buf.subarray(icmtIdx + 8, icmtIdx + 8 + size));
}
}
}
// 2. OGG/FLAC Vorbis Comment TITLE / ARTIST / COMMENT / DESCRIPTION 查找
if (['OGG', 'FLAC'].includes(format)) {
// 2.1 查找标题
let titleIdx = buf.indexOf('TITLE=');
if (titleIdx === -1) {
titleIdx = buf.indexOf('title=');
}
if (titleIdx !== -1 && titleIdx + 6 < scanSize) {
const textBuf = buf.subarray(titleIdx + 6);
let endIdx = 0;
while (endIdx < textBuf.length) {
const charCode = textBuf[endIdx];
if (charCode === undefined || charCode < 32 || charCode >= 127) {
break;
}
endIdx++;
}
title = cleanString(textBuf.subarray(0, endIdx));
}
// 2.2 查找作者
let artistIdx = buf.indexOf('ARTIST=');
let prefixLen = 7;
if (artistIdx === -1) {
artistIdx = buf.indexOf('artist=');
prefixLen = 7;
}
if (artistIdx === -1) {
artistIdx = buf.indexOf('PERFORMER=');
prefixLen = 10;
}
if (artistIdx === -1) {
artistIdx = buf.indexOf('performer=');
prefixLen = 10;
}
if (artistIdx !== -1 && artistIdx + prefixLen < scanSize) {
const textBuf = buf.subarray(artistIdx + prefixLen);
let endIdx = 0;
while (endIdx < textBuf.length) {
const charCode = textBuf[endIdx];
if (charCode === undefined || charCode < 32 || charCode >= 127) {
break;
}
endIdx++;
}
artist = cleanString(textBuf.subarray(0, endIdx));
}
// 2.3 查找留言 (COMMENT 或 DESCRIPTION)
let commIdx = buf.indexOf('COMMENT=');
let commPrefix = 8;
if (commIdx === -1) {
commIdx = buf.indexOf('comment=');
commPrefix = 8;
}
if (commIdx === -1) {
commIdx = buf.indexOf('DESCRIPTION=');
commPrefix = 12;
}
if (commIdx === -1) {
commIdx = buf.indexOf('description=');
commPrefix = 12;
}
if (commIdx !== -1 && commIdx + commPrefix < scanSize) {
const textBuf = buf.subarray(commIdx + commPrefix);
let endIdx = 0;
while (endIdx < textBuf.length) {
const charCode = textBuf[endIdx];
if (charCode === undefined || charCode < 32 || charCode >= 127) {
break;
}
endIdx++;
}
message = cleanString(textBuf.subarray(0, endIdx));
}
}
}
return {
title,
artist: artist || null,
trackerFormat: format,
channels: null,
trackerName: null,
message,
instruments: null,
metadata: {}
};
}
// 主入口函数
export async function parseMetadata(filePath: string, ext: string): Promise<ParsedMetadata> {
const upperExt = ext.toUpperCase();
let fd: any = null;
try {
const fileStats = await stat(filePath);
const fileLength = fileStats.size;
fd = await open(filePath, 'r');
let result: ParsedMetadata;
// 对于 Tracker 音乐,直接预读前 2MB 并在内存中进行高速同步解析,解决超大文件截断问题
if (['XM', 'MOD', 'IT', 'S3M'].includes(upperExt)) {
const scanBufSize = Math.min(2097152, fileLength);
const scanBuf = Buffer.alloc(scanBufSize);
await fd.read(scanBuf, 0, scanBufSize, 0);
const reader = createSyncReader(scanBuf, fileLength);
switch (upperExt) {
case 'XM':
result = parseXm(reader, fileLength);
break;
case 'MOD':
result = parseMod(reader, fileLength);
break;
case 'IT':
result = await parseIt(reader, fileLength);
break;
case 'S3M':
result = parseS3m(reader, fileLength);
break;
default:
throw new Error('Unsupported tracker type');
}
} else {
// 其它非 Tracker 音频格式,依然采用各自的异步解析
switch (upperExt) {
case 'MP3':
result = await parseMp3(fd, fileLength);
break;
case 'OGG':
case 'FLAC':
case 'WAV':
result = await parseGenericAudio(fd, fileLength, upperExt);
break;
default:
result = {
title: '',
artist: null,
trackerFormat: upperExt,
channels: null,
trackerName: null,
message: null,
instruments: null,
metadata: {}
};
}
}
if (!result.title) {
const name = basename(filePath);
const dotIdx = name.lastIndexOf('.');
result.title = dotIdx !== -1 ? name.substring(0, dotIdx) : name;
}
return result;
} catch (error) {
const name = basename(filePath);
const dotIdx = name.lastIndexOf('.');
return {
title: dotIdx !== -1 ? name.substring(0, dotIdx) : name,
artist: null,
trackerFormat: upperExt,
channels: null,
trackerName: null,
message: null,
instruments: null,
metadata: { error: String(error) }
};
} finally {
if (fd) {
await fd.close();
}
}
}
+116
View File
@@ -0,0 +1,116 @@
import { readFile } from "node:fs/promises";
import { join } from "node:path";
export interface SongEntry {
id: number;
fileName: string;
}
const PLAYLIST_PATH = join(
process.cwd(),
"public",
"json",
"playlist_xm_mod_pressed.json",
);
let cachedRaw: unknown[] | null = null;
let cachedSorted: string[] | null = null;
async function loadPlaylistRaw(): Promise<unknown[] | null> {
if (cachedRaw) {
return cachedRaw;
}
try {
const rawText = await readFile(PLAYLIST_PATH, "utf8");
const parsed = JSON.parse(rawText);
if (Array.isArray(parsed)) {
cachedRaw = parsed;
return parsed;
}
} catch (error) {
return null;
}
return null;
}
function extractFileNamesFromJsonh(raw: unknown[]): string[] {
if (!Array.isArray(raw) || raw.length < 2) return [];
const keyCount = Number(raw[0]);
if (!Number.isInteger(keyCount) || keyCount <= 0) return [];
if (raw.length < 1 + keyCount) return [];
const keys = raw.slice(1, 1 + keyCount);
const fnIndex = keys.findIndex((key) => key === "fn");
if (fnIndex === -1) return [];
const valuesStart = 1 + keyCount;
const total = Math.floor((raw.length - valuesStart) / keyCount);
const names: string[] = [];
for (let i = 0; i < total; i += 1) {
const value = raw[valuesStart + i * keyCount + fnIndex];
if (typeof value === "string") {
const trimmed = value.trim();
if (trimmed) names.push(trimmed);
}
}
return names;
}
function extractFileNamesFromPlain(raw: unknown[]): string[] {
const names: string[] = [];
for (const entry of raw) {
if (typeof entry === "string") {
const trimmed = entry.trim();
if (trimmed) names.push(trimmed);
continue;
}
if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
const fn = (entry as { fn?: unknown }).fn;
if (typeof fn === "string") {
const trimmed = fn.trim();
if (trimmed) names.push(trimmed);
}
}
return names;
}
async function loadSortedPlaylist(): Promise<string[] | null> {
if (cachedSorted) return cachedSorted;
const raw = await loadPlaylistRaw();
if (!raw) return null;
const jsonhNames = extractFileNamesFromJsonh(raw);
const names = jsonhNames.length ? jsonhNames : extractFileNamesFromPlain(raw);
if (!names.length) return null;
names.sort((a, b) => {
const fnA = a.toUpperCase();
const fnB = b.toUpperCase();
if (fnA < fnB) return -1;
if (fnA > fnB) return 1;
return 0;
});
cachedSorted = names;
return names;
}
export async function getSongById(id: number): Promise<SongEntry | null> {
if (!Number.isInteger(id) || id < 1) {
return null;
}
const playlist = await loadSortedPlaylist();
if (!playlist) return null;
const fileName = playlist[id - 1];
if (typeof fileName !== "string" || !fileName) return null;
return {
id,
fileName,
};
}
+262
View File
@@ -0,0 +1,262 @@
import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
import db from '../db';
import { parseMetadata } from './metadata-parser';
// 音乐文件夹的基础路径(优先从环境变量读取,默认指向本地 data/music 目录)
const MUSIC_BASE_PATH = process.env.MUSIC_PATH || join(process.cwd(), 'data/music');
// 支持的前端可播放扩展名
// 所有可播放格式:
// - Tracker 模块(Chiptune3/libopenmpt 原生支持)
// - 现代音频(浏览器原生 <audio> 播放)
// - FFmpeg 可转换格式(引擎3 转 MP3 播放)
const PLAYABLE_EXTENSIONS = new Set([
// Tracker: BassoonTracker + Chiptune3
'mod', 'xm',
// Tracker: Chiptune3 (libopenmpt)
'it', 's3m', 'umx', 'mptm', 'stm', 'mtm', 'ptm', 'far', 'ult', '669',
'amf', 'dsm', 'mdl', 'med', 'okt', 'psm', 'dbm', 'imf', 'j2b', 'mo3',
'gdm', 'stp', 'sfx', 'sfx2', 'itp', 'dtm', 'mt2', 'symmod', 'c67', 'ams', 'stx',
'667', 'cba', 'digi', 'dmf', 'dsym', 'etx', 'fc', 'fc13', 'fc14', 'fmt', 'ftm',
'gmc', 'gt2', 'gtk', 'ice', 'ims', 'm15', 'mms', 'mus', 'oxm', 'plm', 'pt36',
'puma', 'rtm', 'smod', 'st26', 'stk', 'wow', 'xmf', 'mdz', 's3z', 'xmz', 'itz',
'mptmz', 'mdr',
// 现代音频:浏览器原生 <audio>
'ogg', 'mp3', 'wav', 'flac',
// FFmpeg 可转换通用音频格式(引擎3)
'aac', 'opus', 'aiff', 'm4a', 'wma', 'ac3', 'dts',
'ape', 'wv', 'tta', 'mp2', 'amr', 'ra', 'au', 'voc', 'pcm',
'spx', 'tak', 'cda', 'adpcm', 'gsm', 'vqf', 'ofr', 'mp1',
]);
export interface ScanStatus {
isScanning: boolean;
totalFiles: number;
processedFiles: number;
addedCount: number;
updatedCount: number;
deletedCount: number;
skippedCount: number;
startTime: number | null;
endTime: number | null;
error: string | null;
}
let currentStatus: ScanStatus = {
isScanning: false,
totalFiles: 0,
processedFiles: 0,
addedCount: 0,
updatedCount: 0,
deletedCount: 0,
skippedCount: 0,
startTime: null,
endTime: null,
error: null
};
// 后台异步执行扫描
export async function runScanAsync(forceRefresh = false) {
if (currentStatus.isScanning) return;
currentStatus = {
isScanning: true,
totalFiles: 0,
processedFiles: 0,
addedCount: 0,
updatedCount: 0,
deletedCount: 0,
skippedCount: 0,
startTime: Date.now(),
endTime: null,
error: null
};
try {
// 1. 读取目录下的所有文件
const files = await readdir(MUSIC_BASE_PATH);
currentStatus.totalFiles = files.length;
// 2. 从数据库中拉取所有已存在的歌曲信息,进行增量比对
const existingSongs = db.prepare('SELECT filename, file_size, file_mtime FROM songs').all() as Array<{
filename: string;
file_size: number;
file_mtime: number;
}>;
const existingMap = new Map<string, { file_size: number; file_mtime: number }>();
for (const song of existingSongs) {
existingMap.set(song.filename, {
file_size: song.file_size,
file_mtime: song.file_mtime
});
}
const currentFilesSet = new Set(files);
// 3. 处理删除的文件
const toDelete: string[] = [];
for (const filename of existingMap.keys()) {
if (!currentFilesSet.has(filename)) {
toDelete.push(filename);
}
}
if (toDelete.length > 0) {
const deleteStmt = db.prepare('DELETE FROM songs WHERE filename = ?');
const deleteTx = db.transaction((names: string[]) => {
for (const name of names) {
deleteStmt.run(name);
}
});
deleteTx(toDelete);
currentStatus.deletedCount = toDelete.length;
}
// 4. 并发读取新文件或修改过的文件并批量写入 SQLite
const concurrency = 100;
const batchSize = 1000;
let batch: any[] = [];
const insertStmt = db.prepare(`
INSERT INTO songs (filename, title, artist, extension, file_size, file_mtime, playable, tracker_format, channels, tracker_name, message, instruments, metadata)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
const updateStmt = db.prepare(`
UPDATE songs SET title = ?, artist = ?, file_size = ?, file_mtime = ?, playable = ?, tracker_format = ?, channels = ?, tracker_name = ?, message = ?, instruments = ?, metadata = ?
WHERE filename = ?
`);
// 批量提交事务
const commitBatch = db.transaction((items: any[]) => {
for (const item of items) {
if (item.action === 'insert') {
insertStmt.run(
item.filename,
item.title,
item.artist,
item.extension,
item.file_size,
item.file_mtime,
item.playable,
item.tracker_format,
item.channels,
item.tracker_name,
item.message,
item.instruments,
item.metadata
);
} else if (item.action === 'update') {
updateStmt.run(
item.title,
item.artist,
item.file_size,
item.file_mtime,
item.playable,
item.tracker_format,
item.channels,
item.tracker_name,
item.message,
item.instruments,
item.metadata,
item.filename
);
}
}
});
// 线程池队列处理
let index = 0;
const workers = Array(concurrency).fill(null).map(async () => {
while (index < files.length) {
const i = index++;
if (i >= files.length) break;
const filename = files[i];
if (!filename) continue;
const filePath = join(MUSIC_BASE_PATH, filename);
try {
const stats = await stat(filePath);
const size = stats.size;
const mtime = stats.mtimeMs;
const existing = existingMap.get(filename);
if (!forceRefresh && existing && existing.file_size === size && Math.abs(existing.file_mtime - mtime) < 1000) {
// 文件未发生变化,跳过
currentStatus.skippedCount++;
continue;
}
// 文件新增或修改
const ext = filename.split('.').pop()?.toLowerCase() || '';
const playable = PLAYABLE_EXTENSIONS.has(ext) ? 1 : 0;
const meta = await parseMetadata(filePath, ext);
const item = {
action: existing ? 'update' : 'insert',
filename,
title: meta.title,
artist: meta.artist,
extension: ext,
file_size: size,
file_mtime: mtime,
playable,
tracker_format: meta.trackerFormat,
channels: meta.channels,
tracker_name: meta.trackerName,
message: meta.message,
instruments: meta.instruments ? JSON.stringify(meta.instruments) : null,
metadata: JSON.stringify(meta.metadata)
};
batch.push(item);
if (batch.length >= batchSize) {
const currentBatch = batch;
batch = [];
commitBatch(currentBatch);
const updates = currentBatch.filter(x => x.action === 'update').length;
const inserts = currentBatch.filter(x => x.action === 'insert').length;
currentStatus.updatedCount += updates;
currentStatus.addedCount += inserts;
}
} catch (e) {
console.error(`Error scanning file ${filename}:`, e);
} finally {
currentStatus.processedFiles++;
}
}
});
await Promise.all(workers);
// 提交剩下的批次
if (batch.length > 0) {
commitBatch(batch);
currentStatus.updatedCount += batch.filter(x => x.action === 'update').length;
currentStatus.addedCount += batch.filter(x => x.action === 'insert').length;
}
currentStatus.endTime = Date.now();
} catch (error: any) {
currentStatus.error = error.message || String(error);
console.error('Scan failed:', error);
} finally {
currentStatus.isScanning = false;
}
}
export function getScanStatus(): ScanStatus {
return currentStatus;
}
export function startScan(forceRefresh = false) {
if (currentStatus.isScanning) return;
// 后台异步执行
runScanAsync(forceRefresh);
}