diff --git a/CloudServer/server.js b/CloudServer/server.js index 6184dc4..8fc4558 100644 --- a/CloudServer/server.js +++ b/CloudServer/server.js @@ -149,11 +149,16 @@ function safeRelativeToBase(baseDir, relativePath) { } app.use('/media/audio/general', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); const dirs = getBeepAudioDirs(); - if (!dirs || dirs.length === 0) return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); let i = 0; const tryNext = () => { - if (i >= dirs.length) return next(); + if (i >= dirs.length) { + const candidates = (dirs || []).map(dir => safeRelativeToBase(dir, rel)).filter(Boolean); + if (candidates && candidates.length) return sendBinaryFileFromCandidates(res, candidates); + return next(); + } const dir = String(dirs[i++] || ''); if (!dir) return tryNext(); express.static(dir, { fallthrough: true })(req, res, tryNext); @@ -161,62 +166,71 @@ app.use('/media/audio/general', (req, res, next) => { tryNext(); }); app.use('/media/audio/beep', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); const dirs = getBeepAudioDirs(); - if (!dirs || dirs.length === 0) return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); let i = 0; const tryNext = () => { - if (i >= dirs.length) return next(); + if (i >= dirs.length) { + const candidates = (dirs || []).map(dir => safeRelativeToBase(dir, rel)).filter(Boolean); + if (candidates && candidates.length) return sendBinaryFileFromCandidates(res, candidates); + return next(); + } const dir = String(dirs[i++] || ''); if (!dir) return tryNext(); express.static(dir, { fallthrough: true })(req, res, tryNext); }; tryNext(); }); -app.use('/media/audio/music', express.static(String(audioMusicDir || ''), { fallthrough: true })); -app.use('/media/audio/voice', express.static(String(audioVoiceDir || ''), { fallthrough: true })); -app.use('/media/audio/line', express.static(String(audioLineDir || ''), { fallthrough: true })); -app.use('/media/audio/station', express.static(String(audioStationDir || ''), { fallthrough: true })); -app.use('/media/temp', express.static(String(tempBroadcastDir || ''), { fallthrough: true })); - -app.get('/media/audio/general/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const candidates = getBeepAudioDirs().map(dir => safeRelativeToBase(dir, rel)).filter(Boolean); - sendBinaryFileFromCandidates(res, candidates); +app.use('/media/audio/music', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); + const base = String(audioMusicDir || ''); + express.static(base, { fallthrough: true })(req, res, () => { + const full = base ? safeRelativeToBase(base, rel) : null; + if (!full) return res.status(404).end(); + sendBinaryFile(res, full); + }); }); -app.get('/media/audio/beep/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const candidates = getBeepAudioDirs().map(dir => safeRelativeToBase(dir, rel)).filter(Boolean); - sendBinaryFileFromCandidates(res, candidates); +app.use('/media/audio/voice', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); + const base = String(audioVoiceDir || ''); + express.static(base, { fallthrough: true })(req, res, () => { + const full = base ? safeRelativeToBase(base, rel) : null; + if (!full) return res.status(404).end(); + sendBinaryFile(res, full); + }); }); -app.get('/media/audio/music/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const full = safeRelativeToBase(audioMusicDir, rel); - if (!full) return res.status(404).end(); - sendBinaryFile(res, full); +app.use('/media/audio/line', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); + const base = String(audioLineDir || ''); + express.static(base, { fallthrough: true })(req, res, () => { + const full = base ? safeRelativeToBase(base, rel) : null; + if (!full) return res.status(404).end(); + sendBinaryFile(res, full); + }); }); -app.get('/media/audio/voice/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const full = safeRelativeToBase(audioVoiceDir, rel); - if (!full) return res.status(404).end(); - sendBinaryFile(res, full); +app.use('/media/audio/station', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); + const base = String(audioStationDir || ''); + express.static(base, { fallthrough: true })(req, res, () => { + const full = base ? safeRelativeToBase(base, rel) : null; + if (!full) return res.status(404).end(); + sendBinaryFile(res, full); + }); }); -app.get('/media/audio/line/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const full = safeRelativeToBase(audioLineDir, rel); - if (!full) return res.status(404).end(); - sendBinaryFile(res, full); -}); -app.get('/media/audio/station/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const full = safeRelativeToBase(audioStationDir, rel); - if (!full) return res.status(404).end(); - sendBinaryFile(res, full); -}); -app.get('/media/temp/:path(*)', (req, res) => { - const rel = decodeURIComponent(req.params.path || ''); - const full = safeRelativeToBase(tempBroadcastDir, rel); - if (!full) return res.status(404).end(); - sendBinaryFile(res, full); +app.use('/media/temp', (req, res, next) => { + if (req.method && String(req.method).toUpperCase() !== 'GET') return next(); + const rel = decodeURIComponent(String(req.path || '/').replace(/^\/+/, '')); + const base = String(tempBroadcastDir || ''); + express.static(base, { fallthrough: true })(req, res, () => { + const full = base ? safeRelativeToBase(base, rel) : null; + if (!full) return res.status(404).end(); + sendBinaryFile(res, full); + }); }); app.get('/media', (req, res) => { diff --git a/station_broadcast.lua b/station_broadcast.lua index 5cc8b1d..ac0b432 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -1,6 +1,6 @@ -- 1. Configuration & Constants -local APP_VERSION_TEXT = "v1.0.103" +local APP_VERSION_TEXT = "v1.0.104" local CONFIG = { CLOUD_URL = "ws://stabadmin.fse-media.group",