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): make dated trash collisions unique
johavenba2e8f891f6e
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.tsindex 149097b6..1e535194 100644--- a/backend/src/applications/files/services/files-manager.service.spec.ts+++ b/backend/src/applications/files/services/files-manager.service.spec.ts@@ -1156,7 +1156,7 @@ describe(FilesManager.name, () => {await service.copyMove(user, src, dst, true, true)- expect(filesUtils.uniqueDatedFilePath).toHaveBeenCalledWith(dst.realPath)+ expect(filesUtils.uniqueDatedFilePath).toHaveBeenCalledWith(src.realPath, false)expect(filesUtils.moveFiles).toHaveBeenCalledTimes(2)expect(filesUtils.moveFiles).toHaveBeenNthCalledWith(1, dst.realPath, datedTrashFile, true)expect(filesUtils.moveFiles).toHaveBeenNthCalledWith(2, src.realPath, dst.realPath, true)diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.tsindex d6059890..e1259234 100644--- a/backend/src/applications/files/services/files-manager.service.ts+++ b/backend/src/applications/files/services/files-manager.service.ts@@ -588,8 +588,8 @@ export class FilesManager {// During an overwrite from trash, this path can be the active copy/move source.// Keep that source stable and archive the overwritten destination under a dated name instead.if (options?.protectedTrashPath && path.resolve(options.protectedTrashPath) === path.resolve(trashFile)) {- const datedDestination = await uniqueDatedFilePath(space.realPath)- trashFile = path.join(trashDir, fileName(datedDestination.path))+ const datedDestination = await uniqueDatedFilePath(trashFile, isDir)+ trashFile = datedDestination.pathdestinationDbFile = { ...destinationDbFile, path: path.join(dirName(destinationDbFile.path), fileName(trashFile)) }}trashDbFile = destinationDbFilediff --git a/backend/src/applications/files/utils/files.spec.ts b/backend/src/applications/files/utils/files.spec.tsindex 03c5239a..8fed7238 100644--- a/backend/src/applications/files/utils/files.spec.ts+++ b/backend/src/applications/files/utils/files.spec.ts@@ -17,6 +17,7 @@ import {temporaryFileName,temporaryFilePath,temporaryFilePrefix,+ uniqueDatedFilePath,writeFromStream,writeFromStreamAndChecksum,writeUploadFromStream,@@ -101,6 +102,54 @@ describe(isCrossDevice.name, () => {})})+describe(uniqueDatedFilePath.name, () => {+ let tmpDir: string++ beforeEach(async () => {+ vi.useFakeTimers()+ vi.setSystemTime(new Date('2026-08-16T10:20:30.123Z'))+ tmpDir = await mkdtemp(path.join(os.tmpdir(), 'unique-dated-path-'))+ })++ afterEach(async () => {+ vi.useRealTimers()+ await rm(tmpDir, { recursive: true, force: true })+ })++ it('increments an existing dated file name without changing its extension', async () => {+ const source = path.join(tmpDir, 'report.txt')+ const dated = path.join(tmpDir, 'report-2026.08.16 10-20-30.123.txt')+ const firstCollision = path.join(tmpDir, 'report-2026.08.16 10-20-30.123 (1).txt')+ await Promise.all([writeFile(source, 'source'), writeFile(dated, 'collision'), writeFile(firstCollision, 'collision')])++ await expect(uniqueDatedFilePath(source)).resolves.toEqual({+ isDir: false,+ path: path.join(tmpDir, 'report-2026.08.16 10-20-30.123 (2).txt')+ })+ })++ it('increments an existing dated directory name after its complete name', async () => {+ const source = path.join(tmpDir, 'documents.v1')+ const dated = path.join(tmpDir, 'documents.v1-2026.08.16 10-20-30.123')+ await Promise.all([fs.mkdir(source), fs.mkdir(dated), fs.mkdir(`${dated} (1)`)])++ await expect(uniqueDatedFilePath(source)).resolves.toEqual({+ isDir: true,+ path: `${dated} (2)`+ })+ })++ it('uses the known destination type when the protected trash source has a different type', async () => {+ const protectedDirectory = path.join(tmpDir, 'report.txt')+ await fs.mkdir(protectedDirectory)++ await expect(uniqueDatedFilePath(protectedDirectory, false)).resolves.toEqual({+ isDir: false,+ path: path.join(tmpDir, 'report-2026.08.16 10-20-30.123.txt')+ })+ })+})+describe(writeFromStream.name, () => {let tmpDir: stringdiff --git a/backend/src/applications/files/utils/files.ts b/backend/src/applications/files/utils/files.tsindex cff8ec20..3b7a4351 100644--- a/backend/src/applications/files/utils/files.ts+++ b/backend/src/applications/files/utils/files.ts@@ -467,15 +467,20 @@ export async function uniqueFilePathFromDir(rPath: string): Promise<string> {return rPath}-export async function uniqueDatedFilePath(rPath: string): Promise<{ isDir: boolean; path: string }> {+export async function uniqueDatedFilePath(rPath: string, knownIsDir?: boolean): Promise<{ isDir: boolean; path: string }> {const date = formatDateISOString(new Date())- if (await isPathIsDir(rPath)) {- return { isDir: true, path: `${rPath}-${date}` }- } else {- const extension = path.extname(rPath)- const nameWithoutExtension = path.basename(rPath, extension)- return { isDir: false, path: path.join(path.dirname(rPath), `${nameWithoutExtension}-${date}${extension}`) }+ const isDir = knownIsDir ?? (await isPathIsDir(rPath))+ const extension = isDir ? '' : path.extname(rPath)+ const nameWithoutExtension = path.basename(rPath, extension)+ const datedName = `${nameWithoutExtension}-${date}`+ const parentDir = path.dirname(rPath)+ let candidate = path.join(parentDir, `${datedName}${extension}`)+ let count = 1+ while (await isPathExists(candidate)) {+ candidate = path.join(parentDir, `${datedName} (${count})${extension}`)+ count++}+ return { isDir, path: candidate }}export async function checkExternalPath(rPath: string) {diff --git a/backend/src/applications/spaces/spaces.md b/backend/src/applications/spaces/spaces.mdindex e7d95ba4..1ba7ad27 100644--- a/backend/src/applications/spaces/spaces.md+++ b/backend/src/applications/spaces/spaces.md@@ -69,7 +69,8 @@ For a resource whose target is a managed trash, deletion performs the following1. Resolve the managed trash and its temporary root from the source space.2. Recreate the source's relative parent path in that trash when required.-3. If the destination name already exists, preserve the existing trash entry under a dated unique name in both the filesystem and database.+3. If the destination name already exists, preserve the existing trash entry under a dated unique name in both the filesystem and database. A numeric+ suffix is added when that dated name is already occupied.4. Move the resource to the selected trash.5. Move the corresponding database records into the trash's canonical scope and remove their locks.