diff --git a/modules/file-explorer.js b/modules/file-explorer.js index 151f0e9..e9a3552 100644 --- a/modules/file-explorer.js +++ b/modules/file-explorer.js @@ -39,36 +39,6 @@ exports.setup = function(mstream, program) { } } - mstream.post('/download-directory', (req, res) => { - if (!req.body.directory) { - return res.status(500).json({ error: 'Missing Params' }); - } - - // Get full path - const pathInfo = vpath.getVPathInfo(req.body.directory, req.user); - if (!pathInfo) { return res.status(500).json({ error: "Could not find file" }); } - - // Make sure it's a directory - if (!fs.statSync(pathInfo.fullPath).isDirectory()) { - res.status(500).json({ error: "Not a directory" }); - return; - } - - const archive = archiver('zip'); - archive.on('error', function (err) { - winston.error(`Download Error: ${err.message}`); - res.status(500).json({ error: err.message }); - }); - - // sets the archive name. TODO: Rename this - res.attachment('zipped-playlist.zip'); - - // streaming magic - archive.pipe(res); - archive.directory(pathInfo.fullPath, false); - archive.finalize(); - }); - mstream.post('/fileplaylist/download', (req, res, next) => { try { const playlistPathInfo = getPathInfoOrThrow(req, req.body.path); diff --git a/src/api/download.js b/src/api/download.js index d78a0ec..d853689 100644 --- a/src/api/download.js +++ b/src/api/download.js @@ -1,10 +1,39 @@ const archiver = require('archiver'); const path = require('path'); +const fs = require('fs').promises; const winston = require('winston'); const vpath = require('../util/vpath'); const shared = require('../api/shared'); exports.setup = (mstream) => { + mstream.post('/api/v1/download/directory', async (req, res) => { + try { + if (!req.body.directory) { throw 'Validation Error' } + + const pathInfo = vpath.getVPathInfo(req.body.directory, req.user); + if (!pathInfo) { return res.status(500).json({ error: "Could not find file" }); } + + if (!(await fs.stat(pathInfo.fullPath)).isDirectory()) { throw 'Not A Directory'; } + + const archive = archiver('zip'); + archive.on('error', (err) => { + winston.error(`Download Error: ${err.message}`); + res.status(500).json({ error: err.message }); + }); + + res.attachment('mstream-directory.zip'); + + // streaming magic + archive.pipe(res); + archive.directory(pathInfo.fullPath, false); + archive.finalize(); + } catch (err) { + winston.error('Download Error', { stack: err }) + res.status(500).json({ error: typeof err === 'string' ? err : 'Unknown Error' }); + } + }); + + mstream.get('/api/v1/download/zip', (req, res) => { let fileArray; if (req.sharedPlaylistId) { diff --git a/webapp/assets/js/mstream.js b/webapp/assets/js/mstream.js index 9661f95..0a1425f 100644 --- a/webapp/assets/js/mstream.js +++ b/webapp/assets/js/mstream.js @@ -727,7 +727,7 @@ $(document).ready(function () { var directoryString = getDirectoryString($(this)); // Use key if necessary - $("#downform").attr("action", "/download-directory?token=" + MSTREAMAPI.currentServer.token); + $("#downform").attr("action", "/api/v1/download/directory?token=" + MSTREAMAPI.currentServer.token); $('').attr({ type: 'hidden',