mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-06-23 01:18:59 +02:00
147 lines
5.4 KiB
JavaScript
147 lines
5.4 KiB
JavaScript
const { Request, Response } = require('express')
|
|
const Path = require('path')
|
|
const Logger = require('../Logger')
|
|
const fs = require('../libs/fsExtra')
|
|
const { toNumber } = require('../utils/index')
|
|
const fileUtils = require('../utils/fileUtils')
|
|
const Database = require('../Database')
|
|
const { validatePathExists } = require('../utils/fileUtils')
|
|
|
|
/**
|
|
* @typedef RequestUserObject
|
|
* @property {import('../models/User')} user
|
|
*
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
|
*/
|
|
|
|
class FileSystemController {
|
|
constructor() {}
|
|
|
|
/**
|
|
*
|
|
* @param {RequestWithUser} req
|
|
* @param {Response} res
|
|
*/
|
|
async getPaths(req, res) {
|
|
if (!req.user.isAdminOrUp) {
|
|
Logger.error(`[FileSystemController] Non-admin user "${req.user.username}" attempting to get filesystem paths`)
|
|
return res.sendStatus(403)
|
|
}
|
|
|
|
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 = []
|
|
|
|
// 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)
|
|
})
|
|
|
|
res.json({
|
|
posix: !global.isWin,
|
|
directories
|
|
})
|
|
}
|
|
|
|
/**
|
|
* POST: /api/filesystem/pathexists
|
|
*
|
|
* @param {RequestWithUser} req
|
|
* @param {Response} res
|
|
*/
|
|
async checkPathExists(req, res) {
|
|
if (!req.user.canUpload) {
|
|
Logger.error(`[FileSystemController] User "${req.user.username}" without upload permissions attempting to check path exists`)
|
|
return res.sendStatus(403)
|
|
}
|
|
|
|
// filenames - If filenames is provided, the check only returns true if the actual files exist, not just the directory
|
|
// allowBookFiles - If true, allows containing other book related files (e.g. .pdf, .epub, etc.)
|
|
// allowAudioFiles - If true, allows containing other audio related files (e.g. .mp3, .m4b, etc.)
|
|
const { directory, folderPath, filenames, allowBookFiles, allowAudioFiles } = req.body
|
|
|
|
if (!directory?.length || typeof directory !== 'string' || !folderPath?.length || typeof folderPath !== 'string') {
|
|
Logger.error(`[FileSystemController] Invalid request body: ${JSON.stringify(req.body)}`)
|
|
return res.status(400).json({
|
|
error: 'Invalid request body'
|
|
})
|
|
}
|
|
|
|
// Validate filenames: must be undefined or an array of non-empty strings
|
|
if (filenames !== undefined && (!Array.isArray(filenames) || filenames.some((f) => typeof f !== 'string' || f.trim().length === 0))) {
|
|
Logger.error(`[FileSystemController] Invalid filenames in request body: ${JSON.stringify(req.body)}`)
|
|
return res.status(400).json({
|
|
error: 'Invalid filenames'
|
|
})
|
|
}
|
|
|
|
if ((allowBookFiles && typeof allowBookFiles !== 'boolean') || (allowAudioFiles && typeof allowAudioFiles !== 'boolean') || (allowBookFiles && allowAudioFiles)) {
|
|
Logger.error(`[FileSystemController] Invalid allowBookFiles or allowAudioFiles in request body: ${JSON.stringify(req.body)}`)
|
|
return res.status(400).json({
|
|
error: 'Invalid allowBookFiles or allowAudioFiles'
|
|
})
|
|
}
|
|
|
|
// Check that library folder exists
|
|
const libraryFolder = await Database.libraryFolderModel.findOne({
|
|
where: {
|
|
path: folderPath
|
|
}
|
|
})
|
|
|
|
if (!libraryFolder) {
|
|
Logger.error(`[FileSystemController] Library folder not found: ${folderPath}`)
|
|
return res.sendStatus(404)
|
|
}
|
|
|
|
if (!req.user.checkCanAccessLibrary(libraryFolder.libraryId)) {
|
|
Logger.error(`[FileSystemController] User "${req.user.username}" attempting to check path exists for library "${libraryFolder.libraryId}" without access`)
|
|
return res.sendStatus(403)
|
|
}
|
|
|
|
const result = await validatePathExists(libraryFolder, directory, filenames, allowBookFiles, allowAudioFiles)
|
|
|
|
if (!result) return res.status(400).end()
|
|
|
|
console.log(`[FileSystemController] Path exists check for "${directory}" in library "${libraryFolder.libraryId}" with filenames "${Array.isArray(filenames) ? filenames.join(', ') : 'N/A'}", allowBookFiles: ${allowBookFiles}, allowAudioFiles: ${allowAudioFiles} - Result: ${result.exists}`)
|
|
|
|
return res.json(result)
|
|
}
|
|
}
|
|
|
|
module.exports = new FileSystemController()
|