2021-12-27 17:51:19 +01:00
|
|
|
const Path = require('path')
|
2023-05-27 23:00:34 +02:00
|
|
|
const Logger = require('../Logger')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2023-05-27 23:00:34 +02:00
|
|
|
const fs = require('../libs/fsExtra')
|
2021-12-26 18:25:07 +01:00
|
|
|
|
|
|
|
class FileSystemController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
const excludedDirs = ['node_modules', 'client', 'server', '.git', 'static', 'build', 'dist', 'metadata', 'config', 'sys', 'proc'].map(dirname => {
|
2021-12-26 18:25:07 +01:00
|
|
|
return Path.sep + dirname
|
|
|
|
})
|
|
|
|
|
|
|
|
// Do not include existing mapped library paths in response
|
2023-09-09 14:47:17 +02:00
|
|
|
const libraryFoldersPaths = await Database.libraryFolderModel.getAllLibraryFolderPaths()
|
2023-07-22 21:25:20 +02:00
|
|
|
libraryFoldersPaths.forEach((path) => {
|
|
|
|
let dir = path || ''
|
|
|
|
if (dir.includes(global.appRoot)) dir = dir.replace(global.appRoot, '')
|
|
|
|
excludedDirs.push(dir)
|
2021-12-26 18:25:07 +01:00
|
|
|
})
|
|
|
|
|
2022-11-29 18:55:22 +01:00
|
|
|
res.json({
|
|
|
|
directories: await this.getDirectories(global.appRoot, '/', excludedDirs)
|
|
|
|
})
|
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()
|