mStream/src/api/file-explorer.js
2020-12-02 16:23:42 -05:00

48 lines
1.9 KiB
JavaScript

const path = require('path');
const winston = require('winston');
const fileExplorer = require('../util/file-explorer');
exports.setup = (mstream, program) => {
mstream.post("/api/v1/file-explorer", async (req, res) => {
try {
// Return vpaths if no path is given
if (!req.body.dir || req.body.dir === "" || req.body.dir === "/") {
const directories = [];
for (let dir of req.user.vpaths) {
directories.push({
type: "directory",
name: dir
});
}
return res.json({ path: "/", directories: directories, files: [] });
}
// Get vPath Info
const pathInfo = program.getVPathInfo(req.body.dir, req.user);
if (!pathInfo) { return res.status(500).json({ error: "Could not find file" }); }
// Do not allow browsing outside the directory
if (pathInfo.fullPath.substring(0, pathInfo.basePath.length) !== pathInfo.basePath) {
winston.warn(`user '${req.user.username}' attempted to access a directory they don't have access to: ${pathInfo.fullPath}`)
throw 'Access to directory not allowed';
}
// get directory contents
const folderContents = await fileExplorer.getDirectoryContents(pathInfo.fullPath, program.supportedAudioFiles);
// Format directory string for return value
let returnDirectory = path.join(pathInfo.vpath, pathInfo.relativePath);
returnDirectory = returnDirectory.replace(/\\/g, "/"); // Formatting for windows paths
// TODO: Make sure we have a slash at the beginning
if (returnDirectory.slice(-1) !== "/") { returnDirectory += "/"; } // TODO: Remove trailing slash
res.json({
path: returnDirectory,
files: folderContents.files,
directories: folderContents.directories
});
} catch (err) {
res.status(500).json({ error: "Failed to get directory contents" });
}
});
}