Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:files): use storage-aware trash handling

johavena4af3e309ef2
diff --git a/backend/src/applications/files/constants/files.ts b/backend/src/applications/files/constants/files.ts
index 464c8297..7da982c3 100644
--- a/backend/src/applications/files/constants/files.ts
+++ b/backend/src/applications/files/constants/files.ts
@@ -1,6 +1,11 @@
export const DEFAULT_CHECKSUM_ALGORITHM = 'sha512-256'
export const DEFAULT_HIGH_WATER_MARK = 1024 * 1024
export const DEFAULT_MIME_TYPE = 'application/octet-stream'
+export const TEMPORARY_FILE_PREFIX = '~tmp-'
+export const TEMPORARY_PATH = {
+ STORAGE: '.sync-in-tmp',
+ ACTORS: 'users'
+} as const
export const EXTRA_MIMES_TYPE = new Map([
['.go', 'text-x-go'],
['.gz', 'application-gzip'],
diff --git a/backend/src/applications/files/files.config.ts b/backend/src/applications/files/files.config.ts
index 0926974e..2ecca9df 100644
--- a/backend/src/applications/files/files.config.ts
+++ b/backend/src/applications/files/files.config.ts
@@ -94,7 +94,7 @@ export class FilesConfig {
@IsNotEmpty()
@IsString()
- tmpPath: string
+ linksPath: string
@IsInt()
maxUploadSize: number = 5368709120 // 5 GB
diff --git a/backend/src/applications/files/interfaces/file-task.interface.ts b/backend/src/applications/files/interfaces/file-task.interface.ts
index 16299c5f..1091dfb3 100644
--- a/backend/src/applications/files/interfaces/file-task.interface.ts
+++ b/backend/src/applications/files/interfaces/file-task.interface.ts
@@ -1,4 +1,5 @@
import type { FileTask } from '../models/file-task'
+import type { FILE_OPERATION } from '../constants/operations'
export interface FileTasksPollResponse {
active: FileTask[]
@@ -7,10 +8,11 @@ export interface FileTasksPollResponse {
}
export interface FileTaskTransferOptions {
beforeCommit?: () => Promise<void>
- cacheKey: string
crossDevice?: boolean
+ executionId: string
onTransferStart?: () => void
onProgress?: (bytes: number) => void
+ operation: FILE_OPERATION
overwrite?: boolean
signal: AbortSignal
stagingDir?: string
diff --git a/backend/src/applications/files/services/files-content-indexer.service.spec.ts b/backend/src/applications/files/services/files-content-indexer.service.spec.ts
index 5350e887..916e5212 100644
--- a/backend/src/applications/files/services/files-content-indexer.service.spec.ts
+++ b/backend/src/applications/files/services/files-content-indexer.service.spec.ts
@@ -451,6 +451,8 @@ describe(FilesContentIndexer.name, () => {
const readdirSpy = vi.spyOn(fs, 'readdir')
readdirSpy.mockResolvedValueOnce([
{ parentPath: '/root', name: 'sub', isDirectory: () => true },
+ { parentPath: '/root', name: '.sync-in-tmp', isDirectory: () => true },
+ { parentPath: '/root', name: '.sync-in.document.txt', isDirectory: () => false },
{ parentPath: '/root', name: 'a.txt', isDirectory: () => false }
] as any)
readdirSpy.mockResolvedValueOnce([{ parentPath: '/root/sub', name: 'b.txt', isDirectory: () => false }] as any)
@@ -468,6 +470,7 @@ describe(FilesContentIndexer.name, () => {
}
expect(yielded.map((f) => f.name)).toEqual(['b.txt', 'a.txt'])
+ expect(readdirSpy.mock.calls.map(([directory]) => directory)).not.toContain('/root/.sync-in-tmp')
})
it('should skip non-indexable files in getFileMetadata', async () => {
diff --git a/backend/src/applications/files/services/files-content-indexer.service.ts b/backend/src/applications/files/services/files-content-indexer.service.ts
index 131e5f15..8fa497f7 100644
--- a/backend/src/applications/files/services/files-content-indexer.service.ts
+++ b/backend/src/applications/files/services/files-content-indexer.service.ts
@@ -17,7 +17,7 @@ import { FilesContentStore } from '../models/files-content-store'
import { FileContent, FileContentMetadata } from '../schemas/file-content.interface'
import { docTextify } from '../utils/doc-textify/doc-textify'
import { OCRManager } from '../utils/doc-textify/utils/ocr'
-import { getExtensionWithoutDot, getMimeType } from '../utils/files'
+import { getExtensionWithoutDot, getMimeType, isInternalTemporaryEntry } from '../utils/files'
import { FilesContentParser } from './files-content-parser.service'
import { genIndexingKey, genRunId } from '../utils/indexing'
import { FILE_REPOSITORY } from '../constants/operations'
@@ -236,6 +236,7 @@ export class FilesContentIndexer {
}
for (const p of paths) {
+ if (isInternalTemporaryEntry(path.basename(p.realPath))) continue
context.regexBasePath = new RegExp(`^/?${escapePath(p.realPath)}/?`)
context.pathPrefix = p.pathPrefix || ''
if (!p.isDir) {
@@ -319,6 +320,7 @@ export class FilesContentIndexer {
private async *parseFileMetadata(dir: string, context: FileContentIndexContext): AsyncGenerator<FileContentMetadata> {
try {
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
+ if (isInternalTemporaryEntry(entry.name)) continue
const realPath = path.join(entry.parentPath, entry.name)
if (entry.isDirectory()) {
yield* this.parseFileMetadata(realPath, context)
diff --git a/backend/src/applications/files/services/files-manager.service.spec.ts b/backend/src/applications/files/services/files-manager.service.spec.ts
index 0fa61536..61fddb3c 100644
--- a/backend/src/applications/files/services/files-manager.service.spec.ts
+++ b/backend/src/applications/files/services/files-manager.service.spec.ts
@@ -23,7 +23,6 @@ import * as unzipUtils from '../utils/unzip-file'
import * as untarUtils from '../utils/untar-file'
import * as filesUtils from '../utils/files'
import * as tarUtils from '../utils/tar-file'
-import * as taskUtils from '../utils/tasks'
import * as zipUtils from '../utils/zip-file'
import { FilesLockManager } from './files-lock-manager.service'
import { FilesManager } from './files-manager.service'
@@ -31,6 +30,7 @@ import { FilesQueries } from './files-queries.service'
import { FilesTasksTransfer } from './tasks/files-tasks-transfer.service'
import { Mock } from 'vitest'
import { FILE_ERROR } from '../constants/errors'
+import { FILE_OPERATION } from '../constants/operations'
vi.mock('node:dns/promises', () => ({
lookup: vi.fn()
@@ -55,8 +55,10 @@ describe(FilesManager.name, () => {
removeChildLocks: Mock
}
- const user = { id: 7, login: 'john', tmpPath: '/data/users/john/tmp', tasksPath: '/data/users/john/tmp/tasks' } as any
- const taskPath = (cacheKey: string, name: string): string => path.join(user.tasksPath, `${taskUtils.taskTemporaryPrefix(cacheKey)}${name}`)
+ const user = { id: 7, login: 'john', tmpPath: '/data/users/john/tmp' } as any
+ const targetTmpRoot = '/data/users/john/.sync-in-tmp/users/7'
+ const temporaryPath = (operation: FILE_OPERATION, executionId: string, name: string, parentPath = targetTmpRoot): string =>
+ path.join(parentPath, `${filesUtils.temporaryFilePrefix(operation, executionId)}${path.basename(name)}`)
const makeSpace = (overrides: Record<string, any> = {}) =>
({
@@ -67,6 +69,7 @@ describe(FilesManager.name, () => {
realPath: '/data/users/john/files/file.txt',
realBasePath: '/data/users/john/files',
dbFile: { ownerId: 7, path: 'file.txt', inTrash: false },
+ inPersonalSpace: true,
inTrashRepository: false,
quotaIsExceeded: false,
storageQuota: null,
@@ -184,8 +187,12 @@ describe(FilesManager.name, () => {
vi.spyOn(filesUtils, 'isPathExists').mockResolvedValue(true)
vi.spyOn(filesUtils, 'isPathIsDir').mockResolvedValue(false)
vi.spyOn(filesUtils, 'makeDir').mockResolvedValue('/tmp' as any)
- vi.spyOn(filesUtils, 'makeTempDir').mockResolvedValue('/tmp/extract')
- vi.spyOn(filesUtils, 'tempFilePath').mockReturnValue('/tmp/staged-file')
+ vi.spyOn(filesUtils, 'temporaryFilePath').mockImplementation((parentPath, targetPath, operation, executionId = 'direct-id') =>
+ temporaryPath(operation as FILE_OPERATION, executionId, targetPath, parentPath)
+ )
+ vi.spyOn(filesUtils, 'makeTemporaryDirectory').mockImplementation(async (parentPath, targetPath, operation, executionId = 'direct-id') =>
+ temporaryPath(operation as FILE_OPERATION, executionId, targetPath, parentPath)
+ )
vi.spyOn(filesUtils, 'writeUploadFromStream').mockResolvedValue(undefined)
vi.spyOn(filesUtils, 'writeUploadFromStreamAndChecksum').mockResolvedValue('sha256-abc')
vi.spyOn(filesUtils, 'moveFiles').mockResolvedValue(undefined)
@@ -198,16 +205,17 @@ describe(FilesManager.name, () => {
vi.spyOn(filesUtils, 'dirSize').mockResolvedValue([123, {}] as any)
vi.spyOn(filesUtils, 'uniqueFilePathFromDir').mockResolvedValue('/tmp/unique-path.txt')
vi.spyOn(filesUtils, 'uniqueDatedFilePath').mockResolvedValue({ isDir: false, path: '/trash/file-2026.txt' })
- vi.spyOn(taskUtils, 'createTaskTemporaryDir').mockResolvedValue(taskPath('task-d', 'archive'))
- vi.spyOn(taskUtils, 'taskTemporaryPath').mockImplementation((parentPath, cacheKey, name) =>
- path.join(parentPath, `${taskUtils.taskTemporaryPrefix(cacheKey)}${path.basename(name)}`)
- )
vi.spyOn(tarUtils, 'createTar').mockResolvedValue(undefined)
vi.spyOn(zipUtils, 'createZip').mockResolvedValue(undefined)
vi.spyOn(filesUtils, 'getMimeType').mockReturnValue('image-png')
vi.spyOn(spacesPermsUtils, 'canAccessToSpace').mockReturnValue(true)
vi.spyOn(spacesPermsUtils, 'haveSpaceEnvPermissions').mockReturnValue(true)
- vi.spyOn(spacesPathUtils, 'realTrashPathFromSpace').mockReturnValue('/data/users/john/trash')
+ vi.spyOn(spacesPathUtils, 'trashTargetFromSpace').mockReturnValue({
+ mode: 'trash',
+ path: '/data/users/john/trash',
+ temporaryRoot: targetTmpRoot
+ })
+ vi.spyOn(spacesPathUtils, 'temporaryRootFromSpace').mockReturnValue(targetTmpRoot)
})
afterEach(() => {
@@ -474,8 +482,8 @@ describe(FilesManager.name, () => {
const tmpWritePath = vi.mocked(filesUtils.writeUploadFromStream).mock.calls[0][0] as string
expect(filesLockManager.createOrRefresh).toHaveBeenCalled()
- expect(tmpWritePath.startsWith(`${user.tmpPath}${path.sep}`)).toBe(true)
- expect(path.basename(tmpWritePath)).toMatch(/^report\.txt-upload-/)
+ expect(tmpWritePath.startsWith(`${targetTmpRoot}${path.sep}`)).toBe(true)
+ expect(path.basename(tmpWritePath)).toMatch(/^~tmp-upload-[a-z0-9-]+-report\.txt$/)
expect(filesUtils.writeUploadFromStream).toHaveBeenCalledWith(
tmpWritePath,
expect.anything(),
@@ -531,7 +539,7 @@ describe(FilesManager.name, () => {
await service.saveMultipart(user, space, req as any)
const tmpWritePath = vi.mocked(filesUtils.writeUploadFromStream).mock.calls[0][0] as string
- expect(tmpWritePath.startsWith(`${user.tmpPath}${path.sep}`)).toBe(true)
+ expect(tmpWritePath.startsWith(`${targetTmpRoot}${path.sep}`)).toBe(true)
expect(filesUtils.writeUploadFromStream).toHaveBeenCalledWith(
tmpWritePath,
file,
@@ -688,7 +696,7 @@ describe(FilesManager.name, () => {
)
const tmpWritePath = vi.mocked(filesUtils.writeUploadFromStream).mock.calls[0][0] as string
- expect(tmpWritePath.startsWith(`${user.tmpPath}${path.sep}`)).toBe(true)
+ expect(tmpWritePath.startsWith(`${targetTmpRoot}${path.sep}`)).toBe(true)
expect(filesUtils.writeUploadFromStream).toHaveBeenCalledWith(
tmpWritePath,
file,
@@ -850,11 +858,14 @@ describe(FilesManager.name, () => {
await service.saveMultipart(user, space, req as any)
const tmpWritePath = vi.mocked(filesUtils.writeUploadFromStream).mock.calls[0][0] as string
+ const makeDirMock = vi.mocked(filesUtils.makeDir)
+ const dstDirMakeCallIndex = makeDirMock.mock.calls.findIndex(([rPath]) => rPath === dstDir)
expect(deleteSpy).toHaveBeenCalledTimes(1)
expect(filesUtils.makeDir).toHaveBeenCalledWith(dstDir, true)
expect(filesUtils.moveFiles).toHaveBeenCalledWith(tmpWritePath, dstFile, true)
- expect(deleteSpy.mock.invocationCallOrder[0]).toBeLessThan(vi.mocked(filesUtils.makeDir).mock.invocationCallOrder[0])
- expect(vi.mocked(filesUtils.makeDir).mock.invocationCallOrder[0]).toBeLessThan(vi.mocked(filesUtils.moveFiles).mock.invocationCallOrder[0])
+ expect(dstDirMakeCallIndex).toBeGreaterThanOrEqual(0)
+ expect(deleteSpy.mock.invocationCallOrder[0]).toBeLessThan(makeDirMock.mock.invocationCallOrder[dstDirMakeCallIndex])
+ expect(makeDirMock.mock.invocationCallOrder[dstDirMakeCallIndex]).toBeLessThan(vi.mocked(filesUtils.moveFiles).mock.invocationCallOrder[0])
})
})
@@ -1047,7 +1058,7 @@ describe(FilesManager.name, () => {
url: 'files/source/src.txt',
realPath: '/src-base/src.txt',
dbFile: { ownerId: 7, path: 'src.txt', inTrash: false },
- task: { cacheKey: 'task-move', props: {} }
+ task: { id: 'task-move', type: FILE_OPERATION.MOVE, cacheKey: 'task-move', props: {} }
})
const dst = makeSpace({
url: 'files/destination/dst.txt',
@@ -1070,7 +1081,7 @@ describe(FilesManager.name, () => {
const src = makeSpace({
realPath: '/data/users/john/files/src.txt',
dbFile: { ownerId: 7, path: 'src.txt', inTrash: false },
- task: { cacheKey: 'task-move', props: {} }
+ task: { id: 'task-move', type: FILE_OPERATION.MOVE, cacheKey: 'task-move', props: {} }
})
const dst = makeSpace({
realPath: '/data/users/john/files/dst.txt',
@@ -1108,7 +1119,7 @@ describe(FilesManager.name, () => {
const src = makeSpace({
realPath: '/data/users/john/files/src.txt',
dbFile: { ownerId: 7, path: 'src.txt', inTrash: false },
- task: { cacheKey: 'task-copy', props: {} }
+ task: { id: 'task-copy', type: FILE_OPERATION.COPY, cacheKey: 'task-copy', props: {} }
})
const dst = makeSpace({
realPath: '/data/users/john/files/dst.txt',
@@ -1147,7 +1158,7 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/no-trash.txt', inTrashRepository: false })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true)
vi.mocked(filesUtils.isPathIsDir).mockResolvedValueOnce(false)
- vi.mocked(spacesPathUtils.realTrashPathFromSpace).mockReturnValueOnce(null)
+ vi.mocked(spacesPathUtils.trashTargetFromSpace).mockReturnValueOnce(null)
await service.delete(user, space)
@@ -1155,11 +1166,30 @@ describe(FilesManager.name, () => {
expect(filesQueries.deleteFiles).toHaveBeenCalledWith(space.dbFile, false, true)
})
+ it('should permanently delete an external share without resolving a trash path', async () => {
+ const space = makeSpace({
+ realPath: '/mnt/share/document.txt',
+ dbFile: { shareExternalId: 12, path: 'document.txt', inTrash: false }
+ })
+ vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true)
+ vi.mocked(filesUtils.isPathIsDir).mockResolvedValueOnce(false)
+ vi.mocked(spacesPathUtils.trashTargetFromSpace).mockReturnValueOnce({ mode: 'permanent', reason: 'external-share' })
+ const emitSpy = vi.spyOn(FileEvent, 'emit')
+
+ await service.delete(user, space)
+
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(space.realPath)
+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()
+ expect(filesTasksTransfer.delete).not.toHaveBeenCalled()
+ expect(filesQueries.deleteFiles).toHaveBeenCalledWith(space.dbFile, false, true)
+ expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: space.realPath })
+ })
+
it('should update the database before reporting an abortable delete source cleanup failure', async () => {
const space = makeSpace({
realPath: '/data/users/john/files/document.txt',
dbFile: { ownerId: 7, path: 'documents/document.txt', inTrash: false },
- task: { cacheKey: 'task-delete', props: {} }
+ task: { id: 'task-delete', type: FILE_OPERATION.DELETE, cacheKey: 'task-delete', props: {} }
})
const trashFile = '/data/users/john/trash/documents/document.txt'
const signal = new AbortController().signal
@@ -1169,7 +1199,7 @@ describe(FilesManager.name, () => {
await expect(service.delete(user, space, undefined, signal)).rejects.toBe(cleanupError)
- expect(filesTasksTransfer.delete).toHaveBeenCalledWith(user, space, trashFile, false, signal, expect.any(Function))
+ expect(filesTasksTransfer.delete).toHaveBeenCalledWith(space, trashFile, targetTmpRoot, false, signal, expect.any(Function))
expect(filesUtils.moveFiles).not.toHaveBeenCalled()
expect(filesQueries.deleteFiles).toHaveBeenCalledWith(space.dbFile, false, false)
})
@@ -1214,7 +1244,7 @@ describe(FilesManager.name, () => {
})
it('should handle HEAD+GET and emit task watch/event', async () => {
- const space = makeSpace({ task: { cacheKey: 'task-1', props: {} } })
+ const space = makeSpace({ task: { id: 'task-1', type: FILE_OPERATION.DOWNLOAD, cacheKey: 'task-1', props: {} } })
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/download.txt')
http.axiosRef
.mockResolvedValueOnce({
@@ -1232,15 +1262,14 @@ describe(FilesManager.name, () => {
expect(space.task.props).toMatchObject({ progress: 1, size: 0, totalSize: 55 })
expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, '/tmp/download.txt')
- expect(taskUtils.taskTemporaryPath).toHaveBeenCalledWith(user.tasksPath, 'task-1', '/tmp/download.txt')
- expect(filesUtils.tempFilePath).not.toHaveBeenCalled()
+ expect(filesUtils.temporaryFilePath).toHaveBeenCalledWith(targetTmpRoot, '/tmp/download.txt', FILE_OPERATION.DOWNLOAD, 'task-1')
expect(filesUtils.writeUploadFromStream).toHaveBeenCalledWith(
- taskPath('task-1', 'download.txt'),
+ temporaryPath(FILE_OPERATION.DOWNLOAD, 'task-1', 'download.txt'),
expect.anything(),
expect.objectContaining({ limiter: expect.objectContaining({ consume: expect.any(Function) }), onProgress: expect.any(Function) })
)
expect(filesTasksTransfer.createByteProgressHandler).toHaveBeenCalledWith(space)
- expect(filesUtils.moveFiles).toHaveBeenCalledWith(taskPath('task-1', 'download.txt'), '/tmp/download.txt')
+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(temporaryPath(FILE_OPERATION.DOWNLOAD, 'task-1', 'download.txt'), '/tmp/download.txt')
expect(filesLockManager.create).toHaveBeenCalledWith(
user,
expect.objectContaining({ path: 'download.txt' }),
@@ -1253,9 +1282,9 @@ describe(FilesManager.name, () => {
it('should reject a remote download exceeding maxUploadSize before GET', async () => {
const space = makeSpace()
- const tmpPath = '/data/users/john/tmp/download.txt-download-uuid'
+ const tmpPath = `${targetTmpRoot}/~tmp-download-direct-id-download.txt`
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/download.txt')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce(tmpPath)
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(tmpPath)
http.axiosRef.mockResolvedValueOnce({
headers: { 'content-length': `${configuration.applications.files.maxUploadSize + 1}` },
request: { socket: { remoteAddress: '8.8.8.8' } }
@@ -1275,7 +1304,7 @@ describe(FilesManager.name, () => {
const error = new FileError(HttpStatus.PAYLOAD_TOO_LARGE, FILE_ERROR.MAX_FILE_SIZE_EXCEEDED)
const space = makeSpace()
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/download.txt')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/download.txt-download-uuid')
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(`${targetTmpRoot}/~tmp-download-direct-id-download.txt`)
vi.mocked(filesUtils.writeUploadFromStream).mockRejectedValueOnce(error)
http.axiosRef
.mockResolvedValueOnce({
@@ -1290,7 +1319,7 @@ describe(FilesManager.name, () => {
await expect(service.downloadFromUrl(user, space, { url: 'https://example.org/file.txt' })).rejects.toBe(error)
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/download.txt-download-uuid')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(`${targetTmpRoot}/~tmp-download-direct-id-download.txt`)
expect(filesUtils.moveFiles).not.toHaveBeenCalled()
expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
expect(fileEmitSpy).not.toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/download.txt' })
@@ -1300,7 +1329,7 @@ describe(FilesManager.name, () => {
const error = new Error('move failed')
const space = makeSpace()
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/download.txt')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/download.txt-download-uuid')
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(`${targetTmpRoot}/~tmp-download-direct-id-download.txt`)
vi.mocked(filesUtils.moveFiles).mockRejectedValueOnce(error)
http.axiosRef
.mockResolvedValueOnce({
@@ -1315,20 +1344,25 @@ describe(FilesManager.name, () => {
await expect(service.downloadFromUrl(user, space, { url: 'https://example.org/file.txt' })).rejects.toBe(error)
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/download.txt-download-uuid')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(`${targetTmpRoot}/~tmp-download-direct-id-download.txt`)
expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
expect(fileEmitSpy).not.toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/download.txt' })
})
})
describe('compress', () => {
+ const compressionTask = (id = 'task-export') => ({ id, type: FILE_OPERATION.COMPRESS, cacheKey: id, props: {} })
+
it('should archive files and emit events', async () => {
const tarSpy = vi.mocked(tarUtils.createTar).mockImplementationOnce(async (_outputPath, _entries, _gzip, _signal, onProgress) => {
onProgress?.(Buffer.byteLength('content'))
})
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive.tgz')
vi.mocked(filesUtils.isPathIsDir).mockImplementation(async (p: string) => p.endsWith('/dir'))
- const space = makeSpace({ realPath: '/data/users/john/files/source.txt', task: { cacheKey: 'task-c', props: {} } })
+ const space = makeSpace({
+ realPath: '/data/users/john/files/source.txt',
+ task: { id: 'task-c', type: FILE_OPERATION.COMPRESS, cacheKey: 'task-c', props: {} }
+ })
const dto = {
name: 'archive',
extension: 'tar',
@@ -1343,22 +1377,40 @@ describe(FilesManager.name, () => {
await service.compress(user, space, dto)
- expect(tarSpy).toHaveBeenCalledWith(taskPath('task-c', 'archive.tgz'), dto.files, true, undefined, expect.any(Function), undefined)
- expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, '/tmp/archive.tgz')
- expect(taskUtils.taskTemporaryPath).toHaveBeenCalledWith(user.tasksPath, 'task-c', '/tmp/archive.tgz')
- expect(filesUtils.tempFilePath).not.toHaveBeenCalled()
+ const archivePath = temporaryPath(FILE_OPERATION.COMPRESS, 'task-c', 'archive.tgz', user.tmpPath)
+ expect(tarSpy).toHaveBeenCalledWith(archivePath, dto.files, true, undefined, expect.any(Function), undefined)
+ expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, path.join(user.tmpPath, 'archive.tgz'))
+ expect(filesUtils.temporaryFilePath).toHaveBeenCalledWith(user.tmpPath, 'archive.tgz', FILE_OPERATION.COMPRESS, 'task-c')
expect(space.task.props.size).toBe(Buffer.byteLength('content'))
- expect(filesUtils.moveFiles).toHaveBeenCalledWith(taskPath('task-c', 'archive.tgz'), '/tmp/archive.tgz')
+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()
})
- it('should allow archive export from trash when compressInDirectory is false', async () => {
- vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive-trash.tgz')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive-trash.tgz-compress-uuid')
+ it('should require an explicit task for an archive export', async () => {
+ const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
+ const dto = {
+ name: 'archive',
+ extension: 'tar',
+ compression: true,
+ compressInDirectory: false,
+ files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
+ } as any
+
+ await expect(service.compress(user, space, dto)).rejects.toEqual(
+ new FileError(HttpStatus.BAD_REQUEST, 'Archive export requires an explicit task')
+ )
+ expect(tarUtils.createTar).not.toHaveBeenCalled()
+ expect(zipUtils.createZip).not.toHaveBeenCalled()
+ })
+
+ it('should allow archive export from trash as an explicit task', async () => {
+ const archivePath = '/data/users/john/tmp/~tmp-compress-task-export-archive-trash.tgz'
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(archivePath)
const emitSpy = vi.spyOn(FileEvent, 'emit')
const space = makeTrashSpace({
url: 'trash/personal/source.txt',
realPath: '/data/users/john/trash/source.txt',
- dbFile: { ownerId: 7, path: 'source.txt', inTrash: true }
+ dbFile: { ownerId: 7, path: 'source.txt', inTrash: true },
+ task: compressionTask()
})
const dto = {
name: 'archive-trash',
@@ -1370,22 +1422,16 @@ describe(FilesManager.name, () => {
await expect(service.compress(user, space, dto)).resolves.toBeUndefined()
expect(filesLockManager.create).not.toHaveBeenCalled()
- expect(tarUtils.createTar).toHaveBeenCalledWith(
- '/data/users/john/tmp/archive-trash.tgz-compress-uuid',
- dto.files,
- true,
- undefined,
- undefined,
- undefined
- )
- expect(filesUtils.moveFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive-trash.tgz-compress-uuid', '/tmp/archive-trash.tgz')
- expect(emitSpy).toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/archive-trash.tgz' })
+ expect(tarUtils.createTar).toHaveBeenCalledWith(archivePath, dto.files, true, undefined, expect.any(Function), undefined)
+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()
+ expect(emitSpy).not.toHaveBeenCalled()
})
it('should cleanup temporary archive and skip ADD event when publishing archive fails', async () => {
const error = new Error('move failed')
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive.tgz')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive.tgz-compress-uuid')
+ const archivePath = `${targetTmpRoot}/~tmp-compress-direct-id-archive.tgz`
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(archivePath)
vi.mocked(filesUtils.moveFiles).mockRejectedValueOnce(error)
const emitSpy = vi.spyOn(FileEvent, 'emit')
const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
@@ -1393,23 +1439,23 @@ describe(FilesManager.name, () => {
name: 'archive',
extension: 'tar',
compression: true,
- compressInDirectory: false,
+ compressInDirectory: true,
files: [{ path: '/data/users/john/files/source.txt', name: 'source.txt', rootAlias: null }]
} as any
await expect(service.compress(user, space, dto)).rejects.toBe(error)
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive.tgz-compress-uuid')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(archivePath)
expect(emitSpy).not.toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/archive.tgz' })
})
it('should cleanup temporary archive when TAR creation fails', async () => {
const error = new Error('archive failed')
- vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive.tgz')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive.tgz-compress-uuid')
+ const archivePath = '/data/users/john/tmp/~tmp-compress-task-export-archive.tgz'
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(archivePath)
vi.mocked(tarUtils.createTar).mockRejectedValueOnce(error)
const emitSpy = vi.spyOn(FileEvent, 'emit')
- const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
+ const space = makeSpace({ realPath: '/data/users/john/files/source.txt', task: compressionTask() })
const dto = {
name: 'archive',
extension: 'tar',
@@ -1420,7 +1466,7 @@ describe(FilesManager.name, () => {
await expect(service.compress(user, space, dto)).rejects.toBe(error)
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive.tgz-compress-uuid')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(archivePath)
expect(filesUtils.moveFiles).not.toHaveBeenCalled()
expect(emitSpy).not.toHaveBeenCalledWith('event', { user, space, action: ACTION.ADD, rPath: '/tmp/archive.tgz' })
})
@@ -1428,10 +1474,10 @@ describe(FilesManager.name, () => {
it('should preserve task cancellation while cleaning the temporary archive', async () => {
const controller = new AbortController()
const reason = new Error('Cancelled')
- vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive.tar')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive.tar-compress-uuid')
+ const archivePath = '/data/users/john/tmp/~tmp-compress-task-export-archive.tar'
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(archivePath)
vi.mocked(tarUtils.createTar).mockRejectedValueOnce(reason)
- const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
+ const space = makeSpace({ realPath: '/data/users/john/files/source.txt', task: compressionTask() })
const dto = {
name: 'archive',
extension: 'tar',
@@ -1442,20 +1488,14 @@ describe(FilesManager.name, () => {
controller.abort(reason)
await expect(service.compress(user, space, dto, controller.signal)).rejects.toBe(reason)
- expect(tarUtils.createTar).toHaveBeenCalledWith(
- '/data/users/john/tmp/archive.tar-compress-uuid',
- dto.files,
- false,
- controller.signal,
- undefined,
- undefined
- )
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive.tar-compress-uuid')
+ expect(tarUtils.createTar).toHaveBeenCalledWith(archivePath, dto.files, false, controller.signal, expect.any(Function), undefined)
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(archivePath)
})
it('should limit an archive to the known remaining quota', async () => {
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive.tar')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive.tar-compress-uuid')
+ const archivePath = `${targetTmpRoot}/~tmp-compress-direct-id-archive.tar`
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(archivePath)
const space = makeSpace({
realPath: '/data/users/john/files/source.txt',
storageQuota: 100,
@@ -1471,13 +1511,13 @@ describe(FilesManager.name, () => {
await service.compress(user, space, dto)
- expect(tarUtils.createTar).toHaveBeenCalledWith('/data/users/john/tmp/archive.tar-compress-uuid', dto.files, false, undefined, undefined, 60)
+ expect(tarUtils.createTar).toHaveBeenCalledWith(archivePath, dto.files, false, undefined, undefined, 60)
})
it('should create a compressed ZIP archive', async () => {
- vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/tmp/archive.zip')
- vi.mocked(filesUtils.tempFilePath).mockReturnValueOnce('/data/users/john/tmp/archive.zip-compress-uuid')
- const space = makeSpace({ realPath: '/data/users/john/files/source.txt' })
+ const archivePath = '/data/users/john/tmp/~tmp-compress-task-export-archive.zip'
+ vi.mocked(filesUtils.temporaryFilePath).mockReturnValueOnce(archivePath)
+ const space = makeSpace({ realPath: '/data/users/john/files/source.txt', task: compressionTask() })
const dto = {
name: 'archive.zip',
extension: 'zip',
@@ -1488,22 +1528,18 @@ describe(FilesManager.name, () => {
await service.compress(user, space, dto)
- expect(zipUtils.createZip).toHaveBeenCalledWith(
- '/data/users/john/tmp/archive.zip-compress-uuid',
- dto.files,
- true,
- undefined,
- undefined,
- undefined
- )
+ expect(zipUtils.createZip).toHaveBeenCalledWith(archivePath, dto.files, true, undefined, expect.any(Function), undefined)
expect(tarUtils.createTar).not.toHaveBeenCalled()
- expect(filesUtils.moveFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive.zip-compress-uuid', '/tmp/archive.zip')
+ expect(filesUtils.moveFiles).not.toHaveBeenCalled()
})
})
describe('decompress', () => {
it('should extract zip and release lock', async () => {
- const space = makeSpace({ realPath: '/data/users/john/files/archive.zip', task: { cacheKey: 'task-d', props: {} } })
+ const space = makeSpace({
+ realPath: '/data/users/john/files/archive.zip',
+ task: { id: 'task-d', type: FILE_OPERATION.DECOMPRESS, cacheKey: 'task-d', props: {} }
+ })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true).mockResolvedValueOnce(false)
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive')
const unzipSpy = vi.spyOn(unzipUtils, 'extractZip').mockResolvedValueOnce(undefined)
@@ -1511,17 +1547,16 @@ describe(FilesManager.name, () => {
await service.decompress(user, space)
- expect(taskUtils.createTaskTemporaryDir).toHaveBeenCalledWith(user.tasksPath, 'task-d', '/data/users/john/files/archive')
- expect(filesUtils.makeTempDir).not.toHaveBeenCalled()
- expect(filesTasksTransfer.createExtractionProgressHandler).toHaveBeenCalledWith(space)
- expect(unzipSpy).toHaveBeenCalledWith(
- '/data/users/john/files/archive.zip',
- taskPath('task-d', 'archive'),
- undefined,
- undefined,
- expect.any(Function)
+ const extractionPath = temporaryPath(FILE_OPERATION.DECOMPRESS, 'task-d', 'archive')
+ expect(filesUtils.makeTemporaryDirectory).toHaveBeenCalledWith(
+ targetTmpRoot,
+ '/data/users/john/files/archive',
+ FILE_OPERATION.DECOMPRESS,
+ 'task-d'
)
- expect(filesUtils.moveFiles).toHaveBeenCalledWith(taskPath('task-d', 'archive'), '/data/users/john/files/archive')
+ expect(filesTasksTransfer.createExtractionProgressHandler).toHaveBeenCalledWith(space)
+ expect(unzipSpy).toHaveBeenCalledWith('/data/users/john/files/archive.zip', extractionPath, undefined, undefined, expect.any(Function))
+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(extractionPath, '/data/users/john/files/archive')
expect(taskEmitSpy).toHaveBeenCalledWith('startWatch', space, '/data/users/john/files/archive')
expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
})
@@ -1530,34 +1565,34 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/archive.tar.gz' })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true).mockResolvedValueOnce(false)
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive')
- vi.mocked(filesUtils.makeTempDir).mockResolvedValueOnce('/data/users/john/tmp/archive-extract-123')
+ vi.mocked(filesUtils.makeTemporaryDirectory).mockResolvedValueOnce(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
const untarSpy = vi.spyOn(untarUtils, 'extractTar').mockResolvedValueOnce(undefined)
await service.decompress(user, space)
expect(untarSpy).toHaveBeenCalledWith(
'/data/users/john/files/archive.tar.gz',
- '/data/users/john/tmp/archive-extract-123',
+ `${targetTmpRoot}/~tmp-decompress-direct-id-archive`,
true,
undefined,
undefined,
undefined
)
- expect(filesUtils.moveFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive-extract-123', '/data/users/john/files/archive')
+ expect(filesUtils.moveFiles).toHaveBeenCalledWith(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`, '/data/users/john/files/archive')
})
it('should limit extracted size to the known remaining quota', async () => {
const space = makeSpace({ realPath: '/data/users/john/files/archive.zip', storageQuota: 100, storageUsage: 40 })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true).mockResolvedValueOnce(false)
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive')
- vi.mocked(filesUtils.makeTempDir).mockResolvedValueOnce('/data/users/john/tmp/archive-extract-123')
+ vi.mocked(filesUtils.makeTemporaryDirectory).mockResolvedValueOnce(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
const unzipSpy = vi.spyOn(unzipUtils, 'extractZip').mockResolvedValueOnce(undefined)
await service.decompress(user, space)
expect(unzipSpy).toHaveBeenCalledWith(
'/data/users/john/files/archive.zip',
- '/data/users/john/tmp/archive-extract-123',
+ `${targetTmpRoot}/~tmp-decompress-direct-id-archive`,
60,
undefined,
undefined
@@ -1568,14 +1603,14 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/archive.zip' })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true)
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive')
- vi.mocked(filesUtils.makeTempDir).mockResolvedValueOnce('/data/users/john/tmp/archive-extract-123')
+ vi.mocked(filesUtils.makeTemporaryDirectory).mockResolvedValueOnce(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
const error = new Error('extraction failed')
vi.spyOn(unzipUtils, 'extractZip').mockRejectedValueOnce(error)
const emitSpy = vi.spyOn(FileEvent, 'emit')
await expect(service.decompress(user, space)).rejects.toBe(error)
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive-extract-123')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
expect(filesUtils.removeFiles).not.toHaveBeenCalledWith('/data/users/john/files/archive')
expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
expect(emitSpy).not.toHaveBeenCalledWith('event', {
@@ -1590,14 +1625,14 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/archive.zip' })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true).mockResolvedValueOnce(false)
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive')
- vi.mocked(filesUtils.makeTempDir).mockResolvedValueOnce('/data/users/john/tmp/archive-extract-123')
+ vi.mocked(filesUtils.makeTemporaryDirectory).mockResolvedValueOnce(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
const error = new Error('move failed')
vi.spyOn(unzipUtils, 'extractZip').mockResolvedValueOnce(undefined)
vi.mocked(filesUtils.moveFiles).mockRejectedValueOnce(error)
await expect(service.decompress(user, space)).rejects.toBe(error)
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive-extract-123')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
expect(filesUtils.removeFiles).not.toHaveBeenCalledWith('/data/users/john/files/archive')
expect(filesLockManager.removeLock).toHaveBeenCalledWith('lock-1')
})
@@ -1606,12 +1641,12 @@ describe(FilesManager.name, () => {
const space = makeSpace({ realPath: '/data/users/john/files/archive.zip' })
vi.mocked(filesUtils.isPathExists).mockResolvedValueOnce(true).mockResolvedValueOnce(true)
vi.mocked(filesUtils.uniqueFilePathFromDir).mockResolvedValueOnce('/data/users/john/files/archive')
- vi.mocked(filesUtils.makeTempDir).mockResolvedValueOnce('/data/users/john/tmp/archive-extract-123')
+ vi.mocked(filesUtils.makeTemporaryDirectory).mockResolvedValueOnce(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
vi.spyOn(unzipUtils, 'extractZip').mockResolvedValueOnce(undefined)
await expect(service.decompress(user, space)).rejects.toEqual(new FileError(HttpStatus.CONFLICT, 'The destination already exists'))
- expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/users/john/tmp/archive-extract-123')
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith(`${targetTmpRoot}/~tmp-decompress-direct-id-archive`)
expect(filesUtils.removeFiles).not.toHaveBeenCalledWith('/data/users/john/files/archive')
expect(filesUtils.moveFiles).not.toHaveBeenCalled()
})
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index 38c98e38..34230920 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -11,17 +11,18 @@ import { HTTP_METHOD } from '../../applications.constants'
import { NOTIFICATION_APP, NOTIFICATION_APP_EVENT } from '../../notifications/constants/notifications'
import { NotificationContent } from '../../notifications/interfaces/notification-properties.interface'
import { NotificationsManager } from '../../notifications/services/notifications-manager.service'
-import { SPACE_OPERATION, SPACE_PERSONAL, SPACE_REPOSITORY } from '../../spaces/constants/spaces'
+import { SPACE_OPERATION } from '../../spaces/constants/spaces'
import { FastifySpaceRequest } from '../../spaces/interfaces/space-request.interface'
import { SpaceEnv } from '../../spaces/models/space-env.model'
import { SpacesManager } from '../../spaces/services/spaces-manager.service'
-import { realTrashPathFromSpace } from '../../spaces/utils/paths'
+import { temporaryRootFromSpace, trashTargetFromSpace } from '../../spaces/utils/paths'
import { canAccessToSpace, haveSpaceEnvPermissions } from '../../spaces/utils/permissions'
import { UserModel } from '../../users/models/user.model'
import { DEPTH, LOCK_DEPTH } from '../../webdav/constants/webdav'
import { CACHE_LOCK_FILE_TTL } from '../constants/cache'
import { TAR_EXTENSION, TAR_GZ_EXTENSION, ZIP_EXTENSION } from '../constants/compress'
import { COMPRESSION_EXTENSION } from '../constants/files'
+import { FILE_OPERATION } from '../constants/operations'
import { ALL_DOCUMENT_TYPES, DEFAULT_DOCUMENT_TYPES, SAMPLE_PATH_WITHOUT_EXT } from '../constants/samples'
import { CompressFileDto, DownloadFileDto } from '../dto/file-operations.dto'
import { FileDBProps } from '../interfaces/file-db-props.interface'
@@ -44,11 +45,10 @@ import {
isPathInside,
isPathIsDir,
makeDir,
- makeTempDir,
+ makeTemporaryDirectory,
moveFiles,
removeFiles,
- tempFilePath,
- temporaryPathPrefix,
+ temporaryFilePath,
touchFile,
uniqueDatedFilePath,
uniqueFilePathFromDir,
@@ -71,7 +71,6 @@ import {
uploadTmpFilePath
} from '../utils/upload-file'
import { maxFileSizeExceededError } from '../utils/errors'
-import { createTaskTemporaryDir, taskTemporaryPath } from '../utils/tasks'
import { FilesTasksTransfer } from './tasks/files-tasks-transfer.service'
import { createTar } from '../utils/tar-file'
import { createZip } from '../utils/zip-file'
@@ -229,6 +228,7 @@ export class FilesManager {
const patchMethod = req.method === HTTP_METHOD.PATCH
const postMethod = req.method === HTTP_METHOD.POST
const realParentPath = dirName(space.realPath)
+ const temporaryRoot = overwrite || patchMethod ? temporaryRootFromSpace(user, space) : undefined
const uploadLimiter = createUploadStreamLimiter(space, configuration.applications.files.maxUploadSize)
// For POST, space.realPath can be either the final file path or the root directory for a folder upload.
@@ -262,7 +262,7 @@ export class FilesManager {
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 tmpFile = temporaryRoot ? uploadTmpFilePath(temporaryRoot, partFileName) : undefined
const writePath = tmpFile || dstFile
const dstDir = dirName(dstFile)
@@ -283,8 +283,9 @@ export class FilesManager {
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))) {
+ if (tmpFile) {
+ await makeDir(dirName(tmpFile), true)
+ } else if (!(await isPathExists(dstDir))) {
await makeDir(dstDir, true)
}
@@ -565,41 +566,33 @@ export class FilesManager {
await removeFiles(space.realPath)
FileEvent.emit('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: space.realPath })
} else {
- const baseTrashPath = realTrashPathFromSpace(user, space)
- if (baseTrashPath) {
+ const trashTarget = trashTargetFromSpace(user, space)
+ if (trashTarget?.mode === 'trash') {
const name = fileName(space.realPath)
- const trashDir = path.join(baseTrashPath, dirName(space.dbFile.path))
+ const trashDir = path.join(trashTarget.path, dirName(space.dbFile.path))
const trashFile = path.join(trashDir, name)
if (!(await isPathExists(trashDir))) {
await makeDir(trashDir, true)
}
if (isTaskContext) {
- sourceCleanupError = await this.filesTasksTransfer.delete(user, space, trashFile, isDir, signal, () =>
+ sourceCleanupError = await this.filesTasksTransfer.delete(space, trashFile, trashTarget.temporaryRoot, isDir, signal, () =>
this.moveExistingTrashFile(space, trashFile)
)
} else {
await this.moveExistingTrashFile(space, trashFile)
await moveFiles(space.realPath, trashFile, true)
}
- // emit file event
- if (space.dbFile.shareExternalId) {
- // deleted files from shares with external locations are moved to the owner’s trash
- FileEvent.emit('event', { user, space, action: ACTION.DELETE_PERMANENTLY, rPath: space.realPath })
- // emit an event for the file newly moved to the owner’s trash space
- const userSpace = new SpaceEnv(SPACE_PERSONAL, null, false)
- userSpace.setup(user, SPACE_REPOSITORY.TRASH, null, [], [])
- FileEvent.emit('event', { user, space: userSpace, action: ACTION.ADD, rPath: trashFile })
- } else {
- // emit an event for the file or directory moved to the trash
- // space keeps its original path and rPath is its new trash path
- FileEvent.emit('event', { user, space, action: ACTION.DELETE, rPath: trashFile })
- }
+ // emit an event for the file or directory moved to the trash
+ // space keeps its original path and rPath is its new trash path
+ FileEvent.emit('event', { user, space, action: ACTION.DELETE, rPath: trashFile })
} else {
- // unsupported case: delete the file (this shouldn't happen)
- this.logger.error({
- tag: this.delete.name,
- msg: `Unable to find trash path for space - *${space.alias}* (${space.id}) : delete permanently : ${space.realPath}`
- })
+ if (!trashTarget) {
+ // unsupported case: delete the file (this shouldn't happen)
+ this.logger.error({
+ tag: this.delete.name,
+ msg: `Unable to find trash path for space - *${space.alias}* (${space.id}) : delete permanently : ${space.realPath}`
+ })
+ }
forceDeleteInDB = true
await removeFiles(space.realPath)
// emit file event
@@ -613,7 +606,7 @@ export class FilesManager {
for (const lock of await this.filesLockManager.getLocksByPath(space.dbFile)) {
this.filesLockManager.removeLock(lock.key).catch((e: Error) => this.logger.error({ tag: this.delete.name, msg: `${e}` }))
}
- // Keep the database aligned with the published trash copy before reporting a residual source.
+ // Keep the database aligned with the completed filesystem operation before reporting a residual source.
await this.filesQueries.deleteFiles(space.dbFile, isDir, forceDeleteInDB)
if (sourceCleanupError) {
this.logSourceCleanupError(sourceCleanupError)
@@ -626,9 +619,8 @@ export class FilesManager {
this.checkNotTrashRepository(space)
this.logger.log({ tag: this.downloadFromUrl.name, msg: `${downloadDto.url}` })
const dstPath = await uniqueFilePathFromDir(space.realPath)
- const tmpPath = isTaskContext
- ? taskTemporaryPath(user.tasksPath, space.task!.cacheKey, dstPath)
- : tempFilePath(user.tmpPath, temporaryPathPrefix(dstPath, 'download'))
+ const temporaryRoot = temporaryRootFromSpace(user, space)
+ const tmpPath = temporaryFilePath(temporaryRoot, dstPath, FILE_OPERATION.DOWNLOAD, space.task?.id)
const dbFile = space.dbFile
dbFile.path = path.join(dirName(dbFile.path), fileName(dstPath))
@@ -639,6 +631,7 @@ export class FilesManager {
}
try {
+ await makeDir(temporaryRoot, true)
await new DownloadFile(this.http).download(downloadDto, tmpPath, {
space,
publishedPath: dstPath,
@@ -664,16 +657,19 @@ export class FilesManager {
const isTaskContext = Boolean(space.task?.cacheKey)
// This method is currently used only by files-methods.service, which handles input sanitization.
// If it is used in other services in the future, make sure to refactor accordingly to sanitize inputs properly.
+ if (!dto.compressInDirectory && !isTaskContext) {
+ throw new FileError(HttpStatus.BAD_REQUEST, 'Archive export requires an explicit task')
+ }
if (dto.compressInDirectory) {
this.checkNotTrashRepository(space)
}
const srcPath = dirName(space.realPath)
const outputExtension = dto.extension === TAR_EXTENSION && dto.compression ? TAR_GZ_EXTENSION : dto.extension
const archiveExt = dto.name.endsWith(`.${outputExtension}`) ? '' : `.${outputExtension}`
- const dstPath = await uniqueFilePathFromDir(path.join(dto.compressInDirectory ? srcPath : user.tasksPath, `${dto.name}${archiveExt}`))
- const tmpPath = isTaskContext
- ? taskTemporaryPath(user.tasksPath, space.task!.cacheKey, dstPath)
- : tempFilePath(user.tmpPath, temporaryPathPrefix(dstPath, 'compress'))
+ const archiveName = `${dto.name}${archiveExt}`
+ const dstPath = dto.compressInDirectory ? await uniqueFilePathFromDir(path.join(srcPath, archiveName)) : path.join(user.tmpPath, archiveName)
+ const temporaryRoot = dto.compressInDirectory ? temporaryRootFromSpace(user, space) : user.tmpPath
+ const tmpPath = temporaryFilePath(temporaryRoot, archiveName, FILE_OPERATION.COMPRESS, space.task?.id)
// create lock
let fileLock: FileLock | undefined
if (dto.compressInDirectory) {
@@ -692,6 +688,7 @@ export class FilesManager {
}
// do
try {
+ await makeDir(temporaryRoot, true)
const maxArchiveSize = space.storageQuota === null ? undefined : Math.max(0, space.storageQuota - space.storageUsage)
const entries = dto.files.map((entry) => ({ ...entry, path: entry.path! }))
const onProgress = isTaskContext ? this.filesTasksTransfer.createByteProgressHandler(space) : undefined
@@ -700,7 +697,9 @@ export class FilesManager {
} else {
await createTar(tmpPath, entries, dto.compression, signal, onProgress, maxArchiveSize)
}
- await moveFiles(tmpPath, dstPath)
+ if (dto.compressInDirectory) {
+ await moveFiles(tmpPath, dstPath)
+ }
} catch (e) {
await removeFiles(tmpPath).catch((err: Error) => this.logger.error({ tag: this.compress.name, msg: `unable to remove ${tmpPath} : ${err}` }))
throw e
@@ -709,8 +708,9 @@ export class FilesManager {
await this.filesLockManager.removeLock(fileLock.key)
}
}
- // emit file event
- FileEvent.emit('event', { user, space, action: ACTION.ADD, rPath: dstPath })
+ if (dto.compressInDirectory) {
+ FileEvent.emit('event', { user, space, action: ACTION.ADD, rPath: dstPath })
+ }
}
async decompress(user: UserModel, space: SpaceEnv, signal?: AbortSignal): Promise<void> {
@@ -726,9 +726,7 @@ export class FilesManager {
}
// make temporary extraction folder
const dstPath = await uniqueFilePathFromDir(path.join(dirName(space.realPath), path.basename(space.realPath, extension)))
- const tmpPath = isTaskContext
- ? await createTaskTemporaryDir(user.tasksPath, space.task!.cacheKey, dstPath)
- : await makeTempDir(user.tmpPath, temporaryPathPrefix(dstPath, 'extract'))
+ const tmpPath = await makeTemporaryDirectory(temporaryRootFromSpace(user, space), dstPath, FILE_OPERATION.DECOMPRESS, space.task?.id)
let fileLock: FileLock | undefined
try {
// create lock
diff --git a/backend/src/applications/files/services/files-scheduler.service.spec.ts b/backend/src/applications/files/services/files-scheduler.service.spec.ts
new file mode 100644
index 00000000..ed4683a2
--- /dev/null
+++ b/backend/src/applications/files/services/files-scheduler.service.spec.ts
@@ -0,0 +1,88 @@
+import fs from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+import { FILE_OPERATION } from '../constants/operations'
+import { FilesScheduler } from './files-scheduler.service'
+
+describe(FilesScheduler.name, () => {
+ let cache: { keys: ReturnType<typeof vi.fn>; mget: ReturnType<typeof vi.fn> }
+ let service: FilesScheduler
+ let temporaryPath: string
+
+ beforeEach(async () => {
+ cache = {
+ keys: vi.fn().mockResolvedValue([]),
+ mget: vi.fn().mockResolvedValue([])
+ }
+ service = new FilesScheduler({} as any, cache as any, {} as any, {} as any, {} as any, {} as any, {} as any)
+ temporaryPath = await fs.mkdtemp(path.join(os.tmpdir(), 'files-scheduler-'))
+ })
+
+ afterEach(async () => {
+ await fs.rm(temporaryPath, { recursive: true, force: true })
+ })
+
+ it('cleans expired home entries while preserving current task artifacts', async () => {
+ const expiration = Date.now() - 1_000
+ const legacyPath = path.join(temporaryPath, 'legacy.tmp')
+ const orphanPath = path.join(temporaryPath, '~tmp-upload-orphan-report.txt')
+ const activePath = path.join(temporaryPath, '~tmp-copy-active-report.txt')
+ const recentPath = path.join(temporaryPath, 'recent.tmp')
+ await Promise.all([
+ fs.writeFile(legacyPath, 'legacy'),
+ fs.writeFile(orphanPath, 'orphan'),
+ fs.writeFile(activePath, 'active'),
+ fs.writeFile(recentPath, 'recent')
+ ])
+ const expired = new Date(expiration - 1_000)
+ await Promise.all([fs.utimes(legacyPath, expired, expired), fs.utimes(orphanPath, expired, expired), fs.utimes(activePath, expired, expired)])
+ cache.keys.mockResolvedValue(['ftask-7-active'])
+ cache.mget.mockResolvedValue([{ id: 'active', type: FILE_OPERATION.COPY }])
+
+ await (service as any).cleanupTemporaryDirectories(7, [{ includeLegacyEntries: true, path: temporaryPath }], expiration)
+
+ await expect(fs.readdir(temporaryPath).then((entries) => entries.sort())).resolves.toEqual(['recent.tmp', '~tmp-copy-active-report.txt'])
+ })
+
+ it('does not query tasks or remove legacy names from target temporary roots', async () => {
+ const legacyPath = path.join(temporaryPath, 'legacy.tmp')
+ await fs.writeFile(legacyPath, 'legacy')
+ const expired = new Date(0)
+ await fs.utimes(legacyPath, expired, expired)
+
+ await (service as any).cleanupTemporaryDirectories(7, [{ includeLegacyEntries: false, path: temporaryPath }], Date.now())
+
+ await expect(fs.readdir(temporaryPath)).resolves.toEqual(['legacy.tmp'])
+ expect(cache.keys).not.toHaveBeenCalled()
+ })
+
+ it('keeps artifacts created in a later directory while task prefixes are being resolved', async () => {
+ const expiration = Date.now() - 1_000
+ const firstPath = path.join(temporaryPath, 'first')
+ const secondPath = path.join(temporaryPath, 'second')
+ const orphanPath = path.join(firstPath, '~tmp-copy-orphan-report.txt')
+ const concurrentPath = path.join(secondPath, '~tmp-copy-concurrent-report.txt')
+ await Promise.all([fs.mkdir(firstPath), fs.mkdir(secondPath)])
+ await fs.writeFile(orphanPath, 'orphan')
+ const expired = new Date(expiration - 1_000)
+ await fs.utimes(orphanPath, expired, expired)
+ cache.keys.mockImplementationOnce(async () => {
+ await fs.writeFile(concurrentPath, 'concurrent')
+ await fs.utimes(concurrentPath, expired, expired)
+ return []
+ })
+
+ await (service as any).cleanupTemporaryDirectories(
+ 7,
+ [
+ { includeLegacyEntries: false, path: firstPath },
+ { includeLegacyEntries: false, path: secondPath }
+ ],
+ expiration
+ )
+
+ await expect(fs.readdir(firstPath)).resolves.toEqual([])
+ await expect(fs.readdir(secondPath)).resolves.toEqual(['~tmp-copy-concurrent-report.txt'])
+ expect(cache.keys).toHaveBeenCalledOnce()
+ })
+})
diff --git a/backend/src/applications/files/services/files-scheduler.service.ts b/backend/src/applications/files/services/files-scheduler.service.ts
index 8d320856..ff5448a7 100644
--- a/backend/src/applications/files/services/files-scheduler.service.ts
+++ b/backend/src/applications/files/services/files-scheduler.service.ts
@@ -5,24 +5,41 @@ import { unionAll } from 'drizzle-orm/mysql-core'
import fs from 'node:fs/promises'
import path from 'node:path'
import { currentTimeStamp } from '../../../common/shared'
+import { configuration } from '../../../configuration/config.environment'
import { Cache } from '../../../infrastructure/cache/cache.service'
import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'
import { DBSchema } from '../../../infrastructure/database/interfaces/database.interface'
import { getTablesWithFileIdColumn } from '../../../infrastructure/database/utils'
-import { USER_PATH, USER_ROLE } from '../../users/constants/user'
+import { SharesQueries } from '../../shares/services/shares-queries.service'
+import { SpaceModel } from '../../spaces/models/space.model'
+import { SpacesQueries } from '../../spaces/services/spaces-queries.service'
+import { USER_ROLE } from '../../users/constants/user'
import { UserModel } from '../../users/models/user.model'
import { users } from '../../users/schemas/users.schema'
import { CACHE_TASK_CANCEL_PREFIX, CACHE_TASK_PREFIX, CACHE_TASK_TTL, CACHE_TASK_USER_PREFIX } from '../constants/cache'
+import { TEMPORARY_FILE_PREFIX, TEMPORARY_PATH } from '../constants/files'
import { FileTask, FileTaskStatus } from '../models/file-task'
import { filesRecents } from '../schemas/files-recents.schema'
import { files } from '../schemas/files.schema'
-import { isPathExists, removeFiles } from '../utils/files'
-import { isActiveTaskStatus, taskTemporaryPrefix } from '../utils/tasks'
+import { isPathExists, removeFiles, temporaryFilePrefix } from '../utils/files'
+import { isActiveTaskStatus } from '../utils/tasks'
import { FilesContentIndexer } from './files-content-indexer.service'
import { FilesTasksManager } from './tasks/files-tasks-manager.service'
import { FilesQuotaManager } from './files-quota-manager.service'
import { FilesTrashRetention } from './files-trash-retention.service'
+interface TemporaryDirectory {
+ includeLegacyEntries: boolean
+ path: string
+}
+
+interface TemporaryDirectorySnapshot {
+ fileNames: string[]
+ path: string
+}
+
+type TemporaryDirectoriesByUser = Map<number, Map<string, TemporaryDirectory>>
+
@Injectable()
export class FilesScheduler {
private readonly TMP_FILE_MAX_AGE = 86_400_000 // one day
@@ -37,7 +54,9 @@ export class FilesScheduler {
private readonly cache: Cache,
private readonly filesContentIndexer: FilesContentIndexer,
private readonly filesQuotaManager: FilesQuotaManager,
- private readonly filesTrashRetention: FilesTrashRetention
+ private readonly filesTrashRetention: FilesTrashRetention,
+ private readonly spacesQueries: SpacesQueries,
+ private readonly sharesQueries: SharesQueries
) {}
@Timeout(5_000)
@@ -98,20 +117,54 @@ export class FilesScheduler {
@Cron(CronExpression.EVERY_DAY_AT_MIDNIGHT)
async cleanupUserTmpFiles(): Promise<void> {
this.logger.log({ tag: this.cleanupUserTmpFiles.name, msg: `START` })
+ const expiration = Date.now() - this.TMP_FILE_MAX_AGE
+ const temporaryDirectories: TemporaryDirectoriesByUser = new Map()
try {
- const expiration = Date.now() - this.TMP_FILE_MAX_AGE
- for (const user of await this.db
+ const appUsers = await this.db
.select({
id: users.id,
login: users.login,
role: users.role
})
- .from(users)) {
- await this.cleanupUserTmpFilesForUser(user, expiration)
+ .from(users)
+
+ for (const user of appUsers) {
+ const homeTmpPath = user.role === USER_ROLE.LINK ? UserModel.getLinkTmpPath(user.id) : UserModel.getTmpPath(user.login)
+ this.registerTemporaryDirectory(temporaryDirectories, user.id, homeTmpPath, true)
+ if (user.role !== USER_ROLE.LINK) {
+ await this.discoverTemporaryUsersRoot(
+ path.join(UserModel.getHomePath(user.login), TEMPORARY_PATH.STORAGE, TEMPORARY_PATH.ACTORS),
+ temporaryDirectories
+ )
+ }
+ }
+
+ if (await isPathExists(configuration.applications.files.spacesPath)) {
+ for (const entry of await fs.readdir(configuration.applications.files.spacesPath, { withFileTypes: true })) {
+ if (entry.isDirectory()) {
+ await this.discoverTemporaryUsersRoot(SpaceModel.getUsersTmpPath(entry.name), temporaryDirectories)
+ }
+ }
+ }
+
+ const externalRoots = new Set<string>()
+ for (const space of await this.spacesQueries.spacesQuotaPaths()) {
+ for (const externalPath of space.externalPaths || []) {
+ if (typeof externalPath === 'string' && externalPath) externalRoots.add(path.resolve(externalPath))
+ }
+ }
+ for (const share of await this.sharesQueries.sharesQuotaExternalPaths()) {
+ if (typeof share.externalPath === 'string' && share.externalPath) externalRoots.add(path.resolve(share.externalPath))
+ }
+ for (const externalRoot of externalRoots) {
+ await this.discoverTemporaryUsersRoot(path.join(externalRoot, TEMPORARY_PATH.STORAGE, TEMPORARY_PATH.ACTORS), temporaryDirectories)
}
} catch (e) {
this.logger.error({ tag: this.cleanupUserTmpFiles.name, msg: `${e}` })
}
+ for (const [userId, directories] of temporaryDirectories) {
+ await this.cleanupTemporaryDirectories(userId, [...directories.values()], expiration)
+ }
this.logger.log({ tag: this.cleanupUserTmpFiles.name, msg: `END` })
}
@@ -215,54 +268,79 @@ export class FilesScheduler {
this.isQuotaUpdateIsRunning = false
}
- private async cleanupUserTaskFiles(userId: number, userTasksPath: string, expiration: number): Promise<void> {
+ private registerTemporaryDirectory(
+ temporaryDirectories: TemporaryDirectoriesByUser,
+ userId: number,
+ temporaryPath: string,
+ includeLegacyEntries = false
+ ): void {
+ let userDirectories = temporaryDirectories.get(userId)
+ if (!userDirectories) {
+ userDirectories = new Map()
+ temporaryDirectories.set(userId, userDirectories)
+ }
+ const existingDirectory = userDirectories.get(temporaryPath)
+ userDirectories.set(temporaryPath, {
+ path: temporaryPath,
+ includeLegacyEntries: includeLegacyEntries || existingDirectory?.includeLegacyEntries === true
+ })
+ }
+
+ private async discoverTemporaryUsersRoot(usersTmpPath: string, temporaryDirectories: TemporaryDirectoriesByUser): Promise<void> {
try {
- // Snapshot the directory first so files created by tasks registered during cache lookup are left for the next pass.
- const fileNames = await fs.readdir(userTasksPath)
- const keys = await this.cache.keys(FilesTasksManager.getCacheKey(userId))
- const tasks: (FileTask | null | undefined)[] = keys.length ? await this.cache.mget(keys) : []
- const protectedFiles = new Set<string>()
- const protectedPrefixes: string[] = []
- let hasActiveTasks = false
- for (const task of tasks) {
- if (!task) continue
- // Exported archives remain downloadable while their completed task is still cached.
- if (task.props.compressInDirectory === false) protectedFiles.add(task.name)
- if (!isActiveTaskStatus(task.status)) continue
- hasActiveTasks = true
- // Active task staging files must survive tmp cleanup; the transfer may still publish or rollback them.
- // QUEUED tasks are included because they may start after this cache snapshot.
- protectedPrefixes.push(taskTemporaryPrefix(FilesTasksManager.getCacheKey(userId, task.id)))
- }
- for (const fileName of fileNames) {
- if (protectedFiles.has(fileName) || protectedPrefixes.some((prefix: string) => fileName.startsWith(prefix))) continue
- // An active task may have just published an unprefixed result before updating its cached name.
- await this.removeTmpFile(path.join(userTasksPath, fileName), hasActiveTasks ? expiration : undefined)
+ if (!(await isPathExists(usersTmpPath))) return
+ for (const entry of await fs.readdir(usersTmpPath, { withFileTypes: true })) {
+ const userId = Number(entry.name)
+ if (!entry.isDirectory() || !Number.isSafeInteger(userId) || userId <= 0) continue
+ this.registerTemporaryDirectory(temporaryDirectories, userId, path.join(usersTmpPath, entry.name))
}
} catch (e) {
- this.logger.error({ tag: this.cleanupUserTaskFiles.name, msg: `unable to browse ${userTasksPath} : ${e}` })
+ this.logger.error({ tag: this.cleanupUserTmpFiles.name, msg: `unable to browse ${usersTmpPath} : ${e}` })
}
}
- private async cleanupUserTmpFilesForUser(user: { id: number; login: string; role: number }, expiration: number): Promise<void> {
- const userTmpPath = UserModel.getTmpPath(user.login, user.role === USER_ROLE.GUEST, user.role === USER_ROLE.LINK)
+ private async snapshotTemporaryDirectory(directory: TemporaryDirectory): Promise<TemporaryDirectorySnapshot | undefined> {
try {
- if (!(await isPathExists(userTmpPath))) {
- return
- }
- for (const f of await fs.readdir(userTmpPath)) {
- const rPath = path.join(userTmpPath, f)
- if (f === USER_PATH.TASKS) {
- await this.cleanupUserTaskFiles(user.id, rPath, expiration)
- } else {
- await this.removeTmpFile(rPath, expiration)
- }
- }
+ if (!(await isPathExists(directory.path))) return
+ const fileNames = await fs.readdir(directory.path)
+ const candidates = directory.includeLegacyEntries ? fileNames : fileNames.filter((fileName) => fileName.startsWith(TEMPORARY_FILE_PREFIX))
+ return candidates.length === 0 ? undefined : { fileNames: candidates, path: directory.path }
} catch (e) {
- this.logger.error({ tag: this.cleanupUserTmpFiles.name, msg: `unable to browse ${userTmpPath} : ${e}` })
+ this.logger.error({ tag: this.cleanupUserTmpFiles.name, msg: `unable to browse ${directory.path} : ${e}` })
}
}
+ private async cleanupTemporaryDirectories(userId: number, directories: TemporaryDirectory[], expiration: number): Promise<void> {
+ const snapshots: TemporaryDirectorySnapshot[] = []
+ for (const directory of directories) {
+ const snapshot = await this.snapshotTemporaryDirectory(directory)
+ if (snapshot) snapshots.push(snapshot)
+ }
+ if (snapshots.length === 0) return
+
+ let protectedPrefixes: string[]
+ try {
+ // Tasks are stored before their staging entries are created. Reading the cache after all
+ // snapshots guarantees that concurrent entries are either protected or left for the next pass.
+ protectedPrefixes = await this.taskPrefixes(userId)
+ } catch (e) {
+ this.logger.error({ tag: this.cleanupUserTmpFiles.name, msg: `unable to resolve temporary files for user ${userId} : ${e}` })
+ return
+ }
+ for (const snapshot of snapshots) {
+ for (const fileName of snapshot.fileNames) {
+ if (protectedPrefixes.some((prefix) => fileName.startsWith(prefix))) continue
+ await this.removeTmpFile(path.join(snapshot.path, fileName), expiration)
+ }
+ }
+ }
+
+ private async taskPrefixes(userId: number): Promise<string[]> {
+ const keys = await this.cache.keys(FilesTasksManager.getCacheKey(userId))
+ const tasks: (FileTask | null | undefined)[] = keys.length ? await this.cache.mget(keys) : []
+ return tasks.filter((task): task is FileTask => Boolean(task?.id && task?.type)).map((task) => temporaryFilePrefix(task.type, task.id))
+ }
+
private async removeTmpFile(rPath: string, expiration?: number): Promise<void> {
try {
if (expiration === undefined || (await fs.lstat(rPath)).mtimeMs < expiration) {
diff --git a/backend/src/applications/files/services/files-search-manager.service.ts b/backend/src/applications/files/services/files-search-manager.service.ts
index 616d1044..df78f4ca 100644
--- a/backend/src/applications/files/services/files-search-manager.service.ts
+++ b/backend/src/applications/files/services/files-search-manager.service.ts
@@ -10,7 +10,7 @@ import { UserModel } from '../../users/models/user.model'
import { SearchFilesDto } from '../dto/file-operations.dto'
import { FilesContentStore } from '../models/files-content-store'
import { FileContent } from '../schemas/file-content.interface'
-import { dirName, fileName, getMimeType } from '../utils/files'
+import { dirName, fileName, getMimeType, isInternalTemporaryEntry } from '../utils/files'
import { genRegexPositiveAndNegativeTerms, normalizeSearchLimit } from '../utils/files-search'
import { FilesContentParser } from './files-content-parser.service'
import { FILE_REPOSITORY } from '../constants/operations'
@@ -73,6 +73,7 @@ export class FilesSearchManager {
const regexpTerms = genRegexPositiveAndNegativeTerms(search)
for (const { paths } of await this.filesParser.allPaths([userId], spaceIds, shareIds)) {
for (const p of paths) {
+ if (isInternalTemporaryEntry(path.basename(p.realPath))) continue
const regexBasePath = new RegExp(`^/?${escapePath(p.realPath)}/?`)
if (!p.isDir) {
const f = await this.analyzeFile(p.realPath, p.pathPrefix, regexBasePath, regexpTerms)
@@ -96,6 +97,7 @@ export class FilesSearchManager {
private async *parseFileNames(dir: string, pathPrefix: string, regexBasePath: RegExp, regexpTerms: RegExp): AsyncGenerator<FileContent> {
try {
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
+ if (isInternalTemporaryEntry(entry.name)) continue
const realPath = path.join(entry.parentPath, entry.name)
const fileContent = await this.analyzeFile(realPath, pathPrefix, regexBasePath, regexpTerms)
if (fileContent !== null) {
diff --git a/backend/src/applications/files/services/tasks/files-tasks-manager.service.spec.ts b/backend/src/applications/files/services/tasks/files-tasks-manager.service.spec.ts
index 95fab570..1b7b3d35 100644
--- a/backend/src/applications/files/services/tasks/files-tasks-manager.service.spec.ts
+++ b/backend/src/applications/files/services/tasks/files-tasks-manager.service.spec.ts
@@ -546,9 +546,10 @@ describe(FilesTasksManager.name, () => {
it('should delete completed task, remove archive file and delete cache entry', async () => {
const removeFilesSpy = vi.spyOn(filesUtils, 'removeFiles').mockResolvedValueOnce(undefined)
const stopWatchSpy = vi.spyOn(filesTasksWatcher as any, 'stopWatch').mockResolvedValueOnce(undefined)
- const user = { id: 22, tasksPath: '/tmp/tasks' } as any
+ const user = { id: 22, tmpPath: '/tmp' } as any
cacheStore.set(`${CACHE_TASK_PREFIX}-22-task-ok`, {
id: 'task-ok',
+ type: FILE_OPERATION.COMPRESS,
name: 'archive.tar',
status: FileTaskStatus.SUCCESS,
props: { compressInDirectory: false }
@@ -556,7 +557,7 @@ describe(FilesTasksManager.name, () => {
await filesTasksManager.deleteTasks(user, ['task-ok'])
- expect(removeFilesSpy).toHaveBeenCalledWith('/tmp/tasks/archive.tar')
+ expect(removeFilesSpy).toHaveBeenCalledWith('/tmp/~tmp-compress-task-ok-archive.tar')
expect(stopWatchSpy).toHaveBeenCalledWith(`${CACHE_TASK_PREFIX}-22-task-ok`)
expect(cache.mget).toHaveBeenCalledWith([`${CACHE_TASK_PREFIX}-22-task-ok`])
expect(cache.mdel).toHaveBeenCalledWith([`${CACHE_TASK_PREFIX}-22-task-ok`])
@@ -573,19 +574,22 @@ describe(FilesTasksManager.name, () => {
it('should ignore active tasks when deleting all tasks', async () => {
const removeFilesSpy = vi.spyOn(filesUtils, 'removeFiles').mockResolvedValue(undefined)
const stopWatchSpy = vi.spyOn(filesTasksWatcher as any, 'stopWatch').mockResolvedValue(undefined)
- const user = { id: 31, tasksPath: '/tmp/tasks' } as any
+ const user = { id: 31, tmpPath: '/tmp' } as any
cacheStore.set(`${CACHE_TASK_PREFIX}-31-task-pending`, {
id: 'task-pending',
+ type: FILE_OPERATION.COMPRESS,
status: FileTaskStatus.PENDING,
props: { compressInDirectory: false }
})
cacheStore.set(`${CACHE_TASK_PREFIX}-31-task-queued`, {
id: 'task-queued',
+ type: FILE_OPERATION.COMPRESS,
status: FileTaskStatus.QUEUED,
props: { compressInDirectory: false }
})
cacheStore.set(`${CACHE_TASK_PREFIX}-31-task-done`, {
id: 'task-done',
+ type: FILE_OPERATION.COMPRESS,
name: 'done.tar',
status: FileTaskStatus.SUCCESS,
props: { compressInDirectory: true }
@@ -609,11 +613,12 @@ describe(FilesTasksManager.name, () => {
it('should reject archive download when task is not applicable', async () => {
cacheStore.set(`${CACHE_TASK_PREFIX}-40-task-a`, {
id: 'task-a',
+ type: FILE_OPERATION.COMPRESS,
status: FileTaskStatus.PENDING,
props: { compressInDirectory: false }
})
- await expect(filesTasksManager.downloadArchive({ id: 40, tasksPath: '/tmp/tasks' } as any, 'task-a', {} as any, {} as any)).rejects.toEqual(
+ await expect(filesTasksManager.downloadArchive({ id: 40, tmpPath: '/tmp' } as any, 'task-a', {} as any, {} as any)).rejects.toEqual(
new HttpException('Not applicable', HttpStatus.BAD_REQUEST)
)
})
@@ -621,13 +626,14 @@ describe(FilesTasksManager.name, () => {
it('should map send-file checks error to HttpException during archive download', async () => {
cacheStore.set(`${CACHE_TASK_PREFIX}-50-task-ok`, {
id: 'task-ok',
+ type: FILE_OPERATION.COMPRESS,
name: 'archive.tar',
status: FileTaskStatus.SUCCESS,
props: { compressInDirectory: false }
})
vi.spyOn(SendFile.prototype, 'checks').mockRejectedValueOnce({ message: 'Location not found', httpCode: 404 } as any)
- const downloadPromise = filesTasksManager.downloadArchive({ id: 50, tasksPath: '/tmp/tasks' } as any, 'task-ok', {} as any, {} as any)
+ const downloadPromise = filesTasksManager.downloadArchive({ id: 50, tmpPath: '/tmp' } as any, 'task-ok', {} as any, {} as any)
await expect(downloadPromise).rejects.toThrow(HttpException)
await expect(downloadPromise).rejects.toThrow('Location not found')
await expect(downloadPromise).rejects.toSatisfy((e: HttpException) => {
@@ -639,6 +645,7 @@ describe(FilesTasksManager.name, () => {
const streamable = { ok: true } as any
cacheStore.set(`${CACHE_TASK_PREFIX}-60-task-ok`, {
id: 'task-ok',
+ type: FILE_OPERATION.COMPRESS,
name: 'archive.tar',
status: FileTaskStatus.SUCCESS,
props: { compressInDirectory: false }
@@ -646,7 +653,7 @@ describe(FilesTasksManager.name, () => {
vi.spyOn(SendFile.prototype, 'checks').mockResolvedValueOnce(undefined)
vi.spyOn(SendFile.prototype, 'stream').mockResolvedValueOnce(streamable)
- const result = await filesTasksManager.downloadArchive({ id: 60, tasksPath: '/tmp/tasks' } as any, 'task-ok', { raw: {} } as any, {} as any)
+ const result = await filesTasksManager.downloadArchive({ id: 60, tmpPath: '/tmp' } as any, 'task-ok', { raw: {} } as any, {} as any)
expect(result).toBe(streamable)
})
diff --git a/backend/src/applications/files/services/tasks/files-tasks-manager.service.ts b/backend/src/applications/files/services/tasks/files-tasks-manager.service.ts
index 461efd67..bb38ef49 100644
--- a/backend/src/applications/files/services/tasks/files-tasks-manager.service.ts
+++ b/backend/src/applications/files/services/tasks/files-tasks-manager.service.ts
@@ -7,7 +7,7 @@ import { currentTimeStamp } from '../../../../common/shared'
import { Cache } from '../../../../infrastructure/cache/cache.service'
import { SpaceEnv } from '../../../spaces/models/space-env.model'
import { SpacesManager } from '../../../spaces/services/spaces-manager.service'
-import { realTrashPathFromSpace } from '../../../spaces/utils/paths'
+import { trashTargetFromSpace } from '../../../spaces/utils/paths'
import { UserModel } from '../../../users/models/user.model'
import { CACHE_TASK_CANCEL_PREFIX, CACHE_TASK_PREFIX, CACHE_TASK_TTL } from '../../constants/cache'
import { FILE_OPERATION } from '../../constants/operations'
@@ -15,7 +15,7 @@ import { CopyMoveFileDto } from '../../dto/file-operations.dto'
import type { FileTaskQueueItem } from '../../interfaces/file-task-queue.interface'
import type { FileTasksPollResponse } from '../../interfaces/file-task.interface'
import { FileTask, FileTaskProps, FileTaskStatus } from '../../models/file-task'
-import { dirName, fileName, removeFiles } from '../../utils/files'
+import { dirName, fileName, removeFiles, temporaryFilePath } from '../../utils/files'
import { SendFile } from '../../utils/send-file'
import { isTaskCancellable } from '../../utils/tasks'
import { FilesMethods } from '../files-methods.service'
@@ -59,7 +59,7 @@ export class FilesTasksManager implements OnModuleDestroy {
const cancellable = await isTaskCancellable(type, space.realPath, dstPath)
const newTask = new FileTask(taskId, type, dirName(space.url), fileName(space.url), cancellable)
await this.storeTask(cacheKey, newTask, FileTaskStatus.QUEUED)
- space.task = { cacheKey, props: {} }
+ space.task = { id: taskId, type, cacheKey, props: {} }
await this.filesTasksQueue.enqueue(user.id, { cacheKey, dto, method, space, task: newTask, user }, (task) => this.startQueuedTask(task))
return newTask
}
@@ -135,8 +135,8 @@ export class FilesTasksManager implements OnModuleDestroy {
if (!task || task.status !== FileTaskStatus.SUCCESS || task.props.compressInDirectory !== false) {
throw new HttpException('Not applicable', HttpStatus.BAD_REQUEST)
}
- const rPath = path.join(user.tasksPath, task.name)
- const sendFile = new SendFile(rPath)
+ const rPath = this.downloadableArchivePath(user, task)
+ const sendFile = new SendFile(rPath, task.name)
try {
await sendFile.checks()
} catch (e) {
@@ -148,10 +148,14 @@ export class FilesTasksManager implements OnModuleDestroy {
private async cleanupTask(user: UserModel, cacheKey: string, task: FileTask): Promise<void> {
this.filesTasksWatcher.stopWatch(cacheKey)
if (task.props.compressInDirectory === false) {
- await removeFiles(path.join(user.tasksPath, task.name)).catch((e: Error) => this.logger.error({ tag: this.deleteTasks.name, msg: `${e}` }))
+ await removeFiles(this.downloadableArchivePath(user, task)).catch((e: Error) => this.logger.error({ tag: this.deleteTasks.name, msg: `${e}` }))
}
}
+ private downloadableArchivePath(user: UserModel, task: FileTask): string {
+ return temporaryFilePath(user.tmpPath, task.name, task.type, task.id)
+ }
+
private async storeTask(cacheKey: string, task: FileTask, status = FileTaskStatus.PENDING) {
task.startedAt = currentTimeStamp(null, true)
task.status = status
@@ -195,7 +199,7 @@ export class FilesTasksManager implements OnModuleDestroy {
if (!storedTask || storedTask.status !== FileTaskStatus.QUEUED) return false
task.task.status = FileTaskStatus.PENDING
task.task.startedAt = currentTimeStamp(null, true)
- task.space.task = { cacheKey: task.cacheKey, props: task.task.props }
+ task.space.task = { id: task.task.id, type: task.task.type, cacheKey: task.cacheKey, props: task.task.props }
await this.cache.set(task.cacheKey, task.task, CACHE_TASK_TTL)
this.runTask(task)
return true
@@ -275,9 +279,9 @@ export class FilesTasksManager implements OnModuleDestroy {
return (await this.spacesManager.spaceEnv(user, dstUrl.split('/'))).realPath
}
if (type === FILE_OPERATION.DELETE && !space.inTrashRepository) {
- const baseTrashPath = realTrashPathFromSpace(user, space)
- if (baseTrashPath) {
- return path.join(baseTrashPath, dirName(space.dbFile.path), fileName(space.realPath))
+ const trashTarget = trashTargetFromSpace(user, space)
+ if (trashTarget?.mode === 'trash') {
+ return path.join(trashTarget.path, dirName(space.dbFile.path), fileName(space.realPath))
}
}
} catch {
diff --git a/backend/src/applications/files/services/tasks/files-tasks-transfer.service.spec.ts b/backend/src/applications/files/services/tasks/files-tasks-transfer.service.spec.ts
index eec8f82c..d50b705b 100644
--- a/backend/src/applications/files/services/tasks/files-tasks-transfer.service.spec.ts
+++ b/backend/src/applications/files/services/tasks/files-tasks-transfer.service.spec.ts
@@ -2,12 +2,14 @@ import fs, { access, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/p
import os from 'node:os'
import path from 'node:path'
import * as filesUtils from '../../utils/files'
-import { taskTemporaryPrefix } from '../../utils/tasks'
+import * as spacesPathUtils from '../../../spaces/utils/paths'
+import { FILE_OPERATION } from '../../constants/operations'
import { FilesTasksTransfer } from './files-tasks-transfer.service'
import { SourceCleanupError } from '../../models/file-error'
describe(FilesTasksTransfer.name, () => {
const cacheKey = 'ftask-7-task-id'
+ const taskId = 'task-id'
let service: FilesTasksTransfer
let tmpDir: string
let srcPath: string
@@ -20,6 +22,7 @@ describe(FilesTasksTransfer.name, () => {
srcPath = path.join(tmpDir, 'source.txt')
dstPath = path.join(tmpDir, 'destination.txt')
stagingDir = path.join(tmpDir, 'tasks')
+ vi.spyOn(spacesPathUtils, 'temporaryRootFromSpace').mockReturnValue(stagingDir)
})
afterEach(async () => {
@@ -27,8 +30,10 @@ describe(FilesTasksTransfer.name, () => {
vi.restoreAllMocks()
})
- const copyAbortable = (options: Record<string, any>) => (service as any).copyAbortable(srcPath, dstPath, options)
- const moveAbortable = (options: Record<string, any>) => (service as any).moveAbortable(srcPath, dstPath, options)
+ const copyAbortable = (options: Record<string, any>) =>
+ (service as any).copyAbortable(srcPath, dstPath, { executionId: taskId, operation: FILE_OPERATION.COPY, ...options })
+ const moveAbortable = (options: Record<string, any>) =>
+ (service as any).moveAbortable(srcPath, dstPath, { executionId: taskId, operation: FILE_OPERATION.MOVE, ...options })
const mockCrossDevice = () => {
vi.spyOn(fs, 'lstat').mockResolvedValueOnce({ dev: 1 } as any)
@@ -39,12 +44,12 @@ describe(FilesTasksTransfer.name, () => {
const content = 'content'
const srcSpace = {
realPath: srcPath,
- task: { cacheKey, props: {} }
+ task: { id: taskId, type: FILE_OPERATION.COPY, cacheKey, props: {} }
} as any
const dstSpace = { realPath: dstPath } as any
await writeFile(srcPath, content)
- await service.copy({ tasksPath: stagingDir } as any, srcSpace, dstSpace, false, false, false, new AbortController().signal, vi.fn())
+ await service.copy({ id: 7 } as any, srcSpace, dstSpace, false, false, false, new AbortController().signal, vi.fn())
expect(srcSpace.task.props).toMatchObject({
progress: 100,
@@ -54,6 +59,19 @@ describe(FilesTasksTransfer.name, () => {
await expect(readFile(dstPath, 'utf8')).resolves.toBe(content)
})
+ it('ignores internal temporary entries during recursive task copies', async () => {
+ srcPath = path.join(tmpDir, 'source')
+ dstPath = path.join(tmpDir, 'destination')
+ await fs.mkdir(path.join(srcPath, '.sync-in-tmp', 'users'), { recursive: true })
+ await writeFile(path.join(srcPath, 'report.txt'), 'report')
+ await writeFile(path.join(srcPath, '.sync-in.partial'), 'partial')
+ await writeFile(path.join(srcPath, '.sync-in-tmp', 'users', 'staging.bin'), 'staging')
+
+ await (service as any).copyEntry(srcPath, dstPath, true, true, new AbortController().signal)
+
+ await expect(readdir(dstPath)).resolves.toEqual(['report.txt'])
+ })
+
it('tracks extracted entries and implicit parent directories', () => {
const space = {
task: { cacheKey, props: { progress: 1 } }
@@ -75,7 +93,7 @@ describe(FilesTasksTransfer.name, () => {
it('publishes a copied file only after the commit hook', async () => {
const signal = new AbortController().signal
- const temporaryPath = path.join(stagingDir, `${taskTemporaryPrefix(cacheKey)}destination.txt`)
+ const temporaryPath = filesUtils.temporaryFilePath(stagingDir, dstPath, FILE_OPERATION.COPY, taskId)
let transferredBytes = 0
await writeFile(srcPath, 'content')
@@ -84,7 +102,6 @@ describe(FilesTasksTransfer.name, () => {
await expect(access(dstPath)).rejects.toMatchObject({ code: 'ENOENT' })
await expect(readFile(temporaryPath, 'utf8')).resolves.toBe('content')
},
- cacheKey,
onTransferStart: vi.fn(),
onProgress: (bytes: number) => {
transferredBytes += bytes
@@ -95,7 +112,7 @@ describe(FilesTasksTransfer.name, () => {
expect(transferredBytes).toBe(Buffer.byteLength('content'))
expect(path.dirname(temporaryPath)).toBe(stagingDir)
- expect(path.basename(temporaryPath)).toBe(`${taskTemporaryPrefix(cacheKey)}destination.txt`)
+ expect(path.basename(temporaryPath)).toBe(`${filesUtils.temporaryFilePrefix(FILE_OPERATION.COPY, taskId)}destination.txt`)
await expect(readFile(dstPath, 'utf8')).resolves.toBe('content')
await expect(access(temporaryPath)).rejects.toMatchObject({ code: 'ENOENT' })
})
@@ -105,7 +122,7 @@ describe(FilesTasksTransfer.name, () => {
await writeFile(srcPath, 'content')
controller.abort()
- await expect(copyAbortable({ cacheKey, signal: controller.signal, stagingDir })).rejects.toMatchObject({ name: 'AbortError' })
+ await expect(copyAbortable({ signal: controller.signal, stagingDir })).rejects.toMatchObject({ name: 'AbortError' })
await expect(access(dstPath)).rejects.toMatchObject({ code: 'ENOENT' })
await expect(readdir(stagingDir)).resolves.toEqual([])
@@ -122,7 +139,6 @@ describe(FilesTasksTransfer.name, () => {
beforeCommit: async () => {
destinationPrepared = true
},
- cacheKey,
onTransferStart: () => {
expect(destinationPrepared).toBe(true)
transferStarted = true
@@ -142,7 +158,7 @@ describe(FilesTasksTransfer.name, () => {
await writeFile(dstPath, 'existing content')
mockCrossDevice()
- await expect(copyAbortable({ cacheKey, signal, stagingDir })).rejects.toMatchObject({ code: 'EEXIST' })
+ await expect(copyAbortable({ signal, stagingDir })).rejects.toMatchObject({ code: 'EEXIST' })
await expect(readFile(dstPath, 'utf8')).resolves.toBe('existing content')
})
@@ -156,7 +172,7 @@ describe(FilesTasksTransfer.name, () => {
controller.abort()
mockCrossDevice()
- await expect(copyAbortable({ beforeCommit, cacheKey, overwrite: true, signal: controller.signal, stagingDir })).rejects.toMatchObject({
+ await expect(copyAbortable({ beforeCommit, overwrite: true, signal: controller.signal, stagingDir })).rejects.toMatchObject({
name: 'AbortError'
})
@@ -175,14 +191,14 @@ describe(FilesTasksTransfer.name, () => {
vi.spyOn(fs, 'stat').mockResolvedValueOnce({ dev: 2 } as any)
vi.spyOn(filesUtils, 'removeFiles').mockRejectedValueOnce(new Error('cleanup failed'))
- await expect(copyAbortable({ cacheKey, signal, stagingDir })).rejects.toBe(transferError)
+ await expect(copyAbortable({ signal, stagingDir })).rejects.toBe(transferError)
})
it('copies then removes the source for a cross-device move', async () => {
const signal = new AbortController().signal
await writeFile(srcPath, 'content')
- await moveAbortable({ cacheKey, crossDevice: true, signal, stagingDir })
+ await moveAbortable({ crossDevice: true, signal, stagingDir })
await expect(readFile(dstPath, 'utf8')).resolves.toBe('content')
await expect(access(srcPath)).rejects.toMatchObject({ code: 'ENOENT' })
@@ -195,7 +211,7 @@ describe(FilesTasksTransfer.name, () => {
await writeFile(srcPath, 'content')
vi.spyOn(filesUtils, 'removeFiles').mockRejectedValueOnce(error)
- const cleanupError = await moveAbortable({ cacheKey, crossDevice: true, signal, stagingDir })
+ const cleanupError = await moveAbortable({ crossDevice: true, signal, stagingDir })
expect(cleanupError).toBeInstanceOf(SourceCleanupError)
expect(cleanupError).toMatchObject({
diff --git a/backend/src/applications/files/services/tasks/files-tasks-transfer.service.ts b/backend/src/applications/files/services/tasks/files-tasks-transfer.service.ts
index a0de7d49..af9fee53 100644
--- a/backend/src/applications/files/services/tasks/files-tasks-transfer.service.ts
+++ b/backend/src/applications/files/services/tasks/files-tasks-transfer.service.ts
@@ -5,11 +5,22 @@ import path from 'node:path'
import { pipeline } from 'node:stream/promises'
import { UserModel } from '../../../users/models/user.model'
import { SpaceEnv } from '../../../spaces/models/space-env.model'
+import { temporaryRootFromSpace } from '../../../spaces/utils/paths'
import { DEFAULT_HIGH_WATER_MARK } from '../../constants/files'
+import { FILE_OPERATION } from '../../constants/operations'
import { FileTaskEvent } from '../../events/file-events'
import type { FileTaskCopyTaskOptions, FileTaskExtractionEntry, FileTaskTransferOptions } from '../../interfaces/file-task.interface'
-import { createProgressTransform, fileSize, isCrossDevice, isPathExists, moveFiles, removeFiles } from '../../utils/files'
-import { countDirEntriesAndSize, taskTemporaryPath } from '../../utils/tasks'
+import {
+ createProgressTransform,
+ fileSize,
+ isCrossDevice,
+ isInternalTemporaryEntry,
+ isPathExists,
+ moveFiles,
+ removeFiles,
+ temporaryFilePath
+} from '../../utils/files'
+import { countDirEntriesAndSize } from '../../utils/tasks'
import { SourceCleanupError } from '../../models/file-error'
@Injectable()
@@ -27,13 +38,14 @@ export class FilesTasksTransfer {
await this.initializeTaskProps(srcSpace, isDir)
await this.copyAbortable(srcSpace.realPath, dstSpace.realPath, {
beforeCommit: this.prepareTaskDestination(srcSpace, dstSpace, overwrite, deleteDestination),
- cacheKey: srcSpace.task!.cacheKey,
+ executionId: srcSpace.task!.id,
onProgress: this.createByteProgressHandler(srcSpace),
onTransferStart: () => this.startTransferTaskWatch(srcSpace, dstSpace.realPath),
+ operation: FILE_OPERATION.COPY,
overwrite,
recursive,
signal,
- stagingDir: user.tasksPath
+ stagingDir: temporaryRootFromSpace(user, dstSpace)
})
}
@@ -50,20 +62,21 @@ export class FilesTasksTransfer {
const beforeCommit = this.prepareTaskDestination(srcSpace, dstSpace, overwrite, deleteDestination)
return this.moveAbortable(srcSpace.realPath, dstSpace.realPath, {
beforeCommit,
- cacheKey: srcSpace.task!.cacheKey,
crossDevice: true,
+ executionId: srcSpace.task!.id,
onProgress: this.createByteProgressHandler(srcSpace),
onTransferStart: () => this.startTransferTaskWatch(srcSpace, dstSpace.realPath),
+ operation: FILE_OPERATION.MOVE,
overwrite,
signal,
- stagingDir: user.tasksPath
+ stagingDir: temporaryRootFromSpace(user, dstSpace)
})
}
async delete(
- user: UserModel,
space: SpaceEnv,
trashFile: string,
+ stagingDir: string,
isDir: boolean,
signal: AbortSignal | undefined,
prepareDestination: () => Promise<void>
@@ -78,13 +91,14 @@ export class FilesTasksTransfer {
}
return this.moveAbortable(space.realPath, trashFile, {
beforeCommit: prepareDestination,
- cacheKey: space.task!.cacheKey,
crossDevice: true,
+ executionId: space.task!.id,
onProgress: this.createByteProgressHandler(space),
onTransferStart: () => this.startTransferTaskWatch(space, trashFile),
+ operation: FILE_OPERATION.DELETE,
overwrite: true,
signal,
- stagingDir: user.tasksPath
+ stagingDir
})
}
@@ -152,16 +166,17 @@ export class FilesTasksTransfer {
private async copyAbortable(srcPath: string, dstPath: string, options: FileTaskCopyTaskOptions): Promise<void> {
const {
beforeCommit,
- cacheKey,
+ executionId,
onProgress,
onTransferStart,
+ operation,
overwrite = false,
preserveTimestamps = true,
recursive = true,
signal,
stagingDir = path.dirname(dstPath)
} = options
- const temporaryPath = taskTemporaryPath(stagingDir, cacheKey, dstPath)
+ const temporaryPath = temporaryFilePath(stagingDir, dstPath, operation, executionId)
await fs.mkdir(stagingDir, { recursive: true })
const copyDirectlyToDestination = await isCrossDevice(stagingDir, dstPath)
@@ -198,7 +213,7 @@ export class FilesTasksTransfer {
}
private async moveAbortable(srcPath: string, dstPath: string, options: FileTaskTransferOptions): Promise<SourceCleanupError | undefined> {
- const { beforeCommit, cacheKey, onProgress, onTransferStart, overwrite = false, signal, stagingDir } = options
+ const { beforeCommit, executionId, onProgress, onTransferStart, operation, overwrite = false, signal, stagingDir } = options
const crossDevice = options.crossDevice ?? (await isCrossDevice(srcPath, dstPath))
if (!crossDevice) {
signal.throwIfAborted()
@@ -206,7 +221,16 @@ export class FilesTasksTransfer {
await moveFiles(srcPath, dstPath, overwrite)
return
}
- await this.copyAbortable(srcPath, dstPath, { beforeCommit, cacheKey, onProgress, onTransferStart, overwrite, signal, stagingDir })
+ await this.copyAbortable(srcPath, dstPath, {
+ beforeCommit,
+ executionId,
+ onProgress,
+ onTransferStart,
+ operation,
+ overwrite,
+ signal,
+ stagingDir
+ })
try {
await removeFiles(srcPath)
} catch (cause) {
@@ -228,6 +252,7 @@ export class FilesTasksTransfer {
await fs.mkdir(dstPath)
if (recursive) {
for (const entry of await fs.readdir(srcPath)) {
+ if (isInternalTemporaryEntry(entry)) continue
await this.copyEntry(path.join(srcPath, entry), path.join(dstPath, entry), true, preserveTimestamps, signal, onProgress)
}
}
diff --git a/backend/src/applications/files/utils/download-file.spec.ts b/backend/src/applications/files/utils/download-file.spec.ts
index 08b3b6ad..c44e0b5d 100644
--- a/backend/src/applications/files/utils/download-file.spec.ts
+++ b/backend/src/applications/files/utils/download-file.spec.ts
@@ -10,8 +10,7 @@ import type { Mock } from 'vitest'
import { FILE_ERROR } from '../constants/errors'
vi.mock('./files', () => ({
- writeUploadFromStream: vi.fn(),
- temporaryPathPrefix: vi.fn()
+ writeUploadFromStream: vi.fn()
}))
vi.mock('node:dns/promises', () => ({
lookup: vi.fn()
diff --git a/backend/src/applications/files/utils/files.spec.ts b/backend/src/applications/files/utils/files.spec.ts
index 1ad5a33f..03c5239a 100644
--- a/backend/src/applications/files/utils/files.spec.ts
+++ b/backend/src/applications/files/utils/files.spec.ts
@@ -1,5 +1,5 @@
import { HttpStatus } from '@nestjs/common'
-import fs, { access, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
+import fs, { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { Readable } from 'node:stream'
@@ -10,11 +10,13 @@ import { storageQuotaExceededError } from './errors'
import {
createSizeLimiter,
isCrossDevice,
+ isInternalTemporaryEntry,
+ isInternalTemporaryPath,
isPathInside,
- makeTempDir,
sanitizeName,
- tempFilePath,
- temporaryPathPrefix,
+ temporaryFileName,
+ temporaryFilePath,
+ temporaryFilePrefix,
writeFromStream,
writeFromStreamAndChecksum,
writeUploadFromStream,
@@ -56,6 +58,22 @@ describe(isPathInside.name, () => {
})
})
+describe(isInternalTemporaryEntry.name, () => {
+ it('reserves storage and Sync temporary names only', () => {
+ expect(isInternalTemporaryEntry('.sync-in-tmp')).toBe(true)
+ expect(isInternalTemporaryEntry('.sync-in.uploading')).toBe(true)
+ expect(isInternalTemporaryEntry('.sync-in')).toBe(false)
+ expect(isInternalTemporaryEntry('.sync-in-tmp-user')).toBe(false)
+ })
+
+ it('checks path segments relative to the exposed repository root', () => {
+ const basePath = path.join(path.sep, 'data', 'users', '.sync-in.user')
+
+ expect(isInternalTemporaryPath(basePath, path.join(basePath, 'documents', '.sync-in.uploading'))).toBe(true)
+ expect(isInternalTemporaryPath(basePath, path.join(basePath, 'documents'))).toBe(false)
+ })
+})
+
describe(sanitizeName.name, () => {
it('removes separators before stripping trailing path segments', () => {
expect(sanitizeName('./')).toBe('')
@@ -210,45 +228,18 @@ describe('upload stream writers', () => {
})
})
-describe(makeTempDir.name, () => {
- let tmpDir: string
+describe(temporaryFilePath.name, () => {
+ it('uses the operation, execution id and sanitized basename', () => {
+ const parentPath = path.join(path.sep, 'storage', '.sync-in-tmp', 'users', '42')
- beforeEach(async () => {
- tmpDir = await mkdtemp(path.join(os.tmpdir(), 'make-temp-dir-'))
+ expect(temporaryFilePath(parentPath, '../report.pdf', 'upload', 'task/id')).toBe(path.join(parentPath, '~tmp-upload-task-id-report.pdf'))
+ expect(temporaryFilePrefix('compress', 'task-id')).toBe('~tmp-compress-task-id-')
})
- afterEach(async () => {
- await rm(tmpDir, { recursive: true, force: true })
- })
-
- it('creates distinct directories with the requested prefix', async () => {
- const firstPath = await makeTempDir(tmpDir, 'extract-')
- const secondPath = await makeTempDir(tmpDir, 'extract-')
-
- expect(firstPath).not.toBe(secondPath)
- expect(path.basename(firstPath)).toMatch(/^extract-/)
- await expect(access(firstPath)).resolves.toBeUndefined()
- await expect(access(secondPath)).resolves.toBeUndefined()
- })
-})
-
-describe(tempFilePath.name, () => {
- it('returns safe distinct paths with the requested parent and prefix', () => {
- const parentPath = path.join(path.sep, 'tmp', 'user')
- const firstPath = tempFilePath(parentPath, 'archive-compress-')
- const secondPath = tempFilePath(parentPath, 'archive-compress-')
-
- expect(firstPath).not.toBe(secondPath)
- expect(path.dirname(firstPath)).toBe(parentPath)
- expect(path.basename(firstPath)).toMatch(/^archive-compress-/)
- expect(path.dirname(tempFilePath(parentPath, path.join('..', 'archive-')))).toBe(parentPath)
- })
-})
+ it('keeps generated names within the filesystem byte limit while preserving the extension', () => {
+ const name = temporaryFileName(`${'é'.repeat(200)}.tar.gz`, 'compress', 'task-id')
-describe(temporaryPathPrefix.name, () => {
- it('uses the destination name and operation', () => {
- expect(temporaryPathPrefix('/destination/report.pdf', 'upload')).toBe('report.pdf-upload-')
- expect(temporaryPathPrefix('/destination', 'extract')).toBe('destination-extract-')
- expect(temporaryPathPrefix('', 'download')).toBe('file-download-')
+ expect(Buffer.byteLength(name)).toBeLessThanOrEqual(255)
+ expect(name.endsWith('.gz')).toBe(true)
})
})
diff --git a/backend/src/applications/files/utils/files.ts b/backend/src/applications/files/utils/files.ts
index f9b460f1..cff8ec20 100644
--- a/backend/src/applications/files/utils/files.ts
+++ b/backend/src/applications/files/utils/files.ts
@@ -10,7 +10,8 @@ import { Readable, Transform } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { formatDateISOString } from '../../../common/functions'
import { currentTimeStamp, isValidFileName, regExpPreventPathTraversal } from '../../../common/shared'
-import { DEFAULT_CHECKSUM_ALGORITHM, DEFAULT_HIGH_WATER_MARK, EXTRA_MIMES_TYPE } from '../constants/files'
+import { DEFAULT_CHECKSUM_ALGORITHM, DEFAULT_HIGH_WATER_MARK, EXTRA_MIMES_TYPE, TEMPORARY_FILE_PREFIX, TEMPORARY_PATH } from '../constants/files'
+import { SYNC_TEMPORARY_FILE_PREFIX } from '../../sync/constants/sync'
import type { FileDBProps } from '../interfaces/file-db-props.interface'
import type { FileProps } from '../interfaces/file-props.interface'
import { FileError } from '../models/file-error'
@@ -32,6 +33,16 @@ export function isPathInside(basePath: string, candidatePath: string, allowBaseP
return resolvedCandidatePath.startsWith(basePathPrefix)
}
+export function isInternalTemporaryEntry(name: string): boolean {
+ return name === TEMPORARY_PATH.STORAGE || name.startsWith(SYNC_TEMPORARY_FILE_PREFIX)
+}
+
+export function isInternalTemporaryPath(basePath: string, candidatePath: string): boolean {
+ const relativePath = path.relative(path.resolve(basePath), path.resolve(candidatePath))
+ if (!relativePath || path.isAbsolute(relativePath) || relativePath.startsWith(`..${path.sep}`) || relativePath === '..') return false
+ return relativePath.split(path.sep).some(isInternalTemporaryEntry)
+}
+
export function sanitizeName(name: string): string {
return name
.replace(/[/\\]/g, '') // remove slashes
@@ -110,17 +121,58 @@ export function makeDir(rPath: string, recursive?: boolean): Promise<string> {
return fs.mkdir(rPath, { recursive: recursive })
}
-export async function makeTempDir(parentPath: string, prefix: string): Promise<string> {
- await makeDir(parentPath, true)
- return fs.mkdtemp(path.join(parentPath, prefix))
+const MAX_TEMPORARY_FILE_NAME_BYTES = 255
+
+function temporaryNameSegment(value: string, label: string): string {
+ const segment = value.replace(/[^a-zA-Z0-9_-]+/g, '-').replace(/^-+|-+$/g, '')
+ if (!segment) throw new Error(`Invalid temporary-file ${label}`)
+ return segment
}
-export function tempFilePath(parentPath: string, prefix: string): string {
- return path.join(parentPath, `${path.basename(prefix)}${crypto.randomUUID()}`)
+function truncateUtf8(value: string, maxBytes: number): string {
+ let result = ''
+ let length = 0
+ for (const character of value) {
+ const characterLength = Buffer.byteLength(character)
+ if (length + characterLength > maxBytes) break
+ result += character
+ length += characterLength
+ }
+ return result
}
-export function temporaryPathPrefix(targetPath: string, operation: 'upload' | 'download' | 'compress' | 'extract'): string {
- return `${fileName(targetPath) || 'file'}-${operation}-`
+function truncateTemporaryBasename(name: string, maxBytes: number): string {
+ if (Buffer.byteLength(name) <= maxBytes) return name
+ const extension = path.extname(name)
+ const extensionLength = Buffer.byteLength(extension)
+ if (extension && extensionLength < maxBytes) {
+ const stem = path.basename(name, extension)
+ return `${truncateUtf8(stem, maxBytes - extensionLength)}${extension}`
+ }
+ return truncateUtf8(name, maxBytes)
+}
+
+export function temporaryFilePrefix(operation: string, executionId: string): string {
+ return `${TEMPORARY_FILE_PREFIX}${temporaryNameSegment(operation, 'operation')}-${temporaryNameSegment(executionId, 'execution id')}-`
+}
+
+export function temporaryFileName(targetPath: string, operation: string, executionId: string = crypto.randomUUID()): string {
+ const prefix = temporaryFilePrefix(operation, executionId)
+ const basename = sanitizeName(fileName(targetPath)) || 'file'
+ const basenameBytes = MAX_TEMPORARY_FILE_NAME_BYTES - Buffer.byteLength(prefix)
+ if (basenameBytes < 1) throw new Error('Temporary-file prefix exceeds the filesystem filename limit')
+ return `${prefix}${truncateTemporaryBasename(basename, basenameBytes)}`
+}
+
+export function temporaryFilePath(parentPath: string, targetPath: string, operation: string, executionId?: string): string {
+ return path.join(parentPath, temporaryFileName(targetPath, operation, executionId))
+}
+
+export async function makeTemporaryDirectory(parentPath: string, targetPath: string, operation: string, executionId?: string): Promise<string> {
+ await makeDir(parentPath, true)
+ const temporaryPath = temporaryFilePath(parentPath, targetPath, operation, executionId)
+ await fs.mkdir(temporaryPath)
+ return temporaryPath
}
export function getMimeType(fPath: string, isDir: boolean): string {
@@ -201,7 +253,12 @@ export async function copyFiles(srcPath: string, dstPath: string, overwrite = fa
await fs.utimes(dstPath, stat.atime, stat.mtime)
}
} else {
- await fse.copy(srcPath, dstPath, { overwrite, preserveTimestamps: preserveTimestamps })
+ const resolvedSrcPath = path.resolve(srcPath)
+ await fse.copy(srcPath, dstPath, {
+ overwrite,
+ preserveTimestamps,
+ filter: (entryPath) => path.resolve(entryPath) === resolvedSrcPath || !isInternalTemporaryEntry(path.basename(entryPath))
+ })
}
}
@@ -339,7 +396,8 @@ export function copyFileContent(srcPath: string, dstPath: string): Promise<void>
export async function walkDir(
rPath: string,
onEntry: (entry: Dirent, entryPath: string) => Promise<void> | void,
- errors?: Record<string, string>
+ errors?: Record<string, string>,
+ includeEntry?: (entry: Dirent, entryPath: string) => boolean
): Promise<void> {
let entries: Dirent[]
@@ -353,9 +411,10 @@ export async function walkDir(
for (const entry of entries) {
const entryPath = path.join(rPath, entry.name)
+ if (includeEntry && !includeEntry(entry, entryPath)) continue
await onEntry(entry, entryPath)
if (entry.isDirectory()) {
- await walkDir(entryPath, onEntry, errors)
+ await walkDir(entryPath, onEntry, errors, includeEntry)
}
}
}
diff --git a/backend/src/applications/files/utils/tar-file.spec.ts b/backend/src/applications/files/utils/tar-file.spec.ts
index 69bba171..14fbdf41 100644
--- a/backend/src/applications/files/utils/tar-file.spec.ts
+++ b/backend/src/applications/files/utils/tar-file.spec.ts
@@ -25,12 +25,16 @@ describe(createTar.name, () => {
it('archives files and directories', async () => {
const directory = path.join(sourceDir, 'docs')
await mkdir(directory)
+ await mkdir(path.join(directory, '.sync-in-tmp', 'users'), { recursive: true })
await writeFile(path.join(directory, 'file.txt'), 'content')
+ await writeFile(path.join(directory, '.sync-in.partial'), 'partial')
+ await writeFile(path.join(directory, '.sync-in-tmp', 'users', 'staging.bin'), 'staging')
await createTar(archivePath, [{ path: directory, name: 'docs' }], false)
const archiveEntries = await readArchiveEntries()
expect(archiveEntries.map(({ path }) => path)).toEqual(expect.arrayContaining(['.', 'file.txt']))
+ expect(archiveEntries.some(({ path: entryPath }) => entryPath.includes('.sync-in'))).toBe(false)
})
it('rejects symbolic links and hard links', async () => {
diff --git a/backend/src/applications/files/utils/tar-file.ts b/backend/src/applications/files/utils/tar-file.ts
index fd37c8bc..2a95de8c 100644
--- a/backend/src/applications/files/utils/tar-file.ts
+++ b/backend/src/applications/files/utils/tar-file.ts
@@ -5,7 +5,7 @@ import { pipeline } from 'node:stream/promises'
import { create, type Pack, type WriteEntry } from 'tar'
import { DEFAULT_HIGH_WATER_MARK } from '../constants/files'
import { storageQuotaExceededError } from './errors'
-import { createProgressTransform, fileName, isPathInside } from './files'
+import { createProgressTransform, fileName, isInternalTemporaryEntry, isPathInside } from './files'
export interface TarFileEntry {
name: string
@@ -58,12 +58,16 @@ export async function createTar(
throw new Error('Cannot create a TAR archive without entries')
}
- const resolvedPaths = entries.map(({ path: entryPath }) => path.resolve(entryPath))
+ const archiveEntries = entries.filter(({ path: entryPath }) => !isInternalTemporaryEntry(path.basename(path.resolve(entryPath))))
+ if (archiveEntries.length === 0) {
+ throw new Error('Cannot create a TAR archive without entries')
+ }
+ const resolvedPaths = archiveEntries.map(({ path: entryPath }) => path.resolve(entryPath))
const stats = await Promise.all(resolvedPaths.map((entryPath) => lstat(entryPath)))
signal?.throwIfAborted()
const directoryPaths = new Set(resolvedPaths.filter((_entryPath, index) => stats[index].isDirectory()))
const selectedDirectoryPaths = [...directoryPaths]
- const selectedEntries = entries
+ const selectedEntries = archiveEntries
.map((entry, index) => ({ entry, realPath: resolvedPaths[index] }))
.filter(({ realPath }) => !selectedDirectoryPaths.some((parentPath) => isPathInside(parentPath, realPath)))
const roots = archiveRoots(
@@ -94,6 +98,7 @@ export async function createTar(
cwd,
filter: (entryPath, entry) => {
const realPath = path.resolve(cwd, entryPath)
+ if (isInternalTemporaryEntry(path.basename(realPath))) return false
const entryType = 'type' in entry ? entry.type : undefined
const isSymbolicLink = 'type' in entry ? entry.type === 'SymbolicLink' : entry.isSymbolicLink()
const hasMultipleFileLinks = !('type' in entry) && entry.isFile() && entry.nlink > 1
diff --git a/backend/src/applications/files/utils/tasks.spec.ts b/backend/src/applications/files/utils/tasks.spec.ts
index ed74af87..ae276981 100644
--- a/backend/src/applications/files/utils/tasks.spec.ts
+++ b/backend/src/applications/files/utils/tasks.spec.ts
@@ -2,10 +2,9 @@ import fs, { mkdtemp, rm, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { FILE_OPERATION } from '../constants/operations'
-import { createTaskTemporaryDir, isTaskCancellable, taskTemporaryPath, taskTemporaryPrefix } from './tasks'
+import { countDirEntriesAndSize, isTaskCancellable } from './tasks'
describe('file task utilities', () => {
- const cacheKey = 'ftask-7-task-id'
let tmpDir: string
let srcPath: string
let dstPath: string
@@ -41,12 +40,23 @@ describe('file task utilities', () => {
expect(lstatSpy).toHaveBeenCalledWith(srcPath)
})
- it('uses a uniform task staging name for files and directories', async () => {
- const filePath = taskTemporaryPath(tmpDir, cacheKey, '/destination/report.txt')
- const directoryPath = await createTaskTemporaryDir(tmpDir, cacheKey, '/destination/archive')
-
- expect(path.basename(filePath)).toBe(`${taskTemporaryPrefix(cacheKey)}report.txt`)
- expect(path.basename(directoryPath)).toBe(`${taskTemporaryPrefix(cacheKey)}archive`)
- expect((await fs.stat(directoryPath)).isDirectory()).toBe(true)
+ it('counts task content while ignoring internal temporary entries', async () => {
+ const sourceDir = path.join(tmpDir, 'source')
+ await fs.mkdir(path.join(sourceDir, 'docs'), { recursive: true })
+ await fs.mkdir(path.join(sourceDir, '.sync-in-tmp', 'users', '7'), { recursive: true })
+ await fs.mkdir(path.join(sourceDir, '.sync-in.partial-dir'), { recursive: true })
+ await Promise.all([
+ writeFile(path.join(sourceDir, 'report.txt'), 'report'),
+ writeFile(path.join(sourceDir, 'docs', 'notes.txt'), 'notes'),
+ writeFile(path.join(sourceDir, '.sync-in.report.txt'), 'ignored'),
+ writeFile(path.join(sourceDir, '.sync-in-tmp', 'users', '7', 'staging.bin'), 'ignored'),
+ writeFile(path.join(sourceDir, '.sync-in.partial-dir', 'chunk.bin'), 'ignored')
+ ])
+
+ await expect(countDirEntriesAndSize(sourceDir)).resolves.toEqual({
+ directories: 1,
+ files: 2,
+ size: Buffer.byteLength('reportnotes')
+ })
})
})
diff --git a/backend/src/applications/files/utils/tasks.ts b/backend/src/applications/files/utils/tasks.ts
index 44343e68..7ad0e760 100644
--- a/backend/src/applications/files/utils/tasks.ts
+++ b/backend/src/applications/files/utils/tasks.ts
@@ -1,25 +1,9 @@
import { Dirent } from 'node:fs'
import fs from 'node:fs/promises'
-import path from 'node:path'
import { FILE_OPERATION } from '../constants/operations'
-import { isCrossDevice, walkDir } from './files'
+import { isCrossDevice, isInternalTemporaryEntry, walkDir } from './files'
import { FileTaskProps, FileTaskStatus } from '../models/file-task'
-export function taskTemporaryPrefix(cacheKey: string): string {
- return `~${cacheKey}-`
-}
-
-export function taskTemporaryPath(parentPath: string, cacheKey: string, name: string): string {
- return path.join(parentPath, `${taskTemporaryPrefix(cacheKey)}${path.basename(name)}`)
-}
-
-export async function createTaskTemporaryDir(parentPath: string, cacheKey: string, name: string): Promise<string> {
- await fs.mkdir(parentPath, { recursive: true })
- const temporaryPath = taskTemporaryPath(parentPath, cacheKey, name)
- await fs.mkdir(temporaryPath)
- return temporaryPath
-}
-
export async function isTaskCancellable(type: FILE_OPERATION, srcPath: string, dstPath?: string): Promise<boolean> {
switch (type) {
case FILE_OPERATION.COPY:
@@ -63,7 +47,8 @@ export async function countDirEntriesAndSize(rPath: string): Promise<Pick<FileTa
}
}
},
- ignoredErrors
+ ignoredErrors,
+ (entry) => !isInternalTemporaryEntry(entry.name)
)
return entriesCount
diff --git a/backend/src/applications/files/utils/upload-file.ts b/backend/src/applications/files/utils/upload-file.ts
index daa1b577..802b09ad 100644
--- a/backend/src/applications/files/utils/upload-file.ts
+++ b/backend/src/applications/files/utils/upload-file.ts
@@ -1,9 +1,8 @@
import { HttpStatus } from '@nestjs/common'
-import path from 'node:path'
-import { randomUUID } from 'node:crypto'
import { FileError } from '../models/file-error'
import { maxFileSizeExceededError, storageQuotaExceededError } from './errors'
-import { temporaryPathPrefix } from './files'
+import { temporaryFilePath } from './files'
+import { FILE_OPERATION } from '../constants/operations'
const FASTIFY_MULTIPART_FILE_TOO_LARGE_CODE = 'FST_REQ_FILE_TOO_LARGE' as const
@@ -128,5 +127,5 @@ export function isMultipartFileTooLargeError(e: any): boolean {
}
export function uploadTmpFilePath(tmpPath: string, partFileName: string): string {
- return path.join(tmpPath, `${temporaryPathPrefix(partFileName, 'upload')}${randomUUID()}`)
+ return temporaryFilePath(tmpPath, partFileName, FILE_OPERATION.UPLOAD)
}
diff --git a/backend/src/applications/files/utils/zip-file.spec.ts b/backend/src/applications/files/utils/zip-file.spec.ts
index 3fbf6845..2ad6c47e 100644
--- a/backend/src/applications/files/utils/zip-file.spec.ts
+++ b/backend/src/applications/files/utils/zip-file.spec.ts
@@ -35,7 +35,10 @@ describe(createZip.name, () => {
it('archives files and empty directories with the requested compression', async () => {
const directory = path.join(sourceDir, 'docs')
await mkdir(path.join(directory, 'empty'), { recursive: true })
+ await mkdir(path.join(directory, '.sync-in-tmp', 'users'), { recursive: true })
await writeFile(path.join(directory, 'file.txt'), 'content')
+ await writeFile(path.join(directory, '.sync-in.partial'), 'partial')
+ await writeFile(path.join(directory, '.sync-in-tmp', 'users', 'staging.bin'), 'staging')
await createZip(archivePath, [{ path: directory, name: 'docs' }], true)
@@ -45,6 +48,7 @@ describe(createZip.name, () => {
compressionMethod: 8,
content: Buffer.from('content')
})
+ expect(archiveEntries.some(({ path: entryPath }) => entryPath.includes('.sync-in'))).toBe(false)
await createZip(archivePath, [{ path: path.join(directory, 'file.txt'), name: 'file.txt' }], false)
diff --git a/backend/src/applications/files/utils/zip-file.ts b/backend/src/applications/files/utils/zip-file.ts
index f08beeb7..35d77f76 100644
--- a/backend/src/applications/files/utils/zip-file.ts
+++ b/backend/src/applications/files/utils/zip-file.ts
@@ -6,7 +6,7 @@ import { Writable } from 'node:stream'
import { pipeline } from 'node:stream/promises'
import { DEFAULT_HIGH_WATER_MARK } from '../constants/files'
import { storageQuotaExceededError } from './errors'
-import { createProgressTransform, fileName, isPathInside } from './files'
+import { createProgressTransform, fileName, isInternalTemporaryEntry, isPathInside } from './files'
export interface ZipFileEntry {
name: string
@@ -50,6 +50,7 @@ async function addPath(archive: ZipWriter<unknown>, root: ZipRoot, realPath: str
const directory = await opendir(realPath)
for await (const entry of directory) {
signal?.throwIfAborted()
+ if (isInternalTemporaryEntry(entry.name)) continue
const entryPath = path.join(realPath, entry.name)
await addPath(archive, root, entryPath, await lstat(entryPath), signal)
}
@@ -80,11 +81,15 @@ export async function createZip(
throw new Error('Cannot create a ZIP archive without entries')
}
- const resolvedPaths = entries.map(({ path: entryPath }) => path.resolve(entryPath))
+ const archiveEntries = entries.filter(({ path: entryPath }) => !isInternalTemporaryEntry(path.basename(path.resolve(entryPath))))
+ if (archiveEntries.length === 0) {
+ throw new Error('Cannot create a ZIP archive without entries')
+ }
+ const resolvedPaths = archiveEntries.map(({ path: entryPath }) => path.resolve(entryPath))
const stats = await Promise.all(resolvedPaths.map((entryPath) => lstat(entryPath)))
signal?.throwIfAborted()
const selectedDirectoryPaths = resolvedPaths.filter((_entryPath, index) => stats[index].isDirectory())
- const selectedEntries = entries
+ const selectedEntries = archiveEntries
.map((entry, index) => ({ entry, realPath: resolvedPaths[index], stats: stats[index] }))
.filter(({ realPath }) => !selectedDirectoryPaths.some((parentPath) => isPathInside(parentPath, realPath)))
const roots = archiveRoots(selectedEntries)
diff --git a/backend/src/applications/links/services/links-manager.service.ts b/backend/src/applications/links/services/links-manager.service.ts
index 46166b58..12f677b9 100644
--- a/backend/src/applications/links/services/links-manager.service.ts
+++ b/backend/src/applications/links/services/links-manager.service.ts
@@ -67,7 +67,6 @@ export class LinksManager {
this.logger.log({ tag: this.linkAccess.name, msg: `*${user.login}* (${user.id}) is logged` })
this.incrementLinkNbAccess(link)
this.usersManager.updateAccesses(user, req.ip, true).catch((e: Error) => this.logger.error({ tag: this.linkAccess.name, msg: `${e}` }))
- await user.makePaths()
return this.authManager.setCookies(user, res)
}
// Already authenticated
@@ -117,7 +116,6 @@ export class LinksManager {
}
// authenticate user to allow access
this.logger.log({ tag: this.linkAuthentication.name, msg: `*${user.login}* (${user.id}) is logged` })
- await user.makePaths()
return this.authManager.setCookies(user, res)
}
diff --git a/backend/src/applications/shares/services/shares-manager.service.spec.ts b/backend/src/applications/shares/services/shares-manager.service.spec.ts
index fdcfe57e..10c35663 100644
--- a/backend/src/applications/shares/services/shares-manager.service.spec.ts
+++ b/backend/src/applications/shares/services/shares-manager.service.spec.ts
@@ -16,6 +16,8 @@ import { SHARE_ALL_OPERATIONS } from '../constants/shares'
import { SharesManager } from './shares-manager.service'
import { SharesQueries } from './shares-queries.service'
import { FilesQuotaManager } from '../../files/services/files-quota-manager.service'
+import * as filesUtils from '../../files/utils/files'
+import { UserModel } from '../../users/models/user.model'
// Mock classes and utility modules used by SharesManager
vi.mock('../../spaces/models/space-env.model', () => ({
@@ -514,6 +516,31 @@ describe(SharesManager.name, () => {
})
})
+ describe('deleteLinkMembers', () => {
+ it('keeps the link user record when its home cannot be removed', async () => {
+ const cleanupError = new Error('cleanup failed')
+ vi.spyOn(UserModel, 'getLinkHomePath').mockReturnValue('/data/links/42')
+ vi.spyOn(filesUtils, 'removeFiles').mockRejectedValueOnce(cleanupError)
+
+ await expect(service.deleteLinkMembers([{ id: 42, linkId: 7 }] as any)).rejects.toBe(cleanupError)
+
+ expect(filesUtils.removeFiles).toHaveBeenCalledWith('/data/links/42')
+ expect(usersQueriesMock.deleteGuestLink).not.toHaveBeenCalled()
+ })
+
+ it('removes the link user only after its home was removed', async () => {
+ vi.spyOn(UserModel, 'getLinkHomePath').mockReturnValue('/data/links/42')
+ const removeHome = vi.spyOn(filesUtils, 'removeFiles').mockResolvedValueOnce(undefined)
+ usersQueriesMock.deleteGuestLink.mockResolvedValueOnce(undefined)
+
+ await service.deleteLinkMembers([{ id: 42, linkId: 7 }] as any)
+
+ expect(removeHome).toHaveBeenCalledWith('/data/links/42')
+ expect(usersQueriesMock.deleteGuestLink).toHaveBeenCalledWith(42)
+ expect(removeHome.mock.invocationCallOrder[0]).toBeLessThan(usersQueriesMock.deleteGuestLink.mock.invocationCallOrder[0])
+ })
+ })
+
describe('child share wrappers', () => {
it('getChildShare returns share link when isLink = true', async () => {
sharesQueriesMock.childExistsForShareOwner.mockResolvedValueOnce(99)
diff --git a/backend/src/applications/shares/services/shares-manager.service.ts b/backend/src/applications/shares/services/shares-manager.service.ts
index a3d022b9..5adaefb5 100644
--- a/backend/src/applications/shares/services/shares-manager.service.ts
+++ b/backend/src/applications/shares/services/shares-manager.service.ts
@@ -14,7 +14,7 @@ import { intersectPermissions, InvalidSlugError } from '../../../common/shared'
import { ContextManager } from '../../../infrastructure/context/services/context-manager.service'
import type { FileProps } from '../../files/interfaces/file-props.interface'
import { FileError } from '../../files/models/file-error'
-import { checkExternalPath, getProps, isPathExists } from '../../files/utils/files'
+import { checkExternalPath, getProps, isPathExists, removeFiles } from '../../files/utils/files'
import { LINK_TYPE } from '../../links/constants/links'
import type { CreateOrUpdateLinkDto } from '../../links/dto/create-or-update-link.dto'
import type { LinkGuest } from '../../links/interfaces/link-guest.interface'
@@ -1131,14 +1131,19 @@ export class SharesManager {
}
private async deleteGuestLinks(guestLinks: Partial<{ id: number; linkId: number }>[]) {
+ let deletionError: unknown
for (const guestLink of guestLinks) {
try {
+ // Keep the database record when filesystem cleanup fails so the deletion can be retried.
+ await removeFiles(UserModel.getLinkHomePath(guestLink.id))
await this.usersQueries.deleteGuestLink(guestLink.id)
this.logger.log({ tag: this.deleteGuestLinks.name, msg: `guest (${guestLink.id}) (link: ${guestLink.linkId}) was removed` })
} catch (e) {
+ deletionError ??= e
this.logger.error({ tag: this.deleteGuestLinks.name, msg: `guest (${guestLink.id}) (link: ${guestLink.linkId}) was not removed : ${e}` })
}
}
+ if (deletionError) throw deletionError
}
/* MANAGE CACHE PERMISSIONS AND NOTIFY */
diff --git a/backend/src/applications/shares/shares.md b/backend/src/applications/shares/shares.md
new file mode 100644
index 00000000..e01c9bbc
--- /dev/null
+++ b/backend/src/applications/shares/shares.md
@@ -0,0 +1,79 @@
+# Shares
+
+The `shares` module exposes existing storage through share aliases. A share does
+not copy its source and does not become the owner of the underlying storage.
+
+## Storage provenance
+
+The share keeps enough provenance to resolve the physical source and its
+managed storage owner:
+
+| Share source | Storage provenance |
+| --- | --- |
+| Personal files | The source file's `ownerId` |
+| Native space | The source `spaceId` |
+| External root attached to a native space | The source `spaceId` and `spaceRootId` |
+| External share | The root share's `externalPath` and `files.shareExternalId` |
+| Child of an external share | The highest external parent and a path below its `externalPath` |
+
+For a native-space external root, the native space remains the managed storage
+owner even though the files are physically outside its home.
+
+A root external share has no managed user or native-space storage owner. Its
+`ownerId` is therefore `null`. A child share can have an `ownerId` identifying
+the owner of the share record, but that value is not storage ownership and must
+not be used to select a user trash repository.
+
+Files indexed below an external share use `files.shareExternalId`. For a child
+share, this identifier refers to the highest external parent so that every
+descendant resolves against the same physical external root.
+
+## Deleting a share and deleting shared content
+
+Deleting a share definition removes the share, its memberships, and its child
+share definitions. It does not delete the source content from the filesystem.
+
+Deleting a file or directory while browsing `shares/<share-alias>/...` is a
+separate file operation. It follows the storage provenance of the shared
+content rather than the authenticated actor or the owner of the share record.
+
+## Shared-content deletion policy
+
+| Shared content | Deletion target |
+| --- | --- |
+| Personal files | The source owner's managed user trash |
+| Native-space files | The source space's managed trash |
+| External root attached to a native space | The source space's managed trash |
+| External share | Permanent deletion |
+| Child or other descendant of an external share | Permanent deletion |
+
+The permanent-deletion rule is selected when the resolved space is in the
+shares repository and its root has an `externalPath`, unless its provenance
+points to a native space. It applies regardless of whether the actor is a
+regular user, guest, or link pseudo-user.
+
+For an external share, deleting shared content:
+
+1. removes the resource directly from the external filesystem;
+2. permanently removes the corresponding file records from the database;
+3. removes associated locks;
+4. emits a `DELETE_PERMANENTLY` event.
+
+The operation never uses the actor's home, a share-record owner's home, or an
+`<external-root>/.trash/` directory. There is no fallback destination and no
+migration from an existing `.trash` directory.
+
+Because no managed trash entry is created, deleted external-share content
+cannot be listed, restored, or removed later by the trash retention scheduler.
+If the external location cannot be modified, the deletion fails instead of
+redirecting the resource to another storage owner.
+
+## Future external trash support
+
+The permanent-deletion rule is the current policy until external trash is
+implemented as a first-class repository. Such a repository must be explicitly
+addressable by the file browser and must define empty, restore, and retention
+lifecycles before external-share deletion can target it.
+
+The managed trash layout and lifecycle are specified in
+[`spaces.md`](../spaces/spaces.md).
diff --git a/backend/src/applications/spaces/models/space-env.model.ts b/backend/src/applications/spaces/models/space-env.model.ts
index 869a3017..88a81cbc 100644
--- a/backend/src/applications/spaces/models/space-env.model.ts
+++ b/backend/src/applications/spaces/models/space-env.model.ts
@@ -1,6 +1,7 @@
import { uniquePermissions } from '../../../common/functions'
import { FileDBProps } from '../../files/interfaces/file-db-props.interface'
import { FileTaskProps } from '../../files/models/file-task'
+import { FILE_OPERATION } from '../../files/constants/operations'
import { UserModel } from '../../users/models/user.model'
import { SPACE_ALIAS, SPACE_ALL_OPERATIONS, SPACE_OPERATION, SPACE_PERMS_SEP, SPACE_REPOSITORY, SPACE_ROLE } from '../constants/spaces'
import { Space } from '../schemas/space.interface'
@@ -62,7 +63,7 @@ export class SpaceEnv implements Pick<Space, 'id' | 'alias' | 'name' | 'enabled'
storageQuota: number
quotaIsExceeded = false
// event
- task?: { cacheKey: string; props: FileTaskProps }
+ task?: { id: string; type: FILE_OPERATION; cacheKey: string; props: FileTaskProps }
constructor(props: Partial<SpaceEnv>, rootAlias = '', mustHaveRoot = true) {
Object.assign(this, props)
diff --git a/backend/src/applications/spaces/models/space.model.ts b/backend/src/applications/spaces/models/space.model.ts
index 784103cf..eb9ffd66 100644
--- a/backend/src/applications/spaces/models/space.model.ts
+++ b/backend/src/applications/spaces/models/space.model.ts
@@ -1,6 +1,7 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { configuration } from '../../../configuration/config.environment'
+import { TEMPORARY_PATH } from '../../files/constants/files'
import { isPathInside } from '../../files/utils/files'
import { SPACE_REPOSITORY } from '../constants/spaces'
import { SpaceRoot } from '../schemas/space-root.interface'
@@ -51,6 +52,26 @@ export class SpaceModel implements Space {
return path.join(SpaceModel.getHomePath(spaceAlias), SPACE_REPOSITORY.TRASH)
}
+ static getTmpPath(spaceAlias: string) {
+ return path.join(SpaceModel.getHomePath(spaceAlias), 'tmp')
+ }
+
+ static getUsersTmpPath(spaceAlias: string) {
+ return path.join(SpaceModel.getTmpPath(spaceAlias), TEMPORARY_PATH.ACTORS)
+ }
+
+ static getUserTmpPath(spaceAlias: string, userId: number) {
+ if (!Number.isSafeInteger(userId) || userId <= 0) {
+ throw new Error('User id must be a positive safe integer')
+ }
+ const usersTmpPath = SpaceModel.getUsersTmpPath(spaceAlias)
+ const userTmpPath = path.resolve(usersTmpPath, String(userId))
+ if (!isPathInside(usersTmpPath, userTmpPath)) {
+ throw new Error('User id resolves outside space temporary directory')
+ }
+ return userTmpPath
+ }
+
static getRepositoryPath(spaceAlias: string, inTrash = false) {
if (inTrash) return SpaceModel.getTrashPath(spaceAlias)
return SpaceModel.getFilesPath(spaceAlias)
diff --git a/backend/src/applications/spaces/services/spaces-browser.service.spec.ts b/backend/src/applications/spaces/services/spaces-browser.service.spec.ts
index 442b5527..720064cc 100644
--- a/backend/src/applications/spaces/services/spaces-browser.service.spec.ts
+++ b/backend/src/applications/spaces/services/spaces-browser.service.spec.ts
@@ -1,6 +1,7 @@
import { ConfigModule } from '@nestjs/config'
import { Test, TestingModule } from '@nestjs/testing'
-import { exportConfiguration } from '../../../configuration/config.environment'
+import fs from 'node:fs/promises'
+import { configuration, exportConfiguration } from '../../../configuration/config.environment'
import { Cache } from '../../../infrastructure/cache/cache.service'
import { ContextManager } from '../../../infrastructure/context/services/context-manager.service'
import { DB_TOKEN_PROVIDER } from '../../../infrastructure/database/constants'
@@ -53,6 +54,10 @@ describe(SpacesBrowser.name, () => {
spacesBrowserService = module.get<SpacesBrowser>(SpacesBrowser)
})
+ afterEach(() => {
+ vi.restoreAllMocks()
+ })
+
it('should be defined', () => {
expect(spacesBrowserService).toBeDefined()
})
@@ -76,4 +81,32 @@ describe(SpacesBrowser.name, () => {
expect(result.space).toEqual({ alias: 'communication', name: 'Communication' })
})
+
+ it('always hides internal temporary entries from filesystem browsing', async () => {
+ const showHiddenFiles = configuration.applications.files.showHiddenFiles
+ configuration.applications.files.showHiddenFiles = true
+ vi.spyOn(fs, 'readdir').mockResolvedValue([
+ { name: '.sync-in-tmp', isDirectory: () => true, isFile: () => false },
+ { name: '.sync-in.uploading', isDirectory: () => false, isFile: () => true },
+ { name: '.visible', isDirectory: () => false, isFile: () => true }
+ ] as any)
+ vi.spyOn(fs, 'stat').mockResolvedValue({
+ ino: 1,
+ size: 7,
+ birthtime: new Date(0),
+ mtime: new Date(0),
+ isDirectory: () => false
+ } as any)
+ const names: string[] = []
+
+ try {
+ for await (const file of (spacesBrowserService as any).parsePath({ realPath: '/storage', relativeUrl: '/space' })) {
+ names.push(file.name)
+ }
+ } finally {
+ configuration.applications.files.showHiddenFiles = showHiddenFiles
+ }
+
+ expect(names).toEqual(['.visible'])
+ })
})
diff --git a/backend/src/applications/spaces/services/spaces-browser.service.ts b/backend/src/applications/spaces/services/spaces-browser.service.ts
index 4dda77ef..42e38881 100644
--- a/backend/src/applications/spaces/services/spaces-browser.service.ts
+++ b/backend/src/applications/spaces/services/spaces-browser.service.ts
@@ -8,7 +8,7 @@ import { FileProps } from '../../files/interfaces/file-props.interface'
import { FilesLockManager } from '../../files/services/files-lock-manager.service'
import { FilesQueries } from '../../files/services/files-queries.service'
import { FilesRecents } from '../../files/services/files-recents.service'
-import { dirName, fileName, getProps } from '../../files/utils/files'
+import { dirName, fileName, getProps, isInternalTemporaryEntry } from '../../files/utils/files'
import { SharesQueries } from '../../shares/services/shares-queries.service'
import { USER_PERMISSION } from '../../users/constants/user'
import { UserModel } from '../../users/models/user.model'
@@ -59,13 +59,14 @@ export class SpacesBrowser {
withLocks: options.withLocks
})
])
+ const visibleRootFiles = rootFiles.filter((file) => !isInternalTemporaryEntry(file.name))
this.updateDBFiles(user, space, dbFiles, fsFiles, options)
if (space.inSharesList) {
// the share space includes shares as root files
- spaceFiles.files = [...rootFiles, ...fsFiles]
+ spaceFiles.files = [...visibleRootFiles, ...fsFiles]
spaceFiles.hasRoots = true
} else {
- await this.mergeSpaceRootFiles(space, rootFiles, fsFiles, spaceFiles)
+ await this.mergeSpaceRootFiles(space, visibleRootFiles, fsFiles, spaceFiles)
}
if (options.withLocks && !space.inTrashRepository) {
// locks were removed when files were moved to the trash, no need to parse locks
@@ -135,6 +136,10 @@ export class SpacesBrowser {
try {
for (const element of await fs.readdir(space.realPath, { withFileTypes: true })) {
const isDir = element.isDirectory()
+ if (isInternalTemporaryEntry(element.name)) {
+ this.logger.verbose({ tag: this.parsePath.name, msg: `ignore internal temporary entry : ${element.name}` })
+ continue
+ }
if (!isDir && !element.isFile()) {
this.logger.log({ tag: this.parsePath.name, msg: `ignore special file : ${element.name}` })
continue
@@ -229,6 +234,7 @@ export class SpacesBrowser {
}
) {
for (const dbFile of dbFiles) {
+ if (isInternalTemporaryEntry(dbFile.name)) continue
const fsFile = fsFiles.find((f: FileProps) => dbFile.name === f.name)
if (fsFile) {
/* important: inherits from the file id in database */
diff --git a/backend/src/applications/spaces/services/spaces-manager.service.ts b/backend/src/applications/spaces/services/spaces-manager.service.ts
index e44e7905..d6630f0d 100644
--- a/backend/src/applications/spaces/services/spaces-manager.service.ts
+++ b/backend/src/applications/spaces/services/spaces-manager.service.ts
@@ -9,7 +9,7 @@ import { createSlug, InvalidSlugError, regExpNumberSuffix } from '../../../commo
import { configuration } from '../../../configuration/config.environment'
import { ContextManager } from '../../../infrastructure/context/services/context-manager.service'
import { FileError } from '../../files/models/file-error'
-import { dirListFileNames, getProps, isPathExists, moveFiles, removeFiles } from '../../files/utils/files'
+import { dirListFileNames, getProps, isInternalTemporaryEntry, isPathExists, moveFiles, removeFiles } from '../../files/utils/files'
import { LINK_TYPE } from '../../links/constants/links'
import { NOTIFICATION_APP, NOTIFICATION_APP_EVENT } from '../../notifications/constants/notifications'
import { NotificationContent } from '../../notifications/interfaces/notification-properties.interface'
@@ -178,7 +178,9 @@ export class SpacesManager {
for (const space of [...(await this.listSpaces(user.id)), personalTrash] as SpaceTrash[]) {
const rPath = space.alias === SPACE_ALIAS.PERSONAL ? user.trashPath : SpaceModel.getTrashPath(space.alias)
try {
- space.nb = (await fs.readdir(rPath)).filter((f) => configuration.applications.files.showHiddenFiles || f[0] !== '.').length
+ space.nb = (await fs.readdir(rPath)).filter(
+ (f) => !isInternalTemporaryEntry(f) && (configuration.applications.files.showHiddenFiles || f[0] !== '.')
+ ).length
if (space.nb) {
const stats = await fs.stat(rPath)
space.mtime = stats.mtime.getTime()
diff --git a/backend/src/applications/spaces/spaces.md b/backend/src/applications/spaces/spaces.md
new file mode 100644
index 00000000..daea15ca
--- /dev/null
+++ b/backend/src/applications/spaces/spaces.md
@@ -0,0 +1,138 @@
+# Spaces
+
+The `spaces` module manages collaborative spaces, anchored roots, external
+roots, and the logical repositories used to browse their files and trash.
+
+## Managed space storage
+
+A native space has a managed home under the configured spaces path:
+
+```text
+spaces/<space-alias>/
+├── files/
+├── trash/
+└── tmp/
+ └── users/
+ └── <actor-id>/
+```
+
+The `files` and `trash` directories are created with the space. Temporary
+directories are created lazily when an operation needs them.
+
+An external root is only a view of an existing filesystem location. Sync-in
+does not create a trash directory inside it:
+
+```text
+<external-root>/
+└── <existing-content>
+```
+
+In particular, `<external-root>/.trash/` is not part of the storage layout.
+Such a directory would not correspond to a logical trash repository, so the
+trash list, browser, restore operations, and retention scheduler could not
+discover or empty it.
+
+## Logical trash repositories
+
+Trash repositories are addressed without a root level:
+
+```text
+trash/personal/<path>
+trash/<space-alias>/<path>
+```
+
+Every segment after `personal` or `<space-alias>` is treated as a path inside
+the selected trash. A space root alias is therefore not resolved while browsing
+trash. For an external root, the trash path is relative to the external root
+itself and does not include its alias.
+
+The trash list examines the personal trash and every space available to the
+user. It returns only repositories containing at least one visible top-level
+entry. Internal temporary entries are always ignored; hidden entries follow the
+`showHiddenFiles` configuration.
+
+## Deletion target resolution
+
+Deletion resolves one authoritative target from the source context and its
+managed ownership:
+
+| Source | Deletion target |
+| --- | --- |
+| Personal files | `users/<actor-login>/trash/` |
+| Files stored in a native space home | `spaces/<space-alias>/trash/` |
+| External root attached to a native space | `spaces/<space-alias>/trash/` |
+| Root anchored to another user's personal files | `users/<owner-login>/trash/` |
+| Share backed by personal files | `users/<owner-login>/trash/` |
+| Native-space-backed share, including an external root | `spaces/<origin-space-alias>/trash/` |
+| External share | Permanent deletion |
+
+The authenticated user is the actor. The owner is the user or native space
+whose managed storage contains, or logically owns, the source.
+
+An external root attached to a native space deliberately uses the native
+space's managed trash. An external share has no managed storage owner and is
+deleted permanently, regardless of the actor type. There is no fallback to
+`<external-root>/.trash/` and no migration from such a directory.
+
+## Deletion lifecycle
+
+For a resource whose target is a managed trash, deletion performs the following
+operations:
+
+1. 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.
+4. Move the resource to the selected trash.
+5. Mark the corresponding database records as being in trash and remove their
+ locks.
+
+The newly deleted resource keeps its original name. When a deletion runs as a
+cancellable task and the source and trash are on different filesystems, it can
+stage the transfer in the temporary directory belonging to the selected
+managed trash before publishing it.
+
+For an external share, deletion skips this lifecycle and removes the source
+directly from the filesystem and database.
+
+Deleting a resource that is already under `trash/...` removes it permanently
+from the filesystem and database. The trash itself is a protected virtual
+endpoint and cannot be deleted. New files, uploads, and in-place modifications
+are rejected in trash; a resource can be restored by moving it from trash to a
+writable files repository.
+
+## Events and quotas
+
+A deletion that remains in the same logical ownership scope emits a `DELETE`
+event. It does not trigger a quota update because moving an entry into trash is
+not considered a logical storage removal.
+
+An external share has no managed owner or trash destination. Its resources are
+removed directly and emit `DELETE_PERMANENTLY`.
+
+## Automatic retention
+
+Trash retention is configured independently for user and native-space
+repositories:
+
+```yaml
+applications:
+ files:
+ trashRetention:
+ users: false
+ spaces: false
+```
+
+Each value accepts a positive integer number of days. `false` or `0` disables
+cleanup for that repository type. Cleanup runs at application startup and then
+daily at 02:00 according to the server scheduler.
+
+For space retention, only enabled native spaces are indexed. The scheduler
+scans `spaces/<space-alias>/trash/`, records entries in disposable retention
+tables, removes expired files before directories, and retries filesystem
+deletion failures on a later run. Paths loaded from retention tables are
+resolved and checked against the managed trash root before deletion.
+
+Retention never scans external roots for `.trash` directories. All deletions
+owned by a native space are visible to the same browser and retention flow
+because they converge on `spaces/<space-alias>/trash/`.
diff --git a/backend/src/applications/spaces/utils/paths.spec.ts b/backend/src/applications/spaces/utils/paths.spec.ts
new file mode 100644
index 00000000..62a4088c
--- /dev/null
+++ b/backend/src/applications/spaces/utils/paths.spec.ts
@@ -0,0 +1,134 @@
+import path from 'node:path'
+import { USER_ROLE } from '../../users/constants/user'
+import { UserModel } from '../../users/models/user.model'
+import { realPathFromSpace, temporaryRootFromSpace, temporaryRootFromStorage, trashTargetFromSpace } from './paths'
+
+vi.mock('../../../configuration/config.environment', () => ({
+ configuration: {
+ applications: {
+ files: {
+ usersPath: '/data/users',
+ spacesPath: '/data/spaces',
+ linksPath: '/data/links'
+ }
+ }
+ }
+}))
+
+describe('temporary path resolution', () => {
+ const user = new UserModel({ id: 42, login: 'alice', role: USER_ROLE.USER } as any)
+
+ it('places personal-space artifacts in the personal storage root', () => {
+ expect(temporaryRootFromSpace(user, { inPersonalSpace: true } as any)).toBe('/data/users/alice/.sync-in-tmp/users/42')
+ })
+
+ it('places native-space artifacts in the managed space temporary root', () => {
+ expect(temporaryRootFromSpace(user, { alias: 'project' } as any)).toBe('/data/spaces/project/tmp/users/42')
+ })
+
+ it('places external-space artifacts under the external storage root', () => {
+ expect(temporaryRootFromSpace(user, { root: { externalPath: '/mnt/archive' } } as any)).toBe('/mnt/archive/.sync-in-tmp/users/42')
+ })
+
+ it('places anchored personal-space artifacts in the owner storage root', () => {
+ const space = { root: { file: { path: 'documents' }, owner: { login: 'bob' } } } as any
+
+ expect(temporaryRootFromSpace(user, space)).toBe('/data/users/bob/.sync-in-tmp/users/42')
+ })
+
+ it('places native and external share artifacts with their physical storage', () => {
+ const nativeShare = { root: { file: { space: { id: 1, alias: 'project' } } } } as any
+ const externalShare = {
+ root: { file: { path: 'shared', space: { id: 1, alias: 'project' }, root: { id: 2, externalPath: '/mnt/archive' } } }
+ } as any
+
+ expect(temporaryRootFromSpace(user, nativeShare)).toBe('/data/spaces/project/tmp/users/42')
+ expect(temporaryRootFromSpace(user, externalShare)).toBe('/mnt/archive/.sync-in-tmp/users/42')
+ })
+
+ it('uses the shared users namespace for guests and a numeric links namespace for link users', () => {
+ const guest = new UserModel({ id: 7, login: 'guest', role: USER_ROLE.GUEST } as any)
+ const link = new UserModel({ id: 9, login: 'link-login', role: USER_ROLE.LINK } as any)
+
+ expect(guest.homePath).toBe('/data/users/guest')
+ expect(guest.tmpPath).toBe('/data/users/guest/tmp')
+ expect(link.homePath).toBe('/data/links/9')
+ expect(link.tmpPath).toBe('/data/links/9/tmp')
+ })
+
+ it('resolves trash and staging together from the target space', () => {
+ expect(trashTargetFromSpace(user, { inPersonalSpace: true } as any)).toEqual({
+ mode: 'trash',
+ path: '/data/users/alice/trash',
+ temporaryRoot: '/data/users/alice/.sync-in-tmp/users/42'
+ })
+ expect(trashTargetFromSpace(user, { alias: 'project' } as any)).toEqual({
+ mode: 'trash',
+ path: '/data/spaces/project/trash',
+ temporaryRoot: '/data/spaces/project/tmp/users/42'
+ })
+ expect(
+ trashTargetFromSpace(user, {
+ alias: 'project',
+ root: { externalPath: '/mnt/archive' },
+ inFilesRepository: true,
+ inSharesRepository: false
+ } as any)
+ ).toEqual({
+ mode: 'trash',
+ path: '/data/spaces/project/trash',
+ temporaryRoot: '/data/spaces/project/tmp/users/42'
+ })
+ expect(trashTargetFromSpace(user, { root: { file: { space: { id: 1, alias: 'project' } } } } as any)).toEqual({
+ mode: 'trash',
+ path: '/data/spaces/project/trash',
+ temporaryRoot: '/data/spaces/project/tmp/users/42'
+ })
+ })
+
+ it('permanently deletes external shares and uses the owner trash for an anchored personal root', () => {
+ expect(trashTargetFromSpace(user, { root: { externalPath: '/mnt/share' }, inFilesRepository: false, inSharesRepository: true } as any)).toEqual({
+ mode: 'permanent',
+ reason: 'external-share'
+ })
+ expect(trashTargetFromSpace(user, { root: { file: { path: 'documents' }, owner: { login: 'bob' } } } as any)).toEqual({
+ mode: 'trash',
+ path: '/data/users/bob/trash',
+ temporaryRoot: '/data/users/bob/.sync-in-tmp/users/42'
+ })
+ expect(trashTargetFromSpace(user, {} as any)).toBeNull()
+ })
+
+ it.each([['.sync-in-tmp', 'users', '42'], ['.sync-in.uploading']])('rejects direct access to internal temporary paths', (...paths) => {
+ const space = { inPersonalSpace: false, root: { externalPath: '/mnt/archive' }, paths, inSharesRepository: false } as any
+
+ expect(() => realPathFromSpace(user, space)).toThrow('Internal temporary locations are not accessible')
+ })
+
+ it('keeps regular external paths accessible', () => {
+ const space = { inPersonalSpace: false, root: { externalPath: '/mnt/archive' }, paths: ['documents'], inSharesRepository: false } as any
+
+ expect(realPathFromSpace(user, space)).toBe('/mnt/archive/documents')
+ })
+
+ it('rejects an external root anchored inside the internal temporary tree', () => {
+ const space = {
+ inPersonalSpace: false,
+ root: { externalPath: '/mnt/archive/.sync-in-tmp/users/42' },
+ paths: [],
+ inSharesRepository: true
+ } as any
+
+ expect(() => realPathFromSpace(user, space)).toThrow('Internal temporary locations are not accessible')
+ })
+
+ it('does not treat segments above a personal repository as requested entries', () => {
+ const reservedLogin = new UserModel({ id: 43, login: '.sync-in.user', role: USER_ROLE.USER } as any)
+
+ expect(realPathFromSpace(reservedLogin, { inPersonalSpace: true, paths: ['documents'] } as any)).toBe('/data/users/.sync-in.user/files/documents')
+ })
+
+ it('rejects invalid actor identifiers', () => {
+ expect(() => temporaryRootFromStorage(path.join('/mnt', 'archive'), 0)).toThrow('Invalid temporary-file owner')
+ })
+})
diff --git a/backend/src/applications/spaces/utils/paths.ts b/backend/src/applications/spaces/utils/paths.ts
index 1c46ef0e..a5cd19b5 100644
--- a/backend/src/applications/spaces/utils/paths.ts
+++ b/backend/src/applications/spaces/utils/paths.ts
@@ -4,7 +4,8 @@ import path from 'node:path'
import { FileDBProps } from '../../files/interfaces/file-db-props.interface'
import { FileProps } from '../../files/interfaces/file-props.interface'
import { FileError } from '../../files/models/file-error'
-import { isPathInside } from '../../files/utils/files'
+import { TEMPORARY_PATH } from '../../files/constants/files'
+import { isInternalTemporaryEntry, isInternalTemporaryPath, isPathInside } from '../../files/utils/files'
import { UserModel } from '../../users/models/user.model'
import { SPACE_REPOSITORY } from '../constants/spaces'
import { SpaceEnv } from '../models/space-env.model'
@@ -70,36 +71,105 @@ export function realPathFromSpace(user: UserModel, space: SpaceEnv, withBasePath
if (!isPathInside(bPath, rPath, true)) {
throw new FileError(HttpStatus.FORBIDDEN, 'Location is not allowed')
}
+ const externalBasePathContainsInternalEntry =
+ Boolean(space.root?.externalPath || space.root?.file?.root?.id) && path.resolve(bPath).split(path.sep).some(isInternalTemporaryEntry)
+ const rootFilePathContainsInternalEntry = space.root?.file?.path?.split(/[/\\]/).some(isInternalTemporaryEntry)
+ if (externalBasePathContainsInternalEntry || rootFilePathContainsInternalEntry || isInternalTemporaryPath(bPath, rPath)) {
+ throw new FileError(HttpStatus.FORBIDDEN, 'Internal temporary locations are not accessible')
+ }
return withBasePath ? [bPath, rPath] : rPath
}
-export function realTrashPathFromSpace(user: UserModel, space: SpaceEnv) {
+export interface TrashTarget {
+ mode: 'trash'
+ path: string
+ temporaryRoot: string
+}
+
+export interface PermanentDeleteTarget {
+ mode: 'permanent'
+ reason: 'external-share'
+}
+
+export type TrashTargetResolution = TrashTarget | PermanentDeleteTarget
+
+export function trashTargetFromSpace(user: UserModel, space: SpaceEnv): TrashTargetResolution | null {
+ const userTarget = (login: string): TrashTarget => ({
+ mode: 'trash',
+ path: UserModel.getTrashPath(login),
+ temporaryRoot: temporaryRootFromStorage(UserModel.getHomePath(login), user.id)
+ })
+ const spaceTarget = (alias: string): TrashTarget => ({
+ mode: 'trash',
+ path: SpaceModel.getTrashPath(alias),
+ temporaryRoot: SpaceModel.getUserTmpPath(alias, user.id)
+ })
+
if (space.inPersonalSpace) {
// personal user space
- return UserModel.getTrashPath(user.login)
+ return userTarget(user.login)
} else if (space.root?.externalPath) {
// external path from space or share
// space case: use the space trash
if (space.root.file?.space?.alias) {
- return SpaceModel.getTrashPath(space.root.file.space.alias)
+ return spaceTarget(space.root.file.space.alias)
} else if (space.inFilesRepository && !space.inSharesRepository) {
- return SpaceModel.getTrashPath(space.alias)
+ return spaceTarget(space.alias)
+ }
+ // external shares have no managed owner and are deleted permanently
+ if (space.inSharesRepository) {
+ return { mode: 'permanent', reason: 'external-share' }
}
- // share case: use the user's trash because this type of share has no owner
- return UserModel.getTrashPath(user.login)
+ return null
} else if (space.root?.file?.path && space.root.owner?.login) {
// space root is linked to a file in a personal space
- return UserModel.getTrashPath(space.root.owner.login)
+ return userTarget(space.root.owner.login)
} else if (space.root?.file?.space?.id) {
// share linked to a space (with an external path or not)
- return SpaceModel.getTrashPath(space.root.file.space.alias)
+ return spaceTarget(space.root.file.space.alias)
} else if (space.alias) {
// space files (no root)
- return SpaceModel.getTrashPath(space.alias)
+ return spaceTarget(space.alias)
}
return null
}
+export function temporaryRootFromStorage(storageRoot: string, userId: number): string {
+ if (!Number.isSafeInteger(userId) || userId <= 0) {
+ throw new FileError(HttpStatus.BAD_REQUEST, 'Invalid temporary-file owner')
+ }
+ const rootPath = path.resolve(storageRoot)
+ const usersTmpPath = path.resolve(rootPath, TEMPORARY_PATH.STORAGE, TEMPORARY_PATH.ACTORS)
+ const userTmpPath = path.resolve(usersTmpPath, String(userId))
+ if (!isPathInside(rootPath, usersTmpPath) || !isPathInside(usersTmpPath, userTmpPath)) {
+ throw new FileError(HttpStatus.FORBIDDEN, 'Temporary location is not allowed')
+ }
+ return userTmpPath
+}
+
+export function temporaryRootFromSpace(user: UserModel, space: SpaceEnv): string {
+ // resolve an actor-isolated temporary directory on the same storage as the target
+ if (space.inPersonalSpace) {
+ return temporaryRootFromStorage(UserModel.getHomePath(user.login), user.id)
+ }
+ if (space.root?.externalPath) {
+ return temporaryRootFromStorage(space.root.externalPath, user.id)
+ }
+ if (space.root?.file?.path && space.root.owner?.login) {
+ return temporaryRootFromStorage(UserModel.getHomePath(space.root.owner.login), user.id)
+ }
+ if (space.root?.file?.space?.id) {
+ if (space.root.file.root?.id) {
+ return temporaryRootFromStorage(space.root.file.root.externalPath, user.id)
+ }
+ return SpaceModel.getUserTmpPath(space.root.file.space.alias, user.id)
+ }
+ if (space.alias) {
+ return SpaceModel.getUserTmpPath(space.alias, user.id)
+ }
+ throw new FileError(HttpStatus.NOT_FOUND, 'Space root not found')
+}
+
export function realPathFromRootFile(f: FileProps): string {
// get realpath
if (f.origin) {
diff --git a/backend/src/applications/sync/constants/sync.ts b/backend/src/applications/sync/constants/sync.ts
index bc5fea08..47420465 100644
--- a/backend/src/applications/sync/constants/sync.ts
+++ b/backend/src/applications/sync/constants/sync.ts
@@ -4,7 +4,7 @@ export const SYNC_IN_SERVER_AGENT = 'sync-in' as const
export const CHECK_SERVER_RESP = { server: SYNC_IN_SERVER_AGENT } as const
export const SYNC_CHECKSUM_ALG = 'sha512-256'
export const SYNC_DIFF_DONE = 'done'
-export const SYNC_FILE_NAME_PREFIX = '.sync-in.'
+export const SYNC_TEMPORARY_FILE_PREFIX = '.sync-in.'
export const SYNC_MAX_PATH_FILTERS = 100
export const SYNC_MAX_PATH_FILTER_LENGTH = 1024
export const SYNC_MAX_PATH_FILTER_PATTERN_LENGTH = SYNC_MAX_PATH_FILTERS * SYNC_MAX_PATH_FILTER_LENGTH + (SYNC_MAX_PATH_FILTERS - 1)
diff --git a/backend/src/applications/sync/services/sync-manager.service.spec.ts b/backend/src/applications/sync/services/sync-manager.service.spec.ts
index 2e3db7a2..415542f2 100644
--- a/backend/src/applications/sync/services/sync-manager.service.spec.ts
+++ b/backend/src/applications/sync/services/sync-manager.service.spec.ts
@@ -27,6 +27,7 @@ vi.mock('fs/promises', () => ({
vi.mock('../../files/utils/files', () => ({
__esModule: true,
checksumFile: vi.fn(),
+ isInternalTemporaryEntry: vi.fn((name: string) => name === '.sync-in-tmp' || name.startsWith('.sync-in.')),
isPathExists: vi.fn(),
isPathIsDir: vi.fn(),
removeFiles: vi.fn(),
@@ -529,6 +530,8 @@ describe(SyncManager.name, () => {
if (dir === base) {
return [
makeDirent('special', base, 'other'),
+ makeDirent('.sync-in-tmp', base, 'dir'),
+ makeDirent('.sync-in.file4', base, 'file'),
makeDirent('dir1', base, 'dir'),
makeDirent('ignoredName', base, 'file'),
makeDirent('fileStatError', base, 'file'),
@@ -578,6 +581,7 @@ describe(SyncManager.name, () => {
const computed = results.find((o) => o['/file4'])?.['/file4']
expect(computed[F_STAT.CHECKSUM]).toBe('computed-hash')
+ expect(fsPromises.readdir.mock.calls.map(([directory]) => directory)).not.toContain(path.join(base, '.sync-in-tmp'))
})
it('should throw a generic error when readdir fails', async () => {
diff --git a/backend/src/applications/sync/services/sync-manager.service.ts b/backend/src/applications/sync/services/sync-manager.service.ts
index a2d5cd2b..35fbe3e7 100644
--- a/backend/src/applications/sync/services/sync-manager.service.ts
+++ b/backend/src/applications/sync/services/sync-manager.service.ts
@@ -9,7 +9,7 @@ import { FILE_OPERATION } from '../../files/constants/operations'
import { FileError } from '../../files/models/file-error'
import { LockConflict } from '../../files/models/file-lock-error'
import { FilesManager } from '../../files/services/files-manager.service'
-import { checksumFile, isPathExists, isPathIsDir, touchFile } from '../../files/utils/files'
+import { checksumFile, isInternalTemporaryEntry, isPathExists, isPathIsDir, touchFile } from '../../files/utils/files'
import { SendFile } from '../../files/utils/send-file'
import { ParseDiffContext } from '../../spaces/interfaces/space-diff.interface'
import { FastifySpaceRequest } from '../../spaces/interfaces/space-request.interface'
@@ -178,6 +178,10 @@ export class SyncManager {
try {
for (const entry of await fs.readdir(dir, { withFileTypes: true })) {
const realPath = path.join(entry.parentPath, entry.name)
+ if (isInternalTemporaryEntry(entry.name)) {
+ this.logger.verbose({ tag: this.parseFiles.name, msg: `ignore internal temporary entry: ${realPath}` })
+ continue
+ }
if (!entry.isDirectory() && !entry.isFile()) {
this.logger.log({ tag: this.parseFiles.name, msg: `ignore special file: ${realPath}` })
continue
diff --git a/backend/src/applications/sync/utils/functions.ts b/backend/src/applications/sync/utils/functions.ts
index 7f9501c5..ebce3658 100644
--- a/backend/src/applications/sync/utils/functions.ts
+++ b/backend/src/applications/sync/utils/functions.ts
@@ -1,10 +1,10 @@
import { BadRequestException } from '@nestjs/common'
import path from 'node:path'
import safeRegex from 'safe-regex2'
-import { SYNC_FILE_NAME_PREFIX, SYNC_MAX_PATH_FILTER_PATTERN_LENGTH, SYNC_MAX_PATH_FILTER_REPETITIONS } from '../constants/sync'
+import { SYNC_MAX_PATH_FILTER_PATTERN_LENGTH, SYNC_MAX_PATH_FILTER_REPETITIONS, SYNC_TEMPORARY_FILE_PREFIX } from '../constants/sync'
export function getSyncTmpFilePath(rPath: string): string {
- return `${path.dirname(rPath)}/${SYNC_FILE_NAME_PREFIX}${path.basename(rPath)}`
+ return `${path.dirname(rPath)}/${SYNC_TEMPORARY_FILE_PREFIX}${path.basename(rPath)}`
}
export function transformPathFilters(value: unknown): RegExp | null {
diff --git a/backend/src/applications/users/admin-users.controller.ts b/backend/src/applications/users/admin-users.controller.ts
index a2bb8275..351ea7bc 100644
--- a/backend/src/applications/users/admin-users.controller.ts
+++ b/backend/src/applications/users/admin-users.controller.ts
@@ -58,7 +58,7 @@ export class AdminUsersController {
@Delete(`${ADMIN_USERS_ROUTE.USERS}/:id`)
@UseGuards(AuthTwoFaVerificationGuard)
deleteUser(@Param('id', ParseIntPipe) userId: number, @Body() deleteUserDto: DeleteUserDto): Promise<void> {
- return this.adminUsersManager.deleteUserOrGuestFromAdmin(userId, { ...deleteUserDto, isGuest: false })
+ return this.adminUsersManager.deleteUserFromAdmin(userId, deleteUserDto)
}
@Get(`${ADMIN_USERS_ROUTE.GUESTS}/${ADMIN_USERS_ROUTE.LIST}`)
@@ -85,8 +85,8 @@ export class AdminUsersController {
@Delete(`${ADMIN_USERS_ROUTE.GUESTS}/:id`)
@UseGuards(AuthTwoFaVerificationGuard)
- deleteGuest(@Param('id', ParseIntPipe) guestId: number, @Body() deleteUserDto: DeleteUserDto): Promise<void> {
- return this.adminUsersManager.deleteUserOrGuestFromAdmin(guestId, { ...deleteUserDto, isGuest: true })
+ deleteGuest(@Param('id', ParseIntPipe) guestId: number): Promise<void> {
+ return this.adminUsersManager.deleteGuestFromAdmin(guestId)
}
@Search(ADMIN_USERS_ROUTE.MEMBERS)
diff --git a/backend/src/applications/users/constants/user.ts b/backend/src/applications/users/constants/user.ts
index 42cf9b26..448bdf58 100644
--- a/backend/src/applications/users/constants/user.ts
+++ b/backend/src/applications/users/constants/user.ts
@@ -2,10 +2,7 @@ export const USER_PASSWORD_MIN_LENGTH = 8
export const USER_MAX_PASSWORD_ATTEMPTS = 10
export const USER_LOGIN_VALIDATION = /^(?! )(?!.* $)[a-zA-Z0-9@\-._ ]{2,255}$/
-export const USER_PATH = {
- TMP: 'tmp',
- TASKS: 'tasks'
-} as const
+export const USER_TMP_DIRECTORY = 'tmp'
export enum USER_ROLE {
ADMINISTRATOR,
diff --git a/backend/src/applications/users/dto/delete-user.dto.ts b/backend/src/applications/users/dto/delete-user.dto.ts
index 9b3ce975..a14408f1 100644
--- a/backend/src/applications/users/dto/delete-user.dto.ts
+++ b/backend/src/applications/users/dto/delete-user.dto.ts
@@ -3,7 +3,4 @@ import { IsBoolean } from 'class-validator'
export class DeleteUserDto {
@IsBoolean()
deleteSpace: boolean
-
- @IsBoolean()
- isGuest: boolean
}
diff --git a/backend/src/applications/users/models/user.model.ts b/backend/src/applications/users/models/user.model.ts
index 4d42f351..a9d484da 100644
--- a/backend/src/applications/users/models/user.model.ts
+++ b/backend/src/applications/users/models/user.model.ts
@@ -5,11 +5,11 @@ import { popFromObject } from '../../../common/shared'
import { configuration } from '../../../configuration/config.environment'
import { isPathInside } from '../../files/utils/files'
import { SPACE_REPOSITORY } from '../../spaces/constants/spaces'
-import { GUEST_PERMISSION, USER_PATH, USER_PERMISSION, USER_PERMS_SEP, USER_ROLE } from '../constants/user'
+import { GUEST_PERMISSION, USER_PERMISSION, USER_PERMS_SEP, USER_ROLE, USER_TMP_DIRECTORY } from '../constants/user'
import type { Owner } from '../interfaces/owner.interface'
import type { UserSecrets } from '../interfaces/user-secrets.interface'
import type { User } from '../schemas/user.interface'
-import { isSafePathSegment } from '../utils/login'
+import { isSafePathSegment, validateUserId } from '../utils/login'
export class UserModel implements User {
id: number
@@ -70,7 +70,7 @@ export class UserModel implements User {
private _homePath: string
get homePath(): string {
- return (this._homePath ||= UserModel.getHomePath(this.login, this.isGuest, this.isLink))
+ return (this._homePath ||= this.isLink ? UserModel.getLinkHomePath(this.id) : UserModel.getHomePath(this.login))
}
private _filesPath: string
@@ -88,13 +88,7 @@ export class UserModel implements User {
private _tmpPath: string
get tmpPath(): string {
- return (this._tmpPath ||= path.join(this.homePath, USER_PATH.TMP))
- }
-
- private _tasksPath: string
-
- get tasksPath(): string {
- return (this._tasksPath ||= path.join(this.tmpPath, USER_PATH.TASKS))
+ return (this._tmpPath ||= path.join(this.homePath, USER_TMP_DIRECTORY))
}
@Expose()
@@ -132,14 +126,11 @@ export class UserModel implements User {
return this.secrets?.appPasswords?.length || 0
}
- static getHomePath(userLogin: string, isGuest = false, isLink = false): string {
+ static getHomePath(userLogin: string): string {
if (!isSafePathSegment(userLogin)) {
throw new Error('User login must be a single path segment')
}
- const basePath =
- isGuest || isLink
- ? path.join(configuration.applications.files.tmpPath, isGuest ? 'guests' : 'links')
- : configuration.applications.files.usersPath
+ const basePath = configuration.applications.files.usersPath
const homePath = path.resolve(basePath, userLogin)
if (!isPathInside(basePath, homePath)) {
throw new Error('User login resolves outside user data directory')
@@ -155,8 +146,22 @@ export class UserModel implements User {
return path.join(UserModel.getHomePath(userLogin), SPACE_REPOSITORY.TRASH)
}
- static getTmpPath(userLogin: string, isGuest = false, isLink = false): string {
- return path.join(UserModel.getHomePath(userLogin, isGuest, isLink), USER_PATH.TMP)
+ static getTmpPath(userLogin: string): string {
+ return path.join(UserModel.getHomePath(userLogin), USER_TMP_DIRECTORY)
+ }
+
+ static getLinkHomePath(userId: number): string {
+ validateUserId(userId)
+ const basePath = configuration.applications.files.linksPath
+ const homePath = path.resolve(basePath, String(userId))
+ if (!isPathInside(basePath, homePath)) {
+ throw new Error('Link user id resolves outside link data directory')
+ }
+ return homePath
+ }
+
+ static getLinkTmpPath(userId: number): string {
+ return path.join(UserModel.getLinkHomePath(userId), USER_TMP_DIRECTORY)
}
static getRepositoryPath(userLogin: string, inTrash = false): string {
@@ -179,12 +184,10 @@ export class UserModel implements User {
}
async makePaths(): Promise<void> {
- if (this.isGuest || this.isLink) {
- await fs.mkdir(this.tasksPath, { recursive: true })
- } else {
- for (const p of [this.filesPath, this.trashPath, this.tasksPath]) {
- await fs.mkdir(p, { recursive: true })
- }
+ if (this.isLink) return
+ const paths = this.isGuest ? [this.tmpPath] : [this.filesPath, this.trashPath, this.tmpPath]
+ for (const p of paths) {
+ await fs.mkdir(p, { recursive: true })
}
}
diff --git a/backend/src/applications/users/services/admin-users-manager.service.spec.ts b/backend/src/applications/users/services/admin-users-manager.service.spec.ts
index 394ab025..a8a11211 100644
--- a/backend/src/applications/users/services/admin-users-manager.service.spec.ts
+++ b/backend/src/applications/users/services/admin-users-manager.service.spec.ts
@@ -5,7 +5,6 @@ import { GROUP_TYPE } from '../constants/group'
import { USER_GROUP_ROLE, USER_ROLE } from '../constants/user'
import type { CreateOrUpdateGroupDto } from '../dto/create-or-update-group.dto'
import type { CreateUserDto, UpdateUserDto, UpdateUserFromGroupDto } from '../dto/create-or-update-user.dto'
-import type { DeleteUserDto } from '../dto/delete-user.dto'
import type { SearchMembersDto } from '../dto/search-members.dto'
import { UserModel } from '../models/user.model'
import { AdminUsersManager } from './admin-users-manager.service'
@@ -366,7 +365,7 @@ describe(AdminUsersManager.name, () => {
adminQueriesMock.deleteUser.mockResolvedValueOnce(true)
fs.isPathExists.mockResolvedValueOnce(true)
fs.removeFiles.mockResolvedValueOnce(undefined)
- await expect(service.deleteUserOrGuest(10, 'john', { deleteSpace: true, isGuest: false })).resolves.toBeUndefined()
+ await expect(service.deleteUserOrGuest(10, 'john', { deleteSpace: true })).resolves.toBeUndefined()
expect(adminQueriesMock.deleteUser).toHaveBeenCalledWith(10, 'john')
expect(fs.isPathExists).toHaveBeenCalled()
expect(fs.removeFiles).toHaveBeenCalled()
@@ -376,17 +375,42 @@ describe(AdminUsersManager.name, () => {
})
})
- describe('deleteUserFromAdmin', () => {
- it('admin password incorrect / deletion ok', async () => {
- adminQueriesMock.usersQueries.compareUserPassword.mockResolvedValueOnce(false)
- await expectHttp(service.deleteUserOrGuestFromAdmin(10, { isGuest: false } as DeleteUserDto))
-
- adminQueriesMock.usersQueries.compareUserPassword.mockResolvedValueOnce(true)
- adminQueriesMock.usersQueries.from.mockResolvedValueOnce({ id: 10, login: 'to-del' } as any)
+ describe('deleteUserFromAdmin / deleteGuestFromAdmin', () => {
+ it('deletes a user from the user endpoint', async () => {
+ adminQueriesMock.usersQueries.from.mockResolvedValueOnce({ id: 10, login: 'to-del', role: USER_ROLE.USER } as any)
adminQueriesMock.deleteUser.mockResolvedValueOnce(true)
- await service.deleteUserOrGuestFromAdmin(10, { isGuest: false, deleteSpace: true } as DeleteUserDto)
+
+ await service.deleteUserFromAdmin(10, { deleteSpace: true })
+
expect(adminQueriesMock.deleteUser).toHaveBeenCalledWith(10, 'to-del')
})
+
+ it.each([USER_ROLE.GUEST, USER_ROLE.LINK])('rejects role %s from the user endpoint', async (role) => {
+ adminQueriesMock.usersQueries.from.mockResolvedValueOnce({ id: 10, login: 'mismatch', role } as any)
+
+ await expectHttp(service.deleteUserFromAdmin(10, { deleteSpace: false }))
+
+ expect(adminQueriesMock.deleteUser).not.toHaveBeenCalled()
+ })
+
+ it('deletes a guest and always removes its home', async () => {
+ adminQueriesMock.usersQueries.from.mockResolvedValueOnce({ id: 11, login: 'guest', role: USER_ROLE.GUEST } as any)
+ adminQueriesMock.deleteUser.mockResolvedValueOnce(true)
+ fs.isPathExists.mockResolvedValueOnce(false)
+
+ await service.deleteGuestFromAdmin(11)
+
+ expect(adminQueriesMock.deleteUser).toHaveBeenCalledWith(11, 'guest')
+ expect(fs.isPathExists).toHaveBeenCalled()
+ })
+
+ it('rejects a regular user from the guest endpoint', async () => {
+ adminQueriesMock.usersQueries.from.mockResolvedValueOnce({ id: 10, login: 'user', role: USER_ROLE.USER } as any)
+
+ await expectHttp(service.deleteGuestFromAdmin(10))
+
+ expect(adminQueriesMock.deleteUser).not.toHaveBeenCalled()
+ })
})
describe('groups', () => {
diff --git a/backend/src/applications/users/services/admin-users-manager.service.ts b/backend/src/applications/users/services/admin-users-manager.service.ts
index f94e08ec..8524d248 100644
--- a/backend/src/applications/users/services/admin-users-manager.service.ts
+++ b/backend/src/applications/users/services/admin-users-manager.service.ts
@@ -102,7 +102,7 @@ export class AdminUsersManager {
if (await this.adminQueries.usersQueries.checkUserExists(v)) {
throw new HttpException('Login already used', HttpStatus.FORBIDDEN)
}
- if (userRole === USER_ROLE.USER) {
+ if (userRole === USER_ROLE.USER || userRole === USER_ROLE.GUEST) {
if (!(await this.renameUserSpace(user.login, v))) {
throw new HttpException('Unable to rename user space', HttpStatus.INTERNAL_SERVER_ERROR)
}
@@ -186,7 +186,7 @@ export class AdminUsersManager {
this.logger.error({ tag: this.deleteUserOrGuest.name, msg: `*${userLogin}* (${userId}) was not deleted : not found` })
}
if (deleteUserDto.deleteSpace) {
- await this.deleteUserSpace(userLogin, deleteUserDto.isGuest)
+ await this.deleteUserSpace(userLogin)
}
} catch (e) {
this.logger.error({ tag: this.deleteUserOrGuest.name, msg: `unable to delete *${userLogin}* (${userId}) : ${e}` })
@@ -194,15 +194,20 @@ export class AdminUsersManager {
}
}
- async deleteUserOrGuestFromAdmin(userId: number, deleteUserDto: DeleteUserDto): Promise<void> {
+ async deleteUserFromAdmin(userId: number, deleteUserDto: DeleteUserDto): Promise<void> {
const userToDelete: UserModel = this.checkUser(await this.adminQueries.usersQueries.from(userId))
- if (userToDelete.isGuest !== deleteUserDto.isGuest) {
+ if (!userToDelete.isUser) {
throw new HttpException('User mismatch', HttpStatus.BAD_REQUEST)
}
- await this.deleteUserOrGuest(userToDelete.id, userToDelete.login, {
- deleteSpace: deleteUserDto.isGuest ? true : deleteUserDto.deleteSpace,
- isGuest: deleteUserDto.isGuest
- } satisfies DeleteUserDto)
+ await this.deleteUserOrGuest(userToDelete.id, userToDelete.login, deleteUserDto)
+ }
+
+ async deleteGuestFromAdmin(guestId: number): Promise<void> {
+ const guestToDelete: UserModel = this.checkUser(await this.adminQueries.usersQueries.from(guestId))
+ if (!guestToDelete.isGuest) {
+ throw new HttpException('User mismatch', HttpStatus.BAD_REQUEST)
+ }
+ await this.deleteUserOrGuest(guestToDelete.id, guestToDelete.login, { deleteSpace: true })
}
listGuests(): Promise<AdminUser[]> {
@@ -338,8 +343,8 @@ export class AdminUsersManager {
return this.authManager.setCookies(admin, res)
}
- async deleteUserSpace(userLogin: string, isGuest = false): Promise<void> {
- const userSpace: string = UserModel.getHomePath(userLogin, isGuest)
+ async deleteUserSpace(userLogin: string): Promise<void> {
+ const userSpace: string = UserModel.getHomePath(userLogin)
try {
if (await isPathExists(userSpace)) {
await removeFiles(userSpace)
diff --git a/backend/src/applications/users/services/users-manager.service.spec.ts b/backend/src/applications/users/services/users-manager.service.spec.ts
index 303e1e14..a8d65cef 100755
--- a/backend/src/applications/users/services/users-manager.service.spec.ts
+++ b/backend/src/applications/users/services/users-manager.service.spec.ts
@@ -58,7 +58,7 @@ describe(UsersManager.name, () => {
dataPath: configuration.applications.files.dataPath,
usersPath: configuration.applications.files.usersPath,
spacesPath: configuration.applications.files.spacesPath,
- tmpPath: configuration.applications.files.tmpPath
+ linksPath: configuration.applications.files.linksPath
}
const flush = () => new Promise<void>((r) => setImmediate(r))
const okStream = (d = 'OK') => {
@@ -91,7 +91,7 @@ describe(UsersManager.name, () => {
configuration.applications.files.dataPath = testDataPath
configuration.applications.files.usersPath = path.join(testDataPath, 'users')
configuration.applications.files.spacesPath = path.join(testDataPath, 'spaces')
- configuration.applications.files.tmpPath = path.join(testDataPath, 'tmp')
+ configuration.applications.files.linksPath = path.join(testDataPath, 'links')
const module: TestingModule = await Test.createTestingModule({
providers: [
@@ -116,7 +116,7 @@ describe(UsersManager.name, () => {
usersQueriesService = module.get(UsersQueries)
cache = module.get(Cache)
userTest = new UserModel(generateUserTest(), false)
- deleteUserDto = { deleteSpace: true, isGuest: false } satisfies DeleteUserDto
+ deleteUserDto = { deleteSpace: true } satisfies DeleteUserDto
})
afterEach(() => vi.restoreAllMocks())
@@ -126,7 +126,7 @@ describe(UsersManager.name, () => {
configuration.applications.files.dataPath = initialFilesPaths.dataPath
configuration.applications.files.usersPath = initialFilesPaths.usersPath
configuration.applications.files.spacesPath = initialFilesPaths.spacesPath
- configuration.applications.files.tmpPath = initialFilesPaths.tmpPath
+ configuration.applications.files.linksPath = initialFilesPaths.linksPath
await fs.rm(testDataPath, { recursive: true, force: true })
})
@@ -508,8 +508,10 @@ describe(UsersManager.name, () => {
convertTempImageToPngMock.mockResolvedValueOnce(undefined)
await expect(usersManager.updateAvatar(mkReq('image/png', okStream()) as any)).resolves.toBeUndefined()
- const expectedSrc = path.join(userTest.tmpPath, 'avatar.png')
+ const expectedSrc = convertTempImageToPngMock.mock.calls[convertTempImageToPngMock.mock.calls.length - 1][0]
const expectedDst = path.join(userTest.homePath, 'avatar.png')
+ expect(path.dirname(expectedSrc)).toBe(userTest.tmpPath)
+ expect(path.basename(expectedSrc)).toMatch(/^~tmp-avatar-[a-z0-9-]+-avatar\.png$/)
expect(convertTempImageToPngMock).toHaveBeenLastCalledWith(expectedSrc, expectedDst)
})
@@ -852,7 +854,7 @@ describe(UsersManager.name, () => {
usersQueriesService.isGuestManager = vi.fn().mockResolvedValue({ id: 9, login: 'guest' })
const delSpy = vi.spyOn(adminUsersManager, 'deleteUserOrGuest').mockResolvedValue(undefined)
await expect(usersManager.deleteGuest(userTest, 9)).resolves.toBeUndefined()
- expect(delSpy).toHaveBeenCalledWith(9, 'guest', { deleteSpace: true, isGuest: true })
+ expect(delSpy).toHaveBeenCalledWith(9, 'guest', { deleteSpace: true })
})
it('proxies forward search + online + whitelist', async () => {
diff --git a/backend/src/applications/users/services/users-manager.service.ts b/backend/src/applications/users/services/users-manager.service.ts
index bc6dd28e..436e3839 100755
--- a/backend/src/applications/users/services/users-manager.service.ts
+++ b/backend/src/applications/users/services/users-manager.service.ts
@@ -16,7 +16,7 @@ import { convertTempImageToPng, generateAvatar, imgMimeTypePrefix, pngMimeType,
import { createLightSlug, genPassword } from '../../../common/shared'
import { configuration, serverConfig } from '../../../configuration/config.environment'
import { Cache } from '../../../infrastructure/cache/cache.service'
-import { isPathExists, removeFiles, sanitizeName } from '../../files/utils/files'
+import { isPathExists, removeFiles, sanitizeName, temporaryFilePath } from '../../files/utils/files'
import { NOTIFICATION_APP, NOTIFICATION_APP_EVENT } from '../../notifications/constants/notifications'
import { NotificationsManager } from '../../notifications/services/notifications-manager.service'
import { GROUP_TYPE } from '../constants/group'
@@ -207,7 +207,7 @@ export class UsersManager {
if (!part.mimetype.startsWith(imgMimeTypePrefix)) {
throw new HttpException('Unsupported file type', HttpStatus.BAD_REQUEST)
}
- const tmpPath = path.join(req.user.tmpPath, USER_AVATAR_FILE_NAME)
+ const tmpPath = temporaryFilePath(req.user.tmpPath, USER_AVATAR_FILE_NAME, 'avatar')
try {
await pipeline(part.file, createWriteStream(tmpPath))
} catch (e) {
@@ -596,7 +596,7 @@ export class UsersManager {
throw new HttpException('You are not allowed to do this action', HttpStatus.FORBIDDEN)
}
// guest has no space but a temporary directory
- return this.adminUsersManager.deleteUserOrGuest(guest.id, guest.login, { deleteSpace: true, isGuest: true })
+ return this.adminUsersManager.deleteUserOrGuest(guest.id, guest.login, { deleteSpace: true })
}
searchMembers(user: UserModel, searchMembersDto: SearchMembersDto): Promise<Member[]> {
diff --git a/backend/src/applications/users/users.e2e-spec.ts b/backend/src/applications/users/users.e2e-spec.ts
index 2e829312..a19fe11f 100644
--- a/backend/src/applications/users/users.e2e-spec.ts
+++ b/backend/src/applications/users/users.e2e-spec.ts
@@ -28,7 +28,7 @@ describe('Users (e2e)', () => {
afterAll(async () => {
await expect(
- adminUsersManager.deleteUserOrGuest(userTest.id, userTest.login, { deleteSpace: true, isGuest: false } satisfies DeleteUserDto)
+ adminUsersManager.deleteUserOrGuest(userTest.id, userTest.login, { deleteSpace: true } satisfies DeleteUserDto)
).resolves.not.toThrow()
await app.close()
})
diff --git a/backend/src/applications/users/users.md b/backend/src/applications/users/users.md
new file mode 100644
index 00000000..02257888
--- /dev/null
+++ b/backend/src/applications/users/users.md
@@ -0,0 +1,215 @@
+# Users
+
+The `users` module manages regular users, guests, link pseudo-users, their home
+directories, and their temporary files.
+
+## Homes and temporary files
+
+Temporary working data is stored with the targeted storage rather than with the
+authenticated actor. Archives produced for user download are the exception: they
+are stored in the actor's home until their task is removed or expires.
+
+All paths in this section are relative to the configured application data path,
+unless an external storage root is explicitly shown.
+
+### Regular user home
+
+A regular user has a complete home containing personal files, trash, temporary
+files, and optional avatar files:
+
+```text
+users/<login>/
+├── files/
+├── trash/
+├── tmp/
+├── avatar.png
+└── avatar.json
+```
+
+### Guest home
+
+Guests share the same login namespace and `users` root as regular users. A guest
+has no personal files or trash repository:
+
+```text
+users/<login>/
+├── tmp/
+├── avatar.png
+└── avatar.json
+```
+
+The avatar files are optional. The guest home follows the guest lifecycle and is
+created, renamed, and removed with the guest.
+
+### Link home
+
+A link pseudo-user has a minimal home dedicated to downloadable artifacts:
+
+```text
+links/<link-user-id>/
+└── tmp/
+```
+
+`<link-user-id>` is the immutable numeric identifier of the link pseudo-user,
+not its login. The home is created lazily and removed with the link.
+
+### Target temporary storage
+
+Working files targeting a native space are isolated by actor:
+
+```text
+spaces/<space-alias>/
+├── files/
+├── trash/
+└── tmp/
+ └── users/
+ └── <actor-id>/
+```
+
+For storage without a managed native-space home, working files are stored under
+the physical storage root:
+
+```text
+<storage-root>/.sync-in-tmp/users/<actor-id>/
+```
+
+The temporary-path resolver determines the physical storage behind a space or
+share, selects the corresponding actor directory, and keeps the resulting path
+inside that temporary root. Temporary directories are created lazily when first
+needed.
+
+`.sync-in-tmp` is an internal reserved directory. It is never exposed through
+filesystem browsing, search, content indexing, archives, recursive copies, or
+Sync diffs, even when hidden files are enabled. Direct requests targeting this
+directory are rejected.
+
+Target working files never use the authenticated actor's home `tmp` directory.
+There is no additional `staging` directory.
+
+### Temporary-file names
+
+Every operation artifact follows the same naming convention:
+
+```text
+~tmp-<operation>-<execution-id>-<basename>
+```
+
+The components have the following meaning:
+
+- `<operation>` is the underlying operation, such as `upload`, `download`,
+ `compress`, `decompress`, `copy`, `move`, `delete`, or `avatar`;
+- `<execution-id>` is a generated UUID for a direct operation;
+- `<execution-id>` is `task.id` for an operation executed as a task;
+- `<basename>` is a sanitized basename, truncated when required by filesystem
+ filename limits.
+
+Examples:
+
+```text
+~tmp-upload-550e8400-e29b-41d4-a716-446655440000-report.pdf
+~tmp-download-6c105f70-f5c4-49af-94b9-f341bbaa1047-video.mp4
+~tmp-compress-6c105f70-f5c4-49af-94b9-f341bbaa1047-photos.zip
+~tmp-decompress-6c105f70-f5c4-49af-94b9-f341bbaa1047-archive
+~tmp-avatar-550e8400-e29b-41d4-a716-446655440000-avatar.png
+```
+
+The actor identifier is already present in the parent path and is not repeated
+in the filename.
+
+The `.sync-in.*` files used by Sync have a separate lifecycle and do not follow
+this convention. Their prefix is also reserved: these entries and directories
+are excluded from filesystem browsing, search, content indexing, archives,
+recursive copies, task metrics, and Sync diffs. Sync creates and manages them
+internally while publishing the corresponding destination file.
+
+### Direct operation lifecycle
+
+Direct operations create their working artifacts in the target temporary
+directory. This includes:
+
+- staged HTTP and multipart overwrites;
+- staged WebDAV overwrites;
+- downloads from a URL;
+- compression into the target directory;
+- decompression;
+- copy and move staging when an operation requires it;
+- other intermediate files associated with a destination.
+
+New files and resumable uploads may write directly to their destination when
+the destination itself is the required checkpoint; they do not create a
+separate temporary artifact.
+
+After validation, an artifact is moved to its final destination. It is removed
+when validation, publication, or an earlier operation step fails.
+
+Avatar updates are profile operations rather than target-storage operations. An
+avatar is prepared in the user or guest home `tmp` directory before being
+published as `avatar.png` at the home root.
+
+### Task artifact lifecycle
+
+A task is the asynchronous execution of an existing operation. Task artifacts
+therefore use the operation naming convention with the task identifier:
+
+```text
+~tmp-<task.type>-<task.id>-<basename>
+```
+
+Task working artifacts remain with the target. For example:
+
+```text
+spaces/project/tmp/users/42/~tmp-decompress-<task-id>-archive
+<external-root>/.sync-in-tmp/users/42/~tmp-copy-<task-id>-directory
+```
+
+All artifacts belonging to a task share the following prefix:
+
+```text
+~tmp-<task.type>-<task.id>-
+```
+
+### Downloadable task archives
+
+A compression task with `compressInDirectory` set to `false` produces an archive
+for user download. Unlike other task artifacts, this archive is created and kept
+in the actor's home:
+
+```text
+USER -> users/<login>/tmp/~tmp-compress-<task-id>-photos.zip
+GUEST -> users/<login>/tmp/~tmp-compress-<task-id>-photos.zip
+LINK -> links/<link-user-id>/tmp/~tmp-compress-<task-id>-photos.zip
+```
+
+The physical filename is internal. `task.name` contains the logical filename
+presented to the user. The download response uses that logical name explicitly:
+
+```ts
+new SendFile(archivePath, task.name)
+```
+
+For example, the physical file
+`~tmp-compress-<task-id>-photos.zip` is downloaded as `photos.zip`.
+
+### Cleanup
+
+An operation removes its working artifact after successful publication or after
+an error. Consequently, completed copy, move, download, compression, and
+decompression tasks do not retain working artifacts on their target storage.
+
+When a downloadable archive task is removed, the task manager removes its
+retained archive using the task ownership prefix:
+
+```text
+~tmp-<task.type>-<task.id>-
+```
+
+The scheduler scans user, guest, link, native-space, personal-storage, and
+registered external-storage temporary roots. It preserves prefixes owned by a
+task that is still cached. Target-storage roots remove expired orphaned
+`~tmp-*` artifacts; home `tmp` directories also remove expired legacy entries.
+These files may be left by a process crash, interruption, task-cache expiration,
+or an earlier storage layout. The scheduler acts as a recovery mechanism and
+does not replace normal operation or task cleanup.
+
+The former `tmp/guests` and `tmp/links` paths are not part of this layout and do
+not receive compatibility cleanup.
diff --git a/backend/src/applications/users/utils/login.ts b/backend/src/applications/users/utils/login.ts
index d2ebb622..e177ddd4 100644
--- a/backend/src/applications/users/utils/login.ts
+++ b/backend/src/applications/users/utils/login.ts
@@ -7,3 +7,9 @@ export function isSafePathSegment(value: unknown): value is string {
export function isValidUserLogin(login: unknown): login is string {
return isSafePathSegment(login) && USER_LOGIN_VALIDATION.test(login)
}
+
+export function validateUserId(userId: number): void {
+ if (!Number.isSafeInteger(userId) || userId <= 0) {
+ throw new Error('User id must be a positive safe integer')
+ }
+}
diff --git a/backend/src/applications/webdav/services/webdav-methods.service.spec.ts b/backend/src/applications/webdav/services/webdav-methods.service.spec.ts
index f45d02ba..35ddf3f4 100644
--- a/backend/src/applications/webdav/services/webdav-methods.service.spec.ts
+++ b/backend/src/applications/webdav/services/webdav-methods.service.spec.ts
@@ -24,8 +24,7 @@ vi.mock('../../files/utils/files', () => ({
dirName: vi.fn(),
genEtag: vi.fn().mockReturnValue('W/"etag-123"'),
removeFiles: vi.fn(() => Promise.resolve()),
- tempFilePath: vi.fn().mockReturnValue('/tmp/test-user/file.txt-upload-id'),
- temporaryPathPrefix: vi.fn().mockReturnValue('file.txt-upload-')
+ temporaryFilePath: vi.fn().mockReturnValue('/space/tmp/users/1/~tmp-upload-id-file.txt')
}))
vi.mock('../../spaces/utils/permissions', () => ({
@@ -34,7 +33,7 @@ vi.mock('../../spaces/utils/permissions', () => ({
vi.mock('../../spaces/utils/paths', async (importOriginal) => {
const actual = await importOriginal<typeof import('../../spaces/utils/paths')>()
- return { ...actual, dbFileFromSpace: vi.fn() }
+ return { ...actual, dbFileFromSpace: vi.fn(), temporaryRootFromSpace: vi.fn().mockReturnValue('/space/tmp/users/1') }
})
vi.mock('../decorators/if-header.decorator', () => ({
@@ -640,7 +639,7 @@ describe('WebDAVMethods', () => {
req.user,
req.space,
req,
- expect.objectContaining({ dav: expect.any(Object), tmpPath: '/tmp/test-user/file.txt-upload-id' })
+ expect.objectContaining({ dav: expect.any(Object), tmpPath: '/space/tmp/users/1/~tmp-upload-id-file.txt' })
)
expect(res.statusCode).toBe(HttpStatus.NO_CONTENT)
expect(res.headers['etag']).toBeDefined()
@@ -726,7 +725,7 @@ describe('WebDAVMethods', () => {
expect(res.statusCode).toBe(409)
expect(res.body).toBe('File conflict')
- expect(removeFiles).toHaveBeenCalledWith('/tmp/test-user/file.txt-upload-id')
+ expect(removeFiles).toHaveBeenCalledWith('/space/tmp/users/1/~tmp-upload-id-file.txt')
})
it('should not clean up direct content-range updates after an error', async () => {
diff --git a/backend/src/applications/webdav/services/webdav-methods.service.ts b/backend/src/applications/webdav/services/webdav-methods.service.ts
index eea3d7a8..eb06dc87 100644
--- a/backend/src/applications/webdav/services/webdav-methods.service.ts
+++ b/backend/src/applications/webdav/services/webdav-methods.service.ts
@@ -7,10 +7,11 @@ import { FileError } from '../../files/models/file-error'
import { LockConflict } from '../../files/models/file-lock-error'
import { FilesLockManager } from '../../files/services/files-lock-manager.service'
import { FilesManager } from '../../files/services/files-manager.service'
-import { dirName, fileName, genEtag, isPathExists, isPathIsDir, removeFiles, tempFilePath, temporaryPathPrefix } from '../../files/utils/files'
+import { dirName, fileName, genEtag, isPathExists, isPathIsDir, removeFiles, temporaryFilePath } from '../../files/utils/files'
+import { FILE_OPERATION } from '../../files/constants/operations'
import { SPACE_OPERATION, SPACE_REPOSITORY } from '../../spaces/constants/spaces'
import { SpaceEnv } from '../../spaces/models/space-env.model'
-import { dbFileFromSpace } from '../../spaces/utils/paths'
+import { dbFileFromSpace, temporaryRootFromSpace } from '../../spaces/utils/paths'
import { haveSpaceEnvPermissions } from '../../spaces/utils/permissions'
import {
DEPTH,
@@ -228,7 +229,7 @@ export class WebDAVMethods {
// Stage only complete overwrites to preserve the previous file on failure. New files and
// ranged uploads write directly because their partial content is the checkpoint used to resume.
if (!isRangeUpload && (await isPathExists(req.space.realPath))) {
- tmpPath = tempFilePath(req.user.tmpPath, temporaryPathPrefix(req.space.realPath, 'upload'))
+ tmpPath = temporaryFilePath(temporaryRootFromSpace(req.user, req.space), req.space.realPath, FILE_OPERATION.UPLOAD)
}
rExists = await this.filesManager.saveStream(req.user, req.space, req, {
...(tmpPath && { tmpPath }),
diff --git a/backend/src/authentication/auth.e2e-spec.ts b/backend/src/authentication/auth.e2e-spec.ts
index 5b9b1ae3..f0aa51bc 100644
--- a/backend/src/authentication/auth.e2e-spec.ts
+++ b/backend/src/authentication/auth.e2e-spec.ts
@@ -63,7 +63,7 @@ describe('Auth (e2e)', () => {
afterAll(async () => {
await expect(
- adminUsersManager.deleteUserOrGuest(userTest.id, userTest.login, { deleteSpace: true, isGuest: false } satisfies DeleteUserDto)
+ adminUsersManager.deleteUserOrGuest(userTest.id, userTest.login, { deleteSpace: true } satisfies DeleteUserDto)
).resolves.not.toThrow()
await app.close()
})
diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts
index 1693f073..631d54c0 100644
--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts
+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.spec.ts
@@ -158,6 +158,7 @@ describe(AuthProviderOIDC.name, () => {
;(service as any).oidcConfig.security.allowPrivateIpAvatarDownload = false
;(service as any).oidcConfig.security.requireVerifiedEmail = false
;(service as any).oidcConfig.options.enablePasswordAuth = false
+ vi.spyOn(filesUtils, 'temporaryFilePath').mockReturnValue('/tmp/sync-in/alice/tmp/~tmp-avatar-test-id-avatar.png')
;(service as any).oidcConfig.options.autoSyncAvatar = false
})
@@ -521,6 +522,7 @@ describe(AuthProviderOIDC.name, () => {
describe('updatePictureUrl', () => {
const oidcUser = { login: 'alice', tmpPath: '/tmp/sync-in/alice/tmp' } as UserModel
+ const avatarTmpPath = '/tmp/sync-in/alice/tmp/~tmp-avatar-test-id-avatar.png'
const userInfo = (picture = 'https://cdn.example.test/avatar.jpg') => ({ picture }) as any
it('returns when picture url is invalid', async () => {
@@ -578,13 +580,11 @@ describe(AuthProviderOIDC.name, () => {
await (service as any).updatePictureUrl(oidcUser, userInfo())
expect(downloadSpy).toHaveBeenCalledTimes(2)
- expect(downloadSpy).toHaveBeenNthCalledWith(
- 2,
- expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }),
- '/tmp/sync-in/alice/tmp/avatar.png',
- { allowPrivateIP: false, maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE }
- )
- expect(convertSpy).toHaveBeenCalledWith('/tmp/sync-in/alice/tmp/avatar.png', '/tmp/sync-in/users/alice/avatar.png')
+ expect(downloadSpy).toHaveBeenNthCalledWith(2, expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }), avatarTmpPath, {
+ allowPrivateIP: false,
+ maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE
+ })
+ expect(convertSpy).toHaveBeenCalledWith(avatarTmpPath, '/tmp/sync-in/users/alice/avatar.png')
expect(metadataSpy).toHaveBeenCalledWith('alice', 'https://cdn.example.test/avatar.jpg', 128, 'Mon, 01 Jan 2024 00:00:00 GMT')
})
@@ -605,18 +605,14 @@ describe(AuthProviderOIDC.name, () => {
await (service as any).updatePictureUrl(oidcUser, userInfo())
- expect(downloadSpy).toHaveBeenNthCalledWith(
- 1,
- expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }),
- '/tmp/sync-in/alice/tmp/avatar.png',
- { allowPrivateIP: true, getContentInfo: true }
- )
- expect(downloadSpy).toHaveBeenNthCalledWith(
- 2,
- expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }),
- '/tmp/sync-in/alice/tmp/avatar.png',
- { allowPrivateIP: true, maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE }
- )
+ expect(downloadSpy).toHaveBeenNthCalledWith(1, expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }), avatarTmpPath, {
+ allowPrivateIP: true,
+ getContentInfo: true
+ })
+ expect(downloadSpy).toHaveBeenNthCalledWith(2, expect.objectContaining({ url: 'https://cdn.example.test/avatar.jpg' }), avatarTmpPath, {
+ allowPrivateIP: true,
+ maxSize: avatarUtils.USER_AVATAR_MAX_UPLOAD_SIZE
+ })
})
it('downloads avatar when content length is missing and stores the actual downloaded size', async () => {
@@ -638,7 +634,7 @@ describe(AuthProviderOIDC.name, () => {
expect(downloadSpy).toHaveBeenCalledTimes(2)
expect(metadataUnchangedSpy).not.toHaveBeenCalled()
- expect(convertSpy).toHaveBeenCalledWith('/tmp/sync-in/alice/tmp/avatar.png', '/tmp/sync-in/users/alice/avatar.png')
+ expect(convertSpy).toHaveBeenCalledWith(avatarTmpPath, '/tmp/sync-in/users/alice/avatar.png')
expect(metadataSpy).toHaveBeenCalledWith('alice', 'https://cdn.example.test/avatar.jpg', 1024, 'Mon, 01 Jan 2024 00:00:00 GMT')
})
diff --git a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts
index 7b71ed05..c43b7584 100644
--- a/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts
+++ b/backend/src/authentication/providers/oidc/auth-provider-oidc.service.ts
@@ -46,7 +46,7 @@ import { HttpService } from '@nestjs/axios'
import { DownloadFileDto } from '../../../applications/files/dto/file-operations.dto'
import { DownloadFile } from '../../../applications/files/utils/download-file'
import { convertTempImageToPng, imgMimeTypePrefix } from '../../../common/image'
-import { fileSize } from '../../../applications/files/utils/files'
+import { fileSize, temporaryFilePath } from '../../../applications/files/utils/files'
@Injectable()
export class AuthProviderOIDC implements AuthProvider {
@@ -456,10 +456,10 @@ export class AuthProviderOIDC implements AuthProvider {
let pictureLastModified: string | undefined
const downloader = new DownloadFile(this.http)
const allowPrivateIP = this.oidcConfig.security.allowPrivateIpAvatarDownload
+ const userAvatarTmpPath = temporaryFilePath(user.tmpPath, USER_AVATAR_FILE_NAME, 'avatar')
try {
- const tmpPicturePath = path.join(user.tmpPath, USER_AVATAR_FILE_NAME)
// retrieve headers
- const { contentType, contentLength, lastModified } = await downloader.download(downloadDto, tmpPicturePath, {
+ const { contentType, contentLength, lastModified } = await downloader.download(downloadDto, userAvatarTmpPath, {
allowPrivateIP,
getContentInfo: true
})
@@ -489,7 +489,6 @@ export class AuthProviderOIDC implements AuthProvider {
}
// download avatar
- const userAvatarTmpPath = path.join(user.tmpPath, USER_AVATAR_FILE_NAME)
try {
await downloader.download(downloadDto, userAvatarTmpPath, { allowPrivateIP, maxSize: USER_AVATAR_MAX_UPLOAD_SIZE })
} catch (e) {
diff --git a/backend/src/configuration/config.environment.ts b/backend/src/configuration/config.environment.ts
index 58065e76..160f8cb3 100755
--- a/backend/src/configuration/config.environment.ts
+++ b/backend/src/configuration/config.environment.ts
@@ -52,7 +52,7 @@ function loadConfiguration(): GlobalConfig {
}
config.applications.files.usersPath = join(config.applications.files.dataPath, 'users')
config.applications.files.spacesPath = join(config.applications.files.dataPath, 'spaces')
- config.applications.files.tmpPath = join(config.applications.files.dataPath, 'tmp')
+ config.applications.files.linksPath = join(config.applications.files.dataPath, 'links')
// DEPRECATIONS
deprecatedFilesEditorsConfig(config)
deprecatedFilesContentIndexingConfig(config)
diff --git a/frontend/src/app/applications/admin/admin.service.ts b/frontend/src/app/applications/admin/admin.service.ts
index 23f33f38..c7e2ee44 100644
--- a/frontend/src/app/applications/admin/admin.service.ts
+++ b/frontend/src/app/applications/admin/admin.service.ts
@@ -96,12 +96,16 @@ export class AdminService {
}
deleteUser(userId: number, deleteUserDto: DeleteUserDto, twoFaHeaders: HttpHeaders): Observable<void> {
- return this.http.request<void>('delete', `${deleteUserDto.isGuest ? API_ADMIN_GUESTS : API_ADMIN_USERS}/${userId}`, {
+ return this.http.request<void>('delete', `${API_ADMIN_USERS}/${userId}`, {
headers: twoFaHeaders,
body: deleteUserDto
})
}
+ deleteGuest(guestId: number, twoFaHeaders: HttpHeaders): Observable<void> {
+ return this.http.delete<void>(`${API_ADMIN_GUESTS}/${guestId}`, { headers: twoFaHeaders })
+ }
+
browseGroup(name?: string, personalGroups = false): Observable<GroupBrowseModel> {
return this.http
.get<GroupBrowse>(`${personalGroups ? API_ADMIN_PERSONAL_GROUPS_BROWSE : API_ADMIN_GROUPS_BROWSE}${name ? `/${name}` : ''}`)
diff --git a/frontend/src/app/applications/admin/components/dialogs/admin-guest-dialog.component.ts b/frontend/src/app/applications/admin/components/dialogs/admin-guest-dialog.component.ts
index e0871274..ec7cf8a1 100644
--- a/frontend/src/app/applications/admin/components/dialogs/admin-guest-dialog.component.ts
+++ b/frontend/src/app/applications/admin/components/dialogs/admin-guest-dialog.component.ts
@@ -4,7 +4,6 @@ import { Component, inject } from '@angular/core'
import { ReactiveFormsModule } from '@angular/forms'
import { FaIconComponent } from '@fortawesome/angular-fontawesome'
import { USER_ROLE } from '@sync-in-server/backend/src/applications/users/constants/user'
-import type { DeleteUserDto } from '@sync-in-server/backend/src/applications/users/dto/delete-user.dto'
import type { SearchMembersDto } from '@sync-in-server/backend/src/applications/users/dto/search-members.dto'
import { L10nTranslateDirective, L10nTranslatePipe } from 'angular-l10n'
import { TabDirective, TabHeadingDirective, TabsetComponent } from 'ngx-bootstrap/tabs'
@@ -76,7 +75,7 @@ export class AdminGuestDialogComponent extends UserGuestDialogComponent {
if (auth2FaHeaders === false) {
return
}
- this.adminService.deleteUser(this.guest.id, { deleteSpace: true, isGuest: true } satisfies DeleteUserDto, auth2FaHeaders).subscribe({
+ this.adminService.deleteGuest(this.guest.id, auth2FaHeaders).subscribe({
next: () => {
this.guestChange.emit(['delete', this.guest])
this.layout.sendNotification('success', 'Guest deleted', this.guest.login)
diff --git a/frontend/src/app/applications/admin/components/dialogs/admin-user-delete-dialog.component.ts b/frontend/src/app/applications/admin/components/dialogs/admin-user-delete-dialog.component.ts
index be49f584..1aed426f 100644
--- a/frontend/src/app/applications/admin/components/dialogs/admin-user-delete-dialog.component.ts
+++ b/frontend/src/app/applications/admin/components/dialogs/admin-user-delete-dialog.component.ts
@@ -39,7 +39,7 @@ export class AdminUserDeleteDialogComponent {
this.onClose()
return
}
- this.adminService.deleteUser(this.user.id, { deleteSpace: this.deleteSpace, isGuest: false } satisfies DeleteUserDto, auth2FaHeaders).subscribe({
+ this.adminService.deleteUser(this.user.id, { deleteSpace: this.deleteSpace } satisfies DeleteUserDto, auth2FaHeaders).subscribe({
next: () => {
this.wasDeleted.emit(true)
this.layout.sendNotification('success', 'Delete user', this.user.login)