|
| 1 | +// server/index.js |
| 2 | +import Fastify from 'fastify'; |
| 3 | +import fastifyStatic from '@fastify/static'; |
| 4 | +import fs from 'fs/promises'; |
| 5 | +import path from 'path'; |
| 6 | +import { fileURLToPath } from 'url'; |
| 7 | + |
| 8 | +// ESM __dirname workaround |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const __dirname = path.dirname(__filename); |
| 11 | + |
| 12 | +// Paths |
| 13 | +const rootDir = path.resolve(__dirname, '..'); |
| 14 | +const publicDir = path.join(rootDir, 'public'); |
| 15 | +const configPath = path.join(publicDir, 'config.yaml'); |
| 16 | + |
| 17 | +const app = Fastify({ logger: true }); |
| 18 | + |
| 19 | +const fileExtensionFilter = { |
| 20 | + covers: /\.(jpg|jpeg|webp|avif|png|gif|bmp)$/i, |
| 21 | + subs: /\.vtt$/i, |
| 22 | + file: /.*/, |
| 23 | +} |
| 24 | + |
| 25 | +// 1. Load config.yaml and resolve mediaFolder |
| 26 | +let musicDir = path.join(publicDir, 'music'); // fallback default |
| 27 | + |
| 28 | +async function loadConfig() { |
| 29 | + try { |
| 30 | + const yamlRaw = await fs.readFile(configPath, 'utf8'); |
| 31 | + const config = loadYaml(yamlRaw); |
| 32 | + |
| 33 | + if (config && typeof config.mediaFolder === 'string') { |
| 34 | + // Resolve mediaFolder relative to rootDir |
| 35 | + musicDir = path.resolve(rootDir, config.mediaFolder.replace(/^\/+/, '')); |
| 36 | + app.log.info(`mediaFolder set to: ${musicDir}`); |
| 37 | + } |
| 38 | + } catch (err) { |
| 39 | + app.log.warn(`Failed to load config.yaml: ${err.message}`); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +await loadConfig(); |
| 44 | + |
| 45 | +// Serve entire /public as root |
| 46 | +app.register(fastifyStatic, { |
| 47 | + root: publicDir, |
| 48 | + prefix: '/', // makes index.html available at / |
| 49 | + index: ['index.html'], |
| 50 | +}); |
| 51 | + |
| 52 | +app.get('/music/*', async (request, reply) => { |
| 53 | + try { |
| 54 | + // Extract requested path |
| 55 | + const rawPath = request.params['*'] || ''; |
| 56 | + const decodedPath = decodeURIComponent(rawPath); |
| 57 | + const safeSubPath = path.normalize(decodedPath).replace(/^(\.\.(\/|\\|$))+/, ''); |
| 58 | + |
| 59 | + const targetPath = path.join(musicDir, safeSubPath); |
| 60 | + const stat = await fs.stat(targetPath); |
| 61 | + |
| 62 | + if (stat.isFile()) { |
| 63 | + // Send the file (with correct headers) |
| 64 | + return reply.sendFile(safeSubPath, musicDir); // Fastify Static will stream the file |
| 65 | + } |
| 66 | + |
| 67 | + if (!stat.isDirectory()) { |
| 68 | + return reply.code(404).send({ error: 'Not a file or directory' }); |
| 69 | + } |
| 70 | + |
| 71 | + // Handle directory listing |
| 72 | + const entries = await fs.readdir(targetPath, { withFileTypes: true }); |
| 73 | + |
| 74 | + const dirs = [], files = [], imgs = [], subs = []; |
| 75 | + |
| 76 | + for (const entry of entries) { |
| 77 | + const name = entry.name; |
| 78 | + if (entry.isDirectory()) { |
| 79 | + dirs.push({ name }); |
| 80 | + } else if (entry.isFile()) { |
| 81 | + if (name.match(fileExtensionFilter.covers)) imgs.push({ name }); |
| 82 | + else if (name.match(fileExtensionFilter.subs)) subs.push({ name }); |
| 83 | + if (name.match(fileExtensionFilter.file) && !name.startsWith('.')) files.push({ name }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + reply.type('application/json').send({ dirs, files, imgs, subs }); |
| 88 | + |
| 89 | + } catch (err) { |
| 90 | + if (err.code === 'ENOENT') { |
| 91 | + reply.code(404).send({ error: 'Not found' }); |
| 92 | + } else { |
| 93 | + app.log.error(err); |
| 94 | + reply.code(500).send({ error: 'Internal server error' }); |
| 95 | + } |
| 96 | + } |
| 97 | +}); |
| 98 | + |
| 99 | +// Start server |
| 100 | +app.listen({ port: 8080 }, err => { |
| 101 | + if (err) { |
| 102 | + app.log.error(err); |
| 103 | + process.exit(1); |
| 104 | + } |
| 105 | +}); |
0 commit comments