mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-02 05:04:13 +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
@@ -27,7 +27,26 @@ describe('upload path collisions', () => {
|
||||
canUpload: true,
|
||||
canUpdate: false,
|
||||
canDelete: false,
|
||||
checkCanAccessLibrary: () => true
|
||||
checkCanAccessLibrary: () => true,
|
||||
checkCanAccessLibraryItem: () => true
|
||||
}
|
||||
|
||||
const updateUser = {
|
||||
username: 'updater',
|
||||
canUpload: true,
|
||||
canUpdate: true,
|
||||
canDelete: false,
|
||||
checkCanAccessLibrary: () => true,
|
||||
checkCanAccessLibraryItem: () => true
|
||||
}
|
||||
|
||||
const deleteUser = {
|
||||
username: 'admin-delete',
|
||||
canUpload: true,
|
||||
canUpdate: true,
|
||||
canDelete: true,
|
||||
checkCanAccessLibrary: () => true,
|
||||
checkCanAccessLibraryItem: () => true
|
||||
}
|
||||
|
||||
async function pathExists(targetPath) {
|
||||
@@ -160,14 +179,14 @@ describe('upload path collisions', () => {
|
||||
expect(await pathExists(file.tempFilePath)).to.equal(false)
|
||||
})
|
||||
|
||||
it('refuses when the destination directory already exists and leaves it unchanged', async () => {
|
||||
it('refuses overwriting an existing file by default and leaves it unchanged', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(uploadRequest(), res)
|
||||
await MiscController.handleUpload(uploadRequest({ user: updateUser }), res)
|
||||
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.body).to.deep.equal({
|
||||
@@ -177,6 +196,309 @@ describe('upload path collisions', () => {
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
})
|
||||
|
||||
it('allows adding new files to an existing folder for users with update permission', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: updateUser,
|
||||
files: {
|
||||
0: fakeUploadFile('02.mp3', 'second-track')
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(200)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await fs.readFile(Path.join(destinationDir, '02.mp3'), 'utf8')).to.equal('second-track')
|
||||
})
|
||||
|
||||
it('refuses an upload-only user adding files to an existing folder', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: uploadUser,
|
||||
files: {
|
||||
0: fakeUploadFile('02.mp3', 'second-track')
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(403)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('refuses adding files to an existing library item the user cannot access', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const book = await Database.bookModel.create({
|
||||
title: 'Book B',
|
||||
tags: ['restricted'],
|
||||
explicit: false
|
||||
})
|
||||
await Database.libraryItemModel.create({
|
||||
path: filePathToPOSIX(destinationDir),
|
||||
title: 'Book B',
|
||||
mediaId: book.id,
|
||||
mediaType: 'book',
|
||||
libraryFiles: [],
|
||||
libraryId: library.id,
|
||||
libraryFolderId: folder.id
|
||||
})
|
||||
|
||||
const restrictedUpdater = {
|
||||
...updateUser,
|
||||
checkCanAccessLibraryItem: () => false
|
||||
}
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: restrictedUpdater,
|
||||
files: {
|
||||
0: fakeUploadFile('02.mp3', 'second-track')
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(403)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('refuses overwrite=true when the user lacks delete permission', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: updateUser,
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: 'true'
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(403)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
})
|
||||
|
||||
it('overwrites existing files and adds new files when overwrite is true and user has delete permission', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
files: {
|
||||
0: fakeUploadFile('01.mp3', 'replaced'),
|
||||
1: fakeUploadFile('02.mp3', 'added')
|
||||
},
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: 'true'
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(200)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('replaced')
|
||||
expect(await fs.readFile(Path.join(destinationDir, '02.mp3'), 'utf8')).to.equal('added')
|
||||
})
|
||||
|
||||
it('accepts overwrite="1" when user has delete permission', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
files: {
|
||||
0: fakeUploadFile('01.mp3', 'replaced')
|
||||
},
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: '1'
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(200)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('replaced')
|
||||
})
|
||||
|
||||
it('treats overwrite="false" as false and refuses overwriting existing files', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
files: {
|
||||
0: fakeUploadFile('02.mp3', 'added'),
|
||||
1: fakeUploadFile('01.mp3', 'replaced')
|
||||
},
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: 'false'
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.body).to.deep.equal({
|
||||
code: uploadPath.UPLOAD_ERROR_CODE.pathExists,
|
||||
error: uploadPath.UPLOAD_PATH_EXISTS_MESSAGE
|
||||
})
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('refuses when the destination path is an existing file even with overwrite=true', async () => {
|
||||
const authorDir = Path.join(folder.path, 'Author A')
|
||||
await fs.mkdir(authorDir, { recursive: true })
|
||||
const bookFile = Path.join(authorDir, 'Book B')
|
||||
await fs.writeFile(bookFile, 'not-a-directory')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: true
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.body).to.deep.equal({
|
||||
code: uploadPath.UPLOAD_ERROR_CODE.pathExists,
|
||||
error: uploadPath.UPLOAD_PATH_EXISTS_MESSAGE
|
||||
})
|
||||
expect(await fs.readFile(bookFile, 'utf8')).to.equal('not-a-directory')
|
||||
})
|
||||
|
||||
it('refuses when a target filename is an existing subdirectory even with overwrite=true', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
const conflictingDir = Path.join(destinationDir, '01.mp3')
|
||||
await fs.mkdir(conflictingDir, { recursive: true })
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: true
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.body).to.deep.equal({
|
||||
code: uploadPath.UPLOAD_ERROR_CODE.pathExists,
|
||||
error: uploadPath.UPLOAD_PATH_EXISTS_MESSAGE
|
||||
})
|
||||
expect((await fs.lstat(conflictingDir)).isDirectory()).to.equal(true)
|
||||
})
|
||||
|
||||
it('refuses a nested upload inside an existing library item even when overwrite=true and the subdirectory exists', async () => {
|
||||
const parentPath = filePathToPOSIX(Path.join(folder.path, 'Author A', 'Book B'))
|
||||
const nestedDir = Path.join(folder.path, 'Author A', 'Book B', 'Sequel')
|
||||
await fs.mkdir(nestedDir, { recursive: true })
|
||||
await Database.libraryItemModel.create({
|
||||
path: parentPath,
|
||||
title: 'Book B',
|
||||
mediaType: 'book',
|
||||
libraryFiles: [],
|
||||
libraryId: library.id,
|
||||
libraryFolderId: folder.id
|
||||
})
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
body: {
|
||||
title: 'Sequel',
|
||||
author: 'Author A',
|
||||
series: 'Book B',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: true
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.body).to.deep.equal({
|
||||
code: uploadPath.UPLOAD_ERROR_CODE.libraryItemExists,
|
||||
libraryItemTitle: 'Book B',
|
||||
error: `${uploadPath.UPLOAD_LIBRARY_ITEM_EXISTS_PREFIX}Book B`
|
||||
})
|
||||
expect(await pathExists(Path.join(nestedDir, '01.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('refuses when a parent path is a file and leaves that file unchanged', async () => {
|
||||
const authorFile = Path.join(folder.path, 'Author A')
|
||||
await fs.writeFile(authorFile, 'not-a-directory')
|
||||
@@ -577,7 +899,7 @@ describe('upload path collisions', () => {
|
||||
expect(await pathExists(Path.join(folder.path, '...'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('does not let an upload-only user overwrite an existing item', async () => {
|
||||
it('does not let an upload-only user modify or overwrite an existing item', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
await fs.writeFile(Path.join(destinationDir, '01.mp3'), 'original')
|
||||
@@ -587,8 +909,140 @@ describe('upload path collisions', () => {
|
||||
|
||||
expect(uploadUser.canUpdate).to.equal(false)
|
||||
expect(uploadUser.canDelete).to.equal(false)
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.statusCode).to.equal(403)
|
||||
expect(await fs.readFile(Path.join(destinationDir, '01.mp3'), 'utf8')).to.equal('original')
|
||||
|
||||
const overwriteRes = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: true
|
||||
}
|
||||
}),
|
||||
overwriteRes
|
||||
)
|
||||
expect(overwriteRes.statusCode).to.equal(403)
|
||||
expect(await fs.readFile(Path.join(destinationDir, '01.mp3'), 'utf8')).to.equal('original')
|
||||
})
|
||||
|
||||
it('keeps an existing folder and its files when adding a file fails', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const first = fakeUploadFile('02.mp3', 'second')
|
||||
const second = fakeUploadFile('03.mp3', 'third', { fail: true })
|
||||
await fs.writeFile(second.tempFilePath, 'third')
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: updateUser,
|
||||
files: { 0: first, 1: second }
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(500)
|
||||
expect(await pathExists(destinationDir)).to.equal(true)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
expect(await pathExists(Path.join(destinationDir, '03.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('removes only newly created files when a later overwrite move fails', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const added = fakeUploadFile('02.mp3', 'added')
|
||||
const failingOverwrite = fakeUploadFile('01.mp3', 'replaced', { fail: true })
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: deleteUser,
|
||||
files: { 0: added, 1: failingOverwrite },
|
||||
body: {
|
||||
title: 'Book B',
|
||||
author: 'Author A',
|
||||
series: '',
|
||||
library: library.id,
|
||||
folder: folder.id,
|
||||
overwrite: true
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(500)
|
||||
expect(await pathExists(destinationDir)).to.equal(true)
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('refuses with 409 and rolls back earlier new files if a file appears concurrently during upload', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
const racedFile = Path.join(destinationDir, '03.mp3')
|
||||
await fs.writeFile(racedFile, 'concurrent')
|
||||
|
||||
sinon.stub(uploadPath, 'assertUploadDestination').resolves({ directoryExists: true, existingFiles: new Set() })
|
||||
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: updateUser,
|
||||
files: {
|
||||
0: fakeUploadFile('02.mp3', 'added'),
|
||||
1: fakeUploadFile('03.mp3', 'conflict')
|
||||
}
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(409)
|
||||
expect(res.body).to.deep.equal({
|
||||
code: uploadPath.UPLOAD_ERROR_CODE.pathExists,
|
||||
error: uploadPath.UPLOAD_PATH_EXISTS_MESSAGE
|
||||
})
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await fs.readFile(racedFile, 'utf8')).to.equal('concurrent')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('answers 403 when adding a file to an existing folder is denied and leaves existing files intact', async () => {
|
||||
const destinationDir = Path.join(folder.path, 'Author A', 'Book B')
|
||||
await fs.mkdir(destinationDir, { recursive: true })
|
||||
const existing = Path.join(destinationDir, '01.mp3')
|
||||
await fs.writeFile(existing, 'original')
|
||||
|
||||
const file = fakeUploadFile('02.mp3', 'second', { fail: true, failCode: 'EACCES' })
|
||||
const res = mockRes()
|
||||
await MiscController.handleUpload(
|
||||
uploadRequest({
|
||||
user: updateUser,
|
||||
files: { 0: file }
|
||||
}),
|
||||
res
|
||||
)
|
||||
|
||||
expect(res.statusCode).to.equal(403)
|
||||
expect(res.body).to.deep.equal({
|
||||
code: uploadPath.UPLOAD_ERROR_CODE.folderNotWritable,
|
||||
error: uploadPath.UPLOAD_FOLDER_NOT_WRITABLE_MESSAGE
|
||||
})
|
||||
expect(await fs.readFile(existing, 'utf8')).to.equal('original')
|
||||
expect(await pathExists(Path.join(destinationDir, '02.mp3'))).to.equal(false)
|
||||
})
|
||||
|
||||
it('removes partial files when a move fails and does not answer 200', async () => {
|
||||
|
||||
Reference in new issue
Block a user