From 09a094e14fa7975b8552cfba89b2fecd0a2f129d Mon Sep 17 00:00:00 2001 From: IrosTheBeggar Date: Mon, 15 Feb 2021 23:08:35 -0500 Subject: [PATCH] code cleanup and file check for download functions --- src/api/download.js | 49 +++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/api/download.js b/src/api/download.js index 3a63fd1..87f934c 100644 --- a/src/api/download.js +++ b/src/api/download.js @@ -46,13 +46,12 @@ exports.setup = (mstream) => { const archive = archiver('zip'); archive.on('error', (err) => { - winston.error(`Download Error: ${err.message}`); - res.status(500).json({ error: err.message }); + winston.error('Download Error', { stack: err }) + res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' }); }); res.attachment('mstream-directory.zip'); - // streaming magic archive.pipe(res); archive.directory(pathInfo.fullPath, false); archive.finalize(); @@ -62,34 +61,33 @@ exports.setup = (mstream) => { } }); - mstream.get('/api/v1/download/zip', (req, res) => { - let fileArray; - if (req.sharedPlaylistId) { - fileArray = shared.lookupPlaylist(req.sharedPlaylistId).playlist; - } else { - // TODO: - return res.status(500).json({ error: err.message }); + mstream.get('/api/v1/download/shared', (req, res) => { + try { + if (!req.sharedPlaylistId) { throw 'Missing Playlist Id'; } + const fileArray = shared.lookupPlaylist(req.sharedPlaylistId).playlist; + download(req, res, fileArray); + } catch (err) { + winston.error('Download Error', { stack: err }) + res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' }); } - - download(req, res, fileArray); }); mstream.post('/api/v1/download/zip', (req, res) => { - let fileArray; - if (req.sharedPlaylistId) { - fileArray = shared.lookupPlaylist(req.sharedPlaylistId).playlist; - } else { - fileArray = JSON.parse(req.body.fileArray); + try { + const fileArray = JSON.parse(req.body.fileArray); + download(req, res, fileArray); + } catch (err) { + winston.error('Download Error', { stack: err }) + res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' }); } - download(req, res, fileArray); }); - function download(req, res, fileArray) { + async function download(req, res, fileArray) { const archive = archiver('zip'); archive.on('error', err => { - winston.error(`Download Error: ${err.message}`); - res.status(500).json({ error: err.message }); + winston.error('Download Error', { stack: err }) + res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' }); }); res.attachment(`mstream-playlist.zip`); @@ -97,12 +95,11 @@ exports.setup = (mstream) => { //streaming magic archive.pipe(res); - for (let i in fileArray) { - // TODO: Confirm each item in posted data is a real file - const pathInfo = vpath.getVPathInfo(fileArray[i], req.user); + for(const file of fileArray) { + const pathInfo = vpath.getVPathInfo(file, req.user); if (!pathInfo) { continue; } - - archive.file(pathInfo.fullPath, { name: path.basename(fileArray[i]) }); + try { await fs.access(pathInfo.fullPath)} catch (err) { return; } + archive.file(pathInfo.fullPath, { name: path.basename(file) }); } archive.finalize();