mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-02 13:14:07 +01:00
Allow adding files to existing upload folders and opt-in file overwrite
This commit is contained in:
1 parent
de5f80e0ff
commit
508516e1df
3 files changed
+612
-33
No files matched your search
@@ -29,7 +29,8 @@ class MiscController {
|
||||
|
||||
/**
|
||||
* POST: /api/upload
|
||||
* Upload files for a new library item. Refuses to overwrite an existing item or file.
|
||||
* Upload files for a new library item, or add files to an existing folder with update permission.
|
||||
* Refuses to overwrite existing files unless `overwrite` is true and the user has update and delete permissions.
|
||||
*
|
||||
* @param {RequestWithUser} req
|
||||
* @param {Response} res
|
||||
@@ -50,6 +51,11 @@ class MiscController {
|
||||
if (!libraryId || !folderId || typeof libraryId !== 'string' || typeof folderId !== 'string' || !title || typeof title !== 'string') {
|
||||
return res.status(400).send('Invalid request body')
|
||||
}
|
||||
const overwrite = req.body.overwrite === true || req.body.overwrite === 'true' || req.body.overwrite === '1'
|
||||
if (overwrite && (!req.user.canUpdate || !req.user.canDelete)) {
|
||||
Logger.warn(`[MiscController] User "${req.user.username}" attempted to upload with overwrite without permission`)
|
||||
return res.sendStatus(403)
|
||||
}
|
||||
if (!series || typeof series !== 'string') {
|
||||
series = null
|
||||
}
|
||||
@@ -71,11 +77,13 @@ class MiscController {
|
||||
return res.sendStatus(500)
|
||||
}
|
||||
|
||||
// A 409 means the destination already belongs to another item. createUploadDirectory
|
||||
// removes only directories this request created when a later mkdir fails.
|
||||
// A 409 means a file or parent library item conflict. createUploadDirectory removes only
|
||||
// directories this request created when a later mkdir fails.
|
||||
// The library lookup is inside the try: Express 4 does not answer a rejected async route.
|
||||
let outputDirectory
|
||||
let firstCreated = null
|
||||
let createdOutputDirectory = true
|
||||
let existingFiles = new Set()
|
||||
try {
|
||||
const library = await Database.libraryModel.findByIdWithFolders(libraryId)
|
||||
if (!library) {
|
||||
@@ -102,8 +110,12 @@ class MiscController {
|
||||
const directoryParts = [...parentParts, sanitizedTitle]
|
||||
outputDirectory = Path.join(folder.path, ...directoryParts)
|
||||
|
||||
await uploadPath.assertUploadDestination(folder.path, outputDirectory, filenames)
|
||||
firstCreated = await uploadPath.createUploadDirectory(outputDirectory)
|
||||
const destination = await uploadPath.assertUploadDestination(folder.path, outputDirectory, filenames, req.user, overwrite)
|
||||
createdOutputDirectory = !destination?.directoryExists
|
||||
existingFiles = destination?.existingFiles || new Set()
|
||||
if (createdOutputDirectory) {
|
||||
firstCreated = await uploadPath.createUploadDirectory(outputDirectory)
|
||||
}
|
||||
} catch (error) {
|
||||
if (!(error instanceof uploadPath.UploadError)) {
|
||||
Logger.error('[MiscController] Failed to upload', outputDirectory, error)
|
||||
@@ -111,16 +123,27 @@ class MiscController {
|
||||
return respond(error)
|
||||
}
|
||||
|
||||
const createdFiles = []
|
||||
try {
|
||||
Logger.info(`Uploading ${files.length} files to`, outputDirectory)
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
await files[i].mv(Path.join(outputDirectory, filenames[i]))
|
||||
const targetPath = Path.join(outputDirectory, filenames[i])
|
||||
if (!overwrite) {
|
||||
await uploadPath.reserveUploadFile(targetPath)
|
||||
createdFiles.push(targetPath)
|
||||
} else if (!existingFiles.has(filenames[i])) {
|
||||
createdFiles.push(targetPath)
|
||||
}
|
||||
await files[i].mv(targetPath)
|
||||
}
|
||||
|
||||
res.sendStatus(200)
|
||||
} catch (error) {
|
||||
Logger.error('[MiscController] Failed to upload', outputDirectory, error)
|
||||
await uploadPath.rollbackFailedUpload(outputDirectory, firstCreated)
|
||||
if (!(error instanceof uploadPath.UploadError)) {
|
||||
Logger.error('[MiscController] Failed to upload', outputDirectory, error)
|
||||
}
|
||||
await uploadPath.rollbackFailedUpload(outputDirectory, firstCreated, createdOutputDirectory, createdFiles)
|
||||
if (error instanceof uploadPath.UploadError) return respond(error)
|
||||
const notWritable = uploadPath.notWritableUploadError(error)
|
||||
if (notWritable) return respond(notWritable)
|
||||
return res.sendStatus(500)
|
||||
|
||||
Reference in new issue
Block a user