Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:files): harden multipart upload replacement
johavenc63f83c21723
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.tsindex 440b669c..cc298859 100644--- a/backend/src/applications/files/services/files-manager.service.spec.ts+++ b/backend/src/applications/files/services/files-manager.service.spec.ts@@ -54,7 +54,7 @@ describe(FilesManager.name, () => {removeChildLocks: jest.Mock}- const user = { id: 7, login: 'john', tasksPath: '/data/users/john/tmp/tasks' } as any+ const user = { id: 7, login: 'john', tmpPath: '/data/users/john/tmp', tasksPath: '/data/users/john/tmp/tasks' } as anyconst makeSpace = (overrides: Record<string, any> = {}) =>({@@ -237,14 +237,64 @@ describe(FilesManager.name, () => {})describe('saveMultipart', () => {+ it('should reject POST when target root already exists before reading multipart parts', async () => {+ const space = makeSpace()+ setPathExists({ [space.realPath]: true }, false)++ const req = {+ method: 'POST',+ files: jest.fn().mockImplementation(async function* () {+ yield { filename: path.basename(space.realPath), file: Readable.from(['content']) }+ })+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(+ new FileError(HttpStatus.METHOD_NOT_ALLOWED, 'Resource already exists')+ )++ expect(req.files).not.toHaveBeenCalled()+ expectNoWriteOperations()+ })++ it.each([+ {+ name: 'missing parent',+ pathExists: (space: any) => ({ [space.realPath]: false, [path.dirname(space.realPath)]: false }),+ isDir: () => true,+ expected: new FileError(HttpStatus.BAD_REQUEST, 'Parent must exists')+ },+ {+ name: 'parent file',+ pathExists: (space: any) => ({ [space.realPath]: false, [path.dirname(space.realPath)]: true }),+ isDir: () => false,+ expected: new FileError(HttpStatus.BAD_REQUEST, 'Parent must be a directory')+ }+ ])('should reject POST when target root has $name', async ({ pathExists, isDir, expected }) => {+ const space = makeSpace()+ setPathExists(pathExists(space), false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async () => isDir())++ const req = {+ method: 'POST',+ files: jest.fn().mockImplementation(async function* () {+ yield { filename: path.basename(space.realPath), file: Readable.from(['content']) }+ })+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(expected)++ expect(req.files).not.toHaveBeenCalled()+ expectNoWriteOperations()+ })+it('should write one PATCH part and emit update event', async () => {const space = makeSpace({url: 'files/personal/report.txt',realPath: '/data/users/john/files/report.txt',dbFile: { ownerId: 7, path: 'report.txt' }})- setPathExists({ [path.dirname(space.realPath)]: true, [space.realPath]: true }, false)- ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValue(true)+ setPathExists({ [path.dirname(space.realPath)]: true, [space.realPath]: true, [user.tmpPath]: true }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === path.dirname(space.realPath))const emitSpy = jest.spyOn(FileEvent, 'emit')const req = {@@ -256,10 +306,337 @@ describe(FilesManager.name, () => {await service.saveMultipart(user, space, req as any)+ const tmpWritePath = (filesUtils.writeFromStream as jest.Mock).mock.calls[0][0] as stringexpect(filesLockManager.createOrRefresh).toHaveBeenCalled()- expect(filesUtils.writeFromStream).toHaveBeenCalledWith('/data/users/john/files/report.txt', expect.anything())+ expect(tmpWritePath.startsWith(`${user.tmpPath}${path.sep}`)).toBe(true)+ expect(tmpWritePath.endsWith('-report.txt')).toBe(true)+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(tmpWritePath, expect.anything())+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(tmpWritePath, '/data/users/john/files/report.txt', true)expect(emitSpy).toHaveBeenCalledWith('event', expect.objectContaining({ action: ACTION.UPDATE, rPath: '/data/users/john/files/report.txt' }))})++ it('should reject PATCH when destination does not exist', async () => {+ const space = makeSpace({+ url: 'files/personal/report.txt',+ realPath: '/data/users/john/files/report.txt',+ dbFile: { ownerId: 7, path: 'report.txt' }+ })+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists({ [path.dirname(space.realPath)]: true, [space.realPath]: false }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === path.dirname(space.realPath))++ const req = {+ method: 'PATCH',+ files: async function* () {+ yield { filename: 'ignored-on-patch.txt', file: Readable.from(['content']) }+ }+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(new FileError(HttpStatus.NOT_FOUND, 'Location not found'))++ expect(filesUtils.writeFromStream).not.toHaveBeenCalled()+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()+ expect(filesUtils.removeFiles).not.toHaveBeenCalled()+ expect(filesLockManager.createOrRefresh).not.toHaveBeenCalled()+ expect(emitSpy).not.toHaveBeenCalled()+ })++ it('should write PUT to a temporary file before moving it to the destination', async () => {+ const space = makeSpace()+ const file = Readable.from(['content'])+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists({ [space.realPath]: true, [path.dirname(space.realPath)]: true, [user.tmpPath]: true }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === path.dirname(space.realPath))++ const req = {+ method: 'PUT',+ files: async function* () {+ yield { filename: path.basename(space.realPath), file }+ }+ }++ await service.saveMultipart(user, space, req as any)++ const tmpWritePath = (filesUtils.writeFromStream as jest.Mock).mock.calls[0][0] as string+ expect(tmpWritePath.startsWith(`${user.tmpPath}${path.sep}`)).toBe(true)+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(tmpWritePath, file)+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(tmpWritePath, space.realPath, true)+ expect(filesUtils.removeFiles).not.toHaveBeenCalled()+ expect(emitSpy).toHaveBeenCalledWith('event', expect.objectContaining({ action: ACTION.UPDATE, rPath: space.realPath }))+ })++ it('should create missing destination directory and release created lock for POST nested upload', async () => {+ const space = makeSpace()+ const partFileName = 'folder/file.txt'+ const dstDir = path.join(path.dirname(space.realPath), 'folder')+ const dstFile = path.join(dstDir, 'file.txt')+ const file = Readable.from(['content'])+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists(+ {+ [space.realPath]: false,+ [path.dirname(space.realPath)]: true,+ [dstDir]: false,+ [dstFile]: false+ },+ false+ )+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === path.dirname(space.realPath))+ filesLockManager.createOrRefresh.mockResolvedValueOnce([true, { key: 'lock-created' }])++ const req = {+ method: 'POST',+ files: async function* () {+ yield { filename: partFileName, file }+ }+ }++ await service.saveMultipart(user, space, req as any)++ expect(filesUtils.makeDir).toHaveBeenCalledWith(dstDir, true)+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(dstFile, file)+ expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-created')+ expect(emitSpy).toHaveBeenCalledWith('event', expect.objectContaining({ action: ACTION.ADD, rPath: dstFile }))+ })++ it('should reject POST when resolved multipart destination already exists', async () => {+ const space = makeSpace()+ const partFileName = 'folder/file.txt'+ const dstDir = path.join(path.dirname(space.realPath), 'folder')+ const dstFile = path.join(dstDir, 'file.txt')+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists(+ {+ [space.realPath]: false,+ [path.dirname(space.realPath)]: true,+ [dstDir]: true,+ [dstFile]: true+ },+ false+ )+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === path.dirname(space.realPath) || p === dstDir)++ const req = {+ method: 'POST',+ files: async function* () {+ yield { filename: partFileName, file: Readable.from(['content']) }+ }+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(+ new FileError(HttpStatus.METHOD_NOT_ALLOWED, 'Resource already exists')+ )++ expect(filesUtils.writeFromStream).not.toHaveBeenCalled()+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()+ expect(filesUtils.removeFiles).not.toHaveBeenCalled()+ expect(filesLockManager.createOrRefresh).not.toHaveBeenCalled()+ expect(emitSpy).not.toHaveBeenCalled()+ })++ it('should reject multipart path traversal before checking destination path', async () => {+ const space = makeSpace()+ const parentPath = path.dirname(space.realPath)+ const forbiddenFile = path.resolve(`${parentPath}${path.sep}`, '../escape.txt')+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists({ [space.realPath]: false, [parentPath]: true }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === parentPath)++ const req = {+ method: 'POST',+ files: async function* () {+ yield { filename: '../escape.txt', file: Readable.from(['content']) }+ }+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(new FileError(HttpStatus.FORBIDDEN, 'Location is not allowed'))++ expect(filesUtils.isPathExists).not.toHaveBeenCalledWith(forbiddenFile)+ expect(filesUtils.isPathIsDir).not.toHaveBeenCalledWith(forbiddenFile)+ expect(filesUtils.writeFromStream).not.toHaveBeenCalled()+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()+ expect(filesLockManager.createOrRefresh).not.toHaveBeenCalled()+ expect(emitSpy).not.toHaveBeenCalled()+ })++ it('should keep existing destination untouched when PUT upload is too large', async () => {+ const space = makeSpace()+ const file = Readable.from(['content']) as Readable & { truncated: boolean }+ file.truncated = true+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists({ [space.realPath]: true, [path.dirname(space.realPath)]: true, [user.tmpPath]: true }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => p === path.dirname(space.realPath))++ const req = {+ method: 'PUT',+ files: async function* () {+ yield { filename: path.basename(space.realPath), file }+ }+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(+ new FileError(HttpStatus.PAYLOAD_TOO_LARGE, 'File size limit exceeded')+ )++ const tmpWritePath = (filesUtils.writeFromStream as jest.Mock).mock.calls[0][0] as string+ expect(tmpWritePath.startsWith(`${user.tmpPath}${path.sep}`)).toBe(true)+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(tmpWritePath, file)+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(tmpWritePath)+ expect(filesUtils.removeFiles).not.toHaveBeenCalledWith(space.realPath)+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()+ expect(emitSpy).not.toHaveBeenCalled()+ })++ it('should reject truncated multipart file as payload too large', async () => {+ const space = makeSpace()+ const dstFile = '/data/users/john/files/too-big.bin'+ const file = Readable.from(['content']) as Readable & { truncated: boolean }+ file.truncated = true+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists({ [space.realPath]: false, [path.dirname(space.realPath)]: true, [dstFile]: false }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValue(true)++ const req = {+ method: 'POST',+ files: jest.fn().mockImplementation(async function* () {+ yield { filename: 'too-big.bin', file }+ })+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(+ new FileError(HttpStatus.PAYLOAD_TOO_LARGE, 'File size limit exceeded')+ )++ expect(req.files).toHaveBeenCalled()+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(dstFile, file)+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(dstFile)+ expect(emitSpy).not.toHaveBeenCalled()+ })++ it('should map multipart iterator file size errors to payload too large', async () => {+ const space = makeSpace()+ const error = Object.assign(new Error('request file too large'), { code: 'FST_REQ_FILE_TOO_LARGE', statusCode: HttpStatus.PAYLOAD_TOO_LARGE })+ setPathExists({ [space.realPath]: false, [path.dirname(space.realPath)]: true }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValue(true)++ const req = {+ method: 'POST',+ files: jest.fn().mockImplementation(async function* () {+ yield* []+ throw error+ })+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toEqual(+ new FileError(HttpStatus.PAYLOAD_TOO_LARGE, 'File size limit exceeded')+ )++ expect(filesUtils.writeFromStream).not.toHaveBeenCalled()+ })++ it('should not map non-file-size multipart 413 errors to file size limit', async () => {+ const space = makeSpace()+ const error = Object.assign(new Error('reach parts limit'), { code: 'FST_PARTS_LIMIT', statusCode: HttpStatus.PAYLOAD_TOO_LARGE })+ setPathExists({ [space.realPath]: false, [path.dirname(space.realPath)]: true }, false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockResolvedValue(true)++ const req = {+ method: 'POST',+ files: jest.fn().mockImplementation(async function* () {+ yield* []+ throw error+ })+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toBe(error)++ expect(filesUtils.writeFromStream).not.toHaveBeenCalled()+ })++ it.each([+ {+ name: 'existing destination directory',+ partFileName: 'file.txt',+ pathExists: (space: any) => ({+ [space.realPath]: true,+ [path.dirname(space.realPath)]: true,+ [user.tmpPath]: true+ }),+ isDir: (space: any, p: string) => p === space.realPath || p === path.dirname(space.realPath)+ },+ {+ name: 'destination parent file',+ partFileName: 'folder/file.txt',+ pathExists: (space: any) => ({+ [path.join(path.dirname(space.realPath), 'folder')]: true,+ [user.tmpPath]: true+ }),+ isDir: () => false+ }+ ])('should cleanup temporary file when PUT move fails after deleting $name', async ({ partFileName, pathExists, isDir }) => {+ const space = makeSpace()+ const file = Readable.from(['content'])+ const error = new Error('move failed')+ const deleteSpy = jest.spyOn(service, 'delete').mockResolvedValue(undefined)+ const emitSpy = jest.spyOn(FileEvent, 'emit')+ setPathExists(pathExists(space), false)+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async (p: string) => isDir(space, p))+ ;(filesUtils.moveFiles as jest.Mock).mockRejectedValueOnce(error)++ const req = {+ method: 'PUT',+ files: async function* () {+ yield { filename: partFileName, file }+ }+ }++ await expect(service.saveMultipart(user, space, req as any)).rejects.toBe(error)++ const tmpWritePath = (filesUtils.writeFromStream as jest.Mock).mock.calls[0][0] as string+ expect(filesUtils.writeFromStream).toHaveBeenCalledWith(tmpWritePath, file)+ expect(deleteSpy).toHaveBeenCalledTimes(1)+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(tmpWritePath, expect.stringContaining(path.basename(partFileName)), true)+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(tmpWritePath)+ expect(emitSpy).not.toHaveBeenCalled()+ expect((filesUtils.writeFromStream as jest.Mock).mock.invocationCallOrder[0]).toBeLessThan(deleteSpy.mock.invocationCallOrder[0])+ expect(deleteSpy.mock.invocationCallOrder[0]).toBeLessThan((filesUtils.moveFiles as jest.Mock).mock.invocationCallOrder[0])+ })++ it('should recreate destination directory after deleting a parent file before moving PUT tmp file', async () => {+ const space = makeSpace()+ const partFileName = 'folder/file.txt'+ const dstDir = path.join(path.dirname(space.realPath), 'folder')+ const dstFile = path.join(dstDir, 'file.txt')+ const file = Readable.from(['content'])+ const deleteSpy = jest.spyOn(service, 'delete').mockResolvedValue(undefined)+ let dstDirExistsChecks = 0+ ;(filesUtils.isPathExists as jest.Mock).mockImplementation(async (p: string) => {+ if (p === dstDir) {+ dstDirExistsChecks+++ return dstDirExistsChecks === 1+ }+ return false+ })+ ;(filesUtils.isPathIsDir as jest.Mock).mockImplementation(async () => false)++ const req = {+ method: 'PUT',+ files: async function* () {+ yield { filename: partFileName, file }+ }+ }++ await service.saveMultipart(user, space, req as any)++ const tmpWritePath = (filesUtils.writeFromStream as jest.Mock).mock.calls[0][0] as string+ expect(deleteSpy).toHaveBeenCalledTimes(1)+ expect(filesUtils.makeDir).toHaveBeenCalledWith(dstDir, true)+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(tmpWritePath, dstFile, true)+ expect(deleteSpy.mock.invocationCallOrder[0]).toBeLessThan((filesUtils.makeDir as jest.Mock).mock.invocationCallOrder[0])+ expect((filesUtils.makeDir as jest.Mock).mock.invocationCallOrder[0]).toBeLessThan(+ (filesUtils.moveFiles as jest.Mock).mock.invocationCallOrder[0]+ )+ })})describe('touch', () => {diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.tsindex 866c2a13..c6054cbf 100644--- a/backend/src/applications/files/services/files-manager.service.ts+++ b/backend/src/applications/files/services/files-manager.service.ts@@ -61,6 +61,7 @@ import { FilesQueries } from './files-queries.service'import { FileEvent, FileTaskEvent } from '../events/file-events'import { ACTION } from '../../../common/constants'import { pipeline } from 'node:stream/promises'+import { isMultipartFileTooLargeError, maxUploadSizeExceededError, uploadTmpFilePath } from '../utils/upload-file'@Injectable()export class FilesManager {@@ -195,13 +196,15 @@ export class FilesManager {*/this.checkNotTrashRepository(space)const overwrite = req.method === HTTP_METHOD.PUT- const patch = req.method === HTTP_METHOD.PATCH+ const patchMethod = req.method === HTTP_METHOD.PATCH+ const postMethod = req.method === HTTP_METHOD.POSTconst realParentPath = dirName(space.realPath)+ // For POST, space.realPath can be either the final file path or the root directory for a folder upload.+ if (postMethod && (await isPathExists(space.realPath))) {+ throw new FileError(HttpStatus.METHOD_NOT_ALLOWED, 'Resource already exists')+ }if (!overwrite) {- if (!patch && (await isPathExists(space.realPath))) {- throw new FileError(HttpStatus.BAD_REQUEST, 'Resource already exists')- }if (!(await isPathExists(realParentPath))) {throw new FileError(HttpStatus.BAD_REQUEST, 'Parent must exists')}@@ -212,60 +215,111 @@ export class FilesManager {const basePath = realParentPath + path.sep- for await (const part of req.files()) {- // If the request uses the PATCH method, the file name corresponds to the space- const partFileName = patch ? fileName(space.realPath) : part.filename- // `part.filename` may contain a path like foo/bar.txt- const dstFile = path.resolve(basePath, partFileName)- const dstExists = await isPathExists(dstFile)- const dstIsDir = dstExists ? await isPathIsDir(dstFile) : false- // Prevent path traversal- if (!dstFile.startsWith(basePath)) {- throw new FileError(HttpStatus.FORBIDDEN, 'Location is not allowed')- }+ try {+ for await (const part of req.files({ throwFileSizeLimit: false })) {+ // If the request uses the PATCH method, the file name corresponds to the space+ const partFileName = patchMethod ? fileName(space.realPath) : part.filename+ // `part.filename` may contain a path like foo/bar.txt+ const dstFile = path.resolve(basePath, partFileName)+ // Prevent path traversal+ if (!dstFile.startsWith(basePath)) {+ throw new FileError(HttpStatus.FORBIDDEN, 'Location is not allowed')+ }+ const dstExists = await isPathExists(dstFile)+ const dstIsDir = dstExists ? await isPathIsDir(dstFile) : false+ if (postMethod && dstExists) {+ throw new FileError(HttpStatus.METHOD_NOT_ALLOWED, 'Resource already exists')+ }+ if (patchMethod && !dstExists) {+ throw new FileError(HttpStatus.NOT_FOUND, 'Location not found')+ }+ // PUT/PATCH write outside the destination first, so a failed upload does not corrupt an existing file.+ const tmpFile = overwrite || patchMethod ? uploadTmpFilePath(user.tmpPath, partFileName) : undefined+ const writePath = tmpFile || dstFile++ const dstDir = dirName(dstFile)+ // For overwrite conflicts, defer destructive deletes until the upload stream is fully validated.+ let dstSpaceToDeleteBeforeMove: SpaceEnv | undefined+ let dstParentSpaceToDeleteBeforeMove: SpaceEnv | undefined++ if (overwrite) {+ // Prevent errors when an uploaded file would replace a directory with the same name+ // Only applies in `overwrite` cases+ if (dstExists && dstIsDir) {+ // If a directory already exists at the destination path, delete it to allow overwriting with the uploaded file+ const dstUrl = path.join(path.dirname(space.url), partFileName)+ dstSpaceToDeleteBeforeMove = await this.spacesManager.spaceEnv(user, dstUrl.split('/'))+ } else if ((await isPathExists(dstDir)) && !(await isPathIsDir(dstDir))) {+ // If the destination's parent exists but is a file, remove it so we can create the directory+ const dstUrl = path.join(path.dirname(space.url), path.dirname(partFileName))+ dstParentSpaceToDeleteBeforeMove = await this.spacesManager.spaceEnv(user, dstUrl.split('/'))+ }+ }+ // Create the destination directory only when writing directly; user.tmpPath already exists.+ if (!tmpFile && !(await isPathExists(dstDir))) {+ await makeDir(dstDir, true)+ }++ // Create or refresh lock+ const dbFile = { ...space.dbFile, path: path.join(dirName(space.dbFile.path), partFileName) }+ // Use a short TTL for the PATCH method (which is also used for refreshing)+ const ttl = patchMethod ? CACHE_LOCK_FILE_TTL : undefined+ const [created, fileLock] = await this.filesLockManager.createOrRefresh(user, dbFile, SERVER_NAME, DEPTH.RESOURCE, ttl)- const dstDir = dirName(dstFile)-- if (overwrite) {- // Prevent errors when an uploaded file would replace a directory with the same name- // Only applies in `overwrite` cases- if (dstExists && dstIsDir) {- // If a directory already exists at the destination path, delete it to allow overwriting with the uploaded file- const dstUrl = path.join(path.dirname(space.url), partFileName)- const dstSpace = await this.spacesManager.spaceEnv(user, dstUrl.split('/'))- await this.delete(user, dstSpace)- } else if ((await isPathExists(dstDir)) && !(await isPathIsDir(dstDir))) {- // If the destination's parent exists but is a file, remove it so we can create the directory- const dstUrl = path.join(path.dirname(space.url), path.dirname(partFileName))- const dstSpace = await this.spacesManager.spaceEnv(user, dstUrl.split('/'))- await this.delete(user, dstSpace)+ let fileWritten = false+ // Do+ try {+ await writeFromStream(writePath, part.file)+ // With throwFileSizeLimit disabled, multipart marks the file stream as truncated instead of rejecting.+ if (part.file.truncated) {+ throw maxUploadSizeExceededError()+ }+ if (tmpFile) {+ // If the following move fails after these deletes, the previous resources remain recoverable from the trash.+ if (dstSpaceToDeleteBeforeMove) {+ await this.delete(user, dstSpaceToDeleteBeforeMove)+ }+ if (dstParentSpaceToDeleteBeforeMove) {+ await this.delete(user, dstParentSpaceToDeleteBeforeMove)+ }+ if (!(await isPathExists(dstDir))) {+ await makeDir(dstDir, true)+ }+ await moveFiles(tmpFile, dstFile, true)+ }+ fileWritten = true+ } catch (e) {+ // Failed temporary uploads are discarded without touching the existing destination.+ if (tmpFile) {+ await removeFiles(tmpFile)+ } else if (!dstExists) {+ await removeFiles(dstFile)+ }+ if (isMultipartFileTooLargeError(e)) {+ throw maxUploadSizeExceededError()+ }+ throw e+ } finally {+ if (fileWritten) {+ // Emit only after the final destination has been written or moved into place.+ const fileEventAction: ACTION = patchMethod || (dstExists && !dstIsDir) ? ACTION.UPDATE : ACTION.ADD+ FileEvent.emit('event', { user, space, action: fileEventAction, rPath: dstFile })+ }+ if (!patchMethod && created) {+ // Remove the file lock only if it has not been refreshed+ await this.filesLockManager.removeLock(fileLock.key)+ }}- }- // Create the directory in the space- if (!(await isPathExists(dstDir))) {- await makeDir(dstDir, true)- }- // Create or refresh lock- const dbFile = { ...space.dbFile, path: path.join(dirName(space.dbFile.path), partFileName) }- // Use a short TTL for the PATCH method (which is also used for refreshing)- const ttl = patch ? CACHE_LOCK_FILE_TTL : undefined- const [created, fileLock] = await this.filesLockManager.createOrRefresh(user, dbFile, SERVER_NAME, DEPTH.RESOURCE, ttl)- // Do- try {- await writeFromStream(dstFile, part.file)- } finally {- // emit file event- const fileEventAction: ACTION = patch || (dstExists && !dstIsDir) ? ACTION.UPDATE : ACTION.ADD- FileEvent.emit('event', { user, space, action: fileEventAction, rPath: dstFile })- if (!patch && created) {- // Remove the file lock only if it has not been refreshed- await this.filesLockManager.removeLock(fileLock.key)+ if (patchMethod) {+ // Only one resource can be updated with the PATCH method.+ break}}- if (patch) {- // Only one resource can be updated with the PATCH method.- break+ } catch (e) {+ if (isMultipartFileTooLargeError(e)) {+ throw maxUploadSizeExceededError()}+ throw e}}diff --git a/backend/src/applications/files/utils/upload-file.ts b/backend/src/applications/files/utils/upload-file.tsnew file mode 100644index 00000000..46f80c07--- /dev/null+++ b/backend/src/applications/files/utils/upload-file.ts@@ -0,0 +1,21 @@+import path from 'node:path'+import { randomUUID } from 'node:crypto'+import { FileError } from '../models/file-error'+import { HttpStatus } from '@nestjs/common'+import { fileName } from './files'++const FASTIFY_MULTIPART_FILE_TOO_LARGE_CODE = 'FST_REQ_FILE_TOO_LARGE' as const+const MAX_UPLOAD_SIZE_EXCEEDED = 'File size limit exceeded' as const++export function isMultipartFileTooLargeError(e: any): boolean {+ // Other multipart limits also return 413; only this code means the file-size limit was reached.+ return e?.code === FASTIFY_MULTIPART_FILE_TOO_LARGE_CODE+}++export function maxUploadSizeExceededError(): FileError {+ return new FileError(HttpStatus.PAYLOAD_TOO_LARGE, MAX_UPLOAD_SIZE_EXCEEDED)+}++export function uploadTmpFilePath(tmpPath: string, partFileName: string): string {+ return path.join(tmpPath, `${randomUUID()}-upload-${fileName(partFileName) || 'file'}`)+}diff --git a/environment/environment.dist.yaml b/environment/environment.dist.yamlindex f51d6b0d..317b8fb0 100755--- a/environment/environment.dist.yaml+++ b/environment/environment.dist.yaml@@ -313,6 +313,7 @@ applications:files:# requireddataPath: /home/sync-in+ # maxUploadSize: Maximum upload file size.# default: 5368709120 (5 GB)maxUploadSize: 5368709120contentIndexing:diff --git a/frontend/src/i18n/de.json b/frontend/src/i18n/de.jsonindex dce3d081..51bba043 100644--- a/frontend/src/i18n/de.json+++ b/frontend/src/i18n/de.json@@ -130,6 +130,7 @@"Storage Usage": "Speichernutzung","Space status": "Status des Bereichs","Storage quota exceeded": "Speicherkontingent überschritten",+ "File size limit exceeded": "Dateigrößenlimit überschritten","The trash is read-only": "Der Papierkorb ist schreibgeschützt","Storage quota will be exceeded": "Speicherkontingent wird überschritten","No more space available": "Kein Speicherplatz mehr verfügbar",diff --git a/frontend/src/i18n/es.json b/frontend/src/i18n/es.jsonindex 2af701e6..529bbfb6 100644--- a/frontend/src/i18n/es.json+++ b/frontend/src/i18n/es.json@@ -130,6 +130,7 @@"Storage Usage": "Uso de almacenamiento","Space status": "Estado del espacio","Storage quota exceeded": "Se ha superado la cuota de almacenamiento",+ "File size limit exceeded": "Se ha superado el límite de tamaño del archivo","The trash is read-only": "La papelera es de solo lectura","Storage quota will be exceeded": "Se superará la cuota de almacenamiento","No more space available": "No hay más espacio disponible",diff --git a/frontend/src/i18n/fr.json b/frontend/src/i18n/fr.jsonindex e308a6f6..65cb9f99 100644--- a/frontend/src/i18n/fr.json+++ b/frontend/src/i18n/fr.json@@ -130,6 +130,7 @@"Storage Usage": "Utilisation du stockage","Space status": "État de l'espace","Storage quota exceeded": "Le quota de stockage est dépassé",+ "File size limit exceeded": "Limite de taille du fichier dépassée","The trash is read-only": "La corbeille est en lecture seule","Storage quota will be exceeded": "Le quota de stockage sera dépassé","No more space available": "Plus d'espace disponible",diff --git a/frontend/src/i18n/hi.json b/frontend/src/i18n/hi.jsonindex a2ca4b67..3108633f 100644--- a/frontend/src/i18n/hi.json+++ b/frontend/src/i18n/hi.json@@ -130,6 +130,7 @@"Storage Usage": "भंडारण उपयोग","Space status": "स्पेस स्थिति","Storage quota exceeded": "भंडारण कोटा पार हो गया",+ "File size limit exceeded": "फ़ाइल आकार की सीमा पार हो गई","The trash is read-only": "ट्रैश केवल-पढ़ने योग्य है","Storage quota will be exceeded": "भंडारण कोटा पार हो जाएगा","No more space available": "कोई अतिरिक्त स्थान उपलब्ध नहीं",diff --git a/frontend/src/i18n/it.json b/frontend/src/i18n/it.jsonindex ca6530e1..014d6470 100644--- a/frontend/src/i18n/it.json+++ b/frontend/src/i18n/it.json@@ -130,6 +130,7 @@"Storage Usage": "Utilizzo dell’archiviazione","Space status": "Stato dello spazio","Storage quota exceeded": "Quota di archiviazione superata",+ "File size limit exceeded": "Limite di dimensione del file superato","The trash is read-only": "Il cestino è di sola lettura","Storage quota will be exceeded": "La quota di archiviazione verrà superata","No more space available": "Nessuno spazio disponibile",diff --git a/frontend/src/i18n/ja.json b/frontend/src/i18n/ja.jsonindex f3709aaa..6adbc15a 100644--- a/frontend/src/i18n/ja.json+++ b/frontend/src/i18n/ja.json@@ -130,6 +130,7 @@"Storage Usage": "ストレージ使用量","Space status": "スペースの状態","Storage quota exceeded": "ストレージの割り当てを超えています",+ "File size limit exceeded": "ファイルサイズの上限を超えています","The trash is read-only": "ゴミ箱は読み取り専用です","Storage quota will be exceeded": "ストレージの割り当てを超過する見込みです","No more space available": "空き容量がありません",diff --git a/frontend/src/i18n/ko.json b/frontend/src/i18n/ko.jsonindex be3e4c38..94afd382 100644--- a/frontend/src/i18n/ko.json+++ b/frontend/src/i18n/ko.json@@ -130,6 +130,7 @@"Storage Usage": "저장 사용량","Space status": "스페이스 상태","Storage quota exceeded": "저장 할당량을 초과했습니다",+ "File size limit exceeded": "파일 크기 제한을 초과했습니다","The trash is read-only": "휴지통은 읽기 전용입니다","Storage quota will be exceeded": "저장 할당량을 초과할 예정입니다","No more space available": "사용 가능한 공간이 없습니다",diff --git a/frontend/src/i18n/nl.json b/frontend/src/i18n/nl.jsonindex 3d25a347..372bcf9b 100644--- a/frontend/src/i18n/nl.json+++ b/frontend/src/i18n/nl.json@@ -130,6 +130,7 @@"Storage Usage": "Opslaggebruik","Space status": "Opslag-status","Storage quota exceeded": "Opslagquotum overschreden",+ "File size limit exceeded": "Limiet voor bestandsgrootte overschreden","The trash is read-only": "De prullenbak is alleen-lezen","Storage quota will be exceeded": "Opslagquotum wordt overschreden","No more space available": "Geen opslagruimte meer beschikbaar",diff --git a/frontend/src/i18n/pl.json b/frontend/src/i18n/pl.jsonindex ceb12170..92968a06 100644--- a/frontend/src/i18n/pl.json+++ b/frontend/src/i18n/pl.json@@ -130,6 +130,7 @@"Storage Usage": "Wykorzystanie przestrzeni","Space status": "Status przestrzeni","Storage quota exceeded": "Przekroczono limit przestrzeni",+ "File size limit exceeded": "Przekroczono limit rozmiaru pliku","The trash is read-only": "Kosz jest tylko do odczytu","Storage quota will be exceeded": "Limit przestrzeni zostanie przekroczony","No more space available": "Brak dostępnego miejsca",diff --git a/frontend/src/i18n/pt-BR.json b/frontend/src/i18n/pt-BR.jsonindex 4a88cb98..c71f9d90 100644--- a/frontend/src/i18n/pt-BR.json+++ b/frontend/src/i18n/pt-BR.json@@ -130,6 +130,7 @@"Storage Usage": "Uso do armazenamento","Space status": "Status do espaço","Storage quota exceeded": "Cota de armazenamento excedida",+ "File size limit exceeded": "Limite de tamanho do arquivo excedido","The trash is read-only": "A lixeira é somente leitura","Storage quota will be exceeded": "A cota de armazenamento será excedida","No more space available": "Sem espaço disponível",diff --git a/frontend/src/i18n/pt.json b/frontend/src/i18n/pt.jsonindex 4674b832..64c4df0d 100644--- a/frontend/src/i18n/pt.json+++ b/frontend/src/i18n/pt.json@@ -130,6 +130,7 @@"Storage Usage": "Uso do armazenamento","Space status": "Estado do espaço","Storage quota exceeded": "A cota de armazenamento foi excedida",+ "File size limit exceeded": "Limite de tamanho do ficheiro excedido","The trash is read-only": "A lixeira é apenas de leitura","Storage quota will be exceeded": "A cota de armazenamento será excedida","No more space available": "Sem mais espaço disponível",diff --git a/frontend/src/i18n/ru.json b/frontend/src/i18n/ru.jsonindex b3bc5795..13d2204f 100644--- a/frontend/src/i18n/ru.json+++ b/frontend/src/i18n/ru.json@@ -130,6 +130,7 @@"Storage Usage": "Использование хранилища","Space status": "Статус пространства","Storage quota exceeded": "Превышена квота хранилища",+ "File size limit exceeded": "Превышен лимит размера файла","The trash is read-only": "Корзина доступна только для чтения","Storage quota will be exceeded": "Квота хранилища будет превышена","No more space available": "Место закончилось",diff --git a/frontend/src/i18n/tr.json b/frontend/src/i18n/tr.jsonindex a4de6fdc..510508dd 100644--- a/frontend/src/i18n/tr.json+++ b/frontend/src/i18n/tr.json@@ -130,6 +130,7 @@"Storage Usage": "Depolama Kullanımı","Space status": "Alan durumu","Storage quota exceeded": "Depolama kotası aşıldı",+ "File size limit exceeded": "Dosya boyutu sınırı aşıldı","The trash is read-only": "Çöp kutusu salt okunurdur","Storage quota will be exceeded": "Depolama kotası aşılacak","No more space available": "Boş alan kalmadı",diff --git a/frontend/src/i18n/zh.json b/frontend/src/i18n/zh.jsonindex fcfa05e7..f197d90a 100644--- a/frontend/src/i18n/zh.json+++ b/frontend/src/i18n/zh.json@@ -130,6 +130,7 @@"Storage Usage": "存储使用情况","Space status": "空间状态","Storage quota exceeded": "存储配额已超出",+ "File size limit exceeded": "已超出文件大小限制","The trash is read-only": "回收站是只读的","Storage quota will be exceeded": "存储配额将被超出","No more space available": "没有更多可用空间",