2024-08-12 00:01:25 +02:00
|
|
|
const { Request, Response } = require('express')
|
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
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObject
|
|
|
|
* @property {import('../models/User')} user
|
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2021-12-26 18:25:07 +01:00
|
|
|
class FileSystemController {
|
2024-08-11 00:15:21 +02:00
|
|
|
constructor() {}
|
2021-12-26 18:25:07 +01:00
|
|
|
|
2024-01-03 23:23:17 +01:00
|
|
|
/**
|
2024-08-11 00:15:21 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-01-03 23:23:17 +01:00
|
|
|
*/
|
2021-12-26 18:25:07 +01:00
|
|
|
async getPaths(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[FileSystemController] Non-admin user "${req.user.username}" attempting to get filesystem paths`)
|
2023-05-27 23:00:34 +02:00
|
|
|
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
|
2024-08-11 00:15:21 +02:00
|
|
|
if (relpath && (!Path.isAbsolute(relpath) || !(await fs.pathExists(relpath)))) {
|
2024-01-03 23:23:17 +01:00
|
|
|
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) {
|
2024-08-11 00:15:21 +02:00
|
|
|
directories = drives.map((d) => {
|
2024-01-03 23:23:17 +01:00
|
|
|
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
|
2024-08-11 00:15:21 +02:00
|
|
|
const excludedDirs = ['node_modules', 'client', 'server', '.git', 'static', 'build', 'dist', 'metadata', 'config', 'sys', 'proc', '.devcontainer', '.nyc_output', '.github', '.vscode'].map((dirname) => {
|
2024-01-03 23:23:17 +01:00
|
|
|
return fileUtils.filePathToPOSIX(Path.join(global.appRoot, dirname))
|
|
|
|
})
|
2024-08-11 00:15:21 +02:00
|
|
|
directories = directories.filter((dir) => {
|
2024-01-03 23:23:17 +01:00
|
|
|
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
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/filesystem/pathexists
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-05-27 23:00:34 +02:00
|
|
|
async checkPathExists(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.canUpload) {
|
|
|
|
Logger.error(`[FileSystemController] Non-admin user "${req.user.username}" attempting to check path exists`)
|
2023-05-27 23:00:34 +02:00
|
|
|
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
|
|
|
}
|
2024-08-11 00:15:21 +02:00
|
|
|
module.exports = new FileSystemController()
|