diff --git a/public/index.js b/public/index.js index 22ebbbf..ddc2f5e 100644 --- a/public/index.js +++ b/public/index.js @@ -178,6 +178,8 @@ queue.push(ctxState) break case 'download': + audio.pause() + window.open('/api/serve/by-id/' + ctxState + '?dl=1', '_blank') break } ctxHide() diff --git a/server.js b/server.js index 19ef515..eacad88 100755 --- a/server.js +++ b/server.js @@ -111,6 +111,7 @@ router.get('/track/:id', async (req, res, next) => { router.get('/playlists', async (req, res, next) => { let db = await dbPromise let playlists = await db.all('SELECT * FROM Playlist') + res.jsonp(playlists) }) @@ -123,6 +124,7 @@ router.get('/playlist/:id', async (req, res, next) => { let tracks = await db.all('SELECT trackId FROM PlaylistEntry WHERE playlistId = ?', id) playlist.tracks = tracks + res.jsonp(playlist) }) @@ -132,8 +134,14 @@ router.get('/serve/by-id/:id', async (req, res, next) => { let track = await db.get('SELECT file FROM Track WHERE id = ?', id) if (!track) return next(new Error('404 file not found')) + let fpath = path.resolve(track.file) + res.set('Cache-Control', 'public, max-age=31557600') - res.sendFile(path.resolve(track.file)) + if (req.query.dl && parseInt(req.query.dl) === 1) { + return res.download(fpath) + } + + res.sendFile(fpath) }) router.use((err, req, res, next) => {