From 53cf8c5744590db7a0616888cf7a0aeb277f4857 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Wed, 10 Oct 2018 13:02:56 +0300 Subject: [PATCH] download --- public/index.js | 2 ++ server.js | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) 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) => {