2021-12-27 17:51:19 +01:00
|
|
|
const Path = require('path')
|
2023-05-27 23:00:34 +02:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const fs = require('../libs/fsExtra')
|
2024-01-03 23:23:17 +01:00
|
|
|
const { toNumber } = require('../utils/index')
|
|
|
|
const fileUtils = require('../utils/fileUtils')
|
2021-12-26 18:25:07 +01:00
|
|
|
|
|
|
|
class FileSystemController {
|
|
|
|
constructor() { }
|
|
|
|
|
2024-01-03 23:23:17 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
*/
|
2021-12-26 18:25:07 +01:00
|
|
|
async getPaths(req, res) {
|
2023-05-27 23:00:34 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[FileSystemController] Non-admin user attempting to get filesystem paths`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2024-01-03 23:23:17 +01:00
|
|
|
const relpath = req.query.path
|
|
|
|
const level = toNumber(req.query.level, 0)
|
|
|
|
|
|
|
|
// Validate path. Must be absolute
|
|
|
|
if (relpath && (!Path.isAbsolute(relpath) || !await fs.pathExists(relpath))) {
|
|
|
|
Logger.error(`[FileSystemController] Invalid path in query string "${relpath}"`)
|
|
|
|
return res.status(400).send('Invalid "path" query string')
|
|
|
|
}
|
|
|
|
Logger.debug(`[FileSystemController] Getting file paths at ${relpath || 'root'} (${level})`)
|
|
|
|
|
|
|
|
let directories = []
|
2021-12-26 18:25:07 +01:00
|
|
|
|
2024-01-03 23:23:17 +01:00
|
|
|
// Windows returns drives first
|
|
|
|
if (global.isWin) {
|
|
|
|
if (relpath) {
|
|
|
|
directories = await fileUtils.getDirectoriesInPath(relpath, level)
|
|
|
|
} else {
|
|
|
|
const drives = await fileUtils.getWindowsDrives().catch((error) => {
|
|
|
|
Logger.error(`[FileSystemController] Failed to get windows drives`, error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
if (drives.length) {
|
|
|
|
directories = drives.map(d => {
|
|
|
|
return {
|
|
|
|
path: d,
|
|
|
|
dirname: d,
|
|
|
|
level: 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
directories = await fileUtils.getDirectoriesInPath(relpath || '/', level)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude some dirs from this project to be cleaner in Docker
|
|
|
|
const excludedDirs = ['node_modules', 'client', 'server', '.git', 'static', 'build', 'dist', 'metadata', 'config', 'sys', 'proc', '.devcontainer', '.nyc_output', '.github', '.vscode'].map(dirname => {
|
|
|
|
return fileUtils.filePathToPOSIX(Path.join(global.appRoot, dirname))
|
|
|
|
})
|
|
|
|
directories = directories.filter(dir => {
|
|
|
|
return !excludedDirs.includes(dir.path)
|
2021-12-26 18:25:07 +01:00
|
|
|
})
|
|
|
|
|
2022-11-29 18:55:22 +01:00
|
|
|
res.json({
|
2024-01-03 23:23:17 +01:00
|
|
|
posix: !global.isWin,
|
|
|
|
directories
|
2022-11-29 18:55:22 +01:00
|
|
|
})
|
2021-12-26 18:25:07 +01:00
|
|
|
}
|
2023-05-27 23:00:34 +02:00
|
|
|
|
|
|
|
// POST: api/filesystem/pathexists
|
|
|
|
async checkPathExists(req, res) {
|
|
|
|
if (!req.user.canUpload) {
|
|
|
|
Logger.error(`[FileSystemController] Non-admin user attempting to check path exists`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
const filepath = req.body.filepath
|
|
|
|
if (!filepath?.length) {
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
|
|
|
|
|
|
|
const exists = await fs.pathExists(filepath)
|
|
|
|
res.json({
|
|
|
|
exists
|
|
|
|
})
|
|
|
|
}
|
2021-12-26 18:25:07 +01:00
|
|
|
}
|
|
|
|
module.exports = new FileSystemController()
|