39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { parseFile as mmParseFile } from 'music-metadata';
|
|
import { DATA_DIR } from './config.js';
|
|
import { loadYearsIndex } from './years.js';
|
|
|
|
export async function listTracks() {
|
|
const years = loadYearsIndex();
|
|
const files = fs.readdirSync(DATA_DIR).filter((f) => /\.(mp3|wav|m4a|ogg)$/i.test(f));
|
|
const tracks = await Promise.all(
|
|
files.map(async (f) => {
|
|
const fp = path.join(DATA_DIR, f);
|
|
let year = null;
|
|
let title = path.parse(f).name;
|
|
let artist = '';
|
|
try {
|
|
const meta = await mmParseFile(fp, { duration: false });
|
|
title = meta.common.title || title;
|
|
artist = meta.common.artist || artist;
|
|
year = meta.common.year || null;
|
|
} catch {}
|
|
const y = years[f]?.year ?? year;
|
|
return { id: f, file: f, title, artist, year: y };
|
|
})
|
|
);
|
|
return tracks;
|
|
}
|
|
|
|
export function registerTracksApi(app) {
|
|
app.get('/api/tracks', async (req, res) => {
|
|
try {
|
|
const tracks = await listTracks();
|
|
res.json({ tracks });
|
|
} catch (e) {
|
|
res.status(500).json({ error: e.message });
|
|
}
|
|
});
|
|
}
|