Skip to content

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

refactor(backend): centralize file and trash interfaces

johavena71b223ecd40
diff --git a/backend/src/applications/files/interfaces/delete-file.interface.ts b/backend/src/applications/files/interfaces/delete-file.interface.ts
new file mode 100644
index 00000000..7fe95c69
--- /dev/null
+++ b/backend/src/applications/files/interfaces/delete-file.interface.ts
@@ -0,0 +1,3 @@
+export interface DeleteFileOptions {
+ protectedTrashPath?: string
+}
diff --git a/backend/src/applications/files/interfaces/download-file.interface.ts b/backend/src/applications/files/interfaces/download-file.interface.ts
index 7da6962f..a9b23882 100644
--- a/backend/src/applications/files/interfaces/download-file.interface.ts
+++ b/backend/src/applications/files/interfaces/download-file.interface.ts
@@ -15,3 +15,10 @@ export interface DownloadFileOptions {
onProgress?: (bytes: number) => void
signal?: AbortSignal
}
+
+export interface DownloadFileRequestOptions {
+ allowPrivateIP?: boolean
+ maxRedirects?: number
+}
+
+export type DownloadStage = 'head' | 'validation' | 'get' | 'stream'
diff --git a/backend/src/applications/files/interfaces/file-scheduler.interface.ts b/backend/src/applications/files/interfaces/file-scheduler.interface.ts
new file mode 100644
index 00000000..093d3b01
--- /dev/null
+++ b/backend/src/applications/files/interfaces/file-scheduler.interface.ts
@@ -0,0 +1,11 @@
+export interface TemporaryDirectory {
+ includeLegacyEntries: boolean
+ path: string
+}
+
+export interface TemporaryDirectorySnapshot {
+ fileNames: string[]
+ path: string
+}
+
+export type TemporaryDirectoriesByUser = Map<number, Map<string, TemporaryDirectory>>
diff --git a/backend/src/applications/files/interfaces/upload-file.interface.ts b/backend/src/applications/files/interfaces/upload-file.interface.ts
new file mode 100644
index 00000000..22945f93
--- /dev/null
+++ b/backend/src/applications/files/interfaces/upload-file.interface.ts
@@ -0,0 +1,13 @@
+export interface UploadFileStreamLimiter {
+ readonly initialFileSize: number
+ // Checks the number of bytes the current stream is expected to append.
+ assertKnownSize: (bytes: number) => void
+ // Checks the expected final file size, including the current range offset.
+ assertFinalSize: (bytes: number) => void
+ consume: (bytes: number) => void
+}
+
+export interface UploadQuotaSnapshot {
+ storageQuota?: number | null
+ storageUsage?: number
+}
diff --git a/backend/src/applications/files/interfaces/write-stream.interface.ts b/backend/src/applications/files/interfaces/write-stream.interface.ts
new file mode 100644
index 00000000..02b12177
--- /dev/null
+++ b/backend/src/applications/files/interfaces/write-stream.interface.ts
@@ -0,0 +1,15 @@
+import type { UploadFileStreamLimiter } from './upload-file.interface'
+
+export interface WriteFromStreamOptions {
+ start?: number
+ signal?: AbortSignal
+ // Called before forwarding a chunk to the destination. Throwing aborts the pipeline.
+ accountBytes?: (bytes: number) => void
+}
+
+export interface WriteUploadStreamOptions {
+ limiter: UploadFileStreamLimiter
+ signal?: AbortSignal
+ // Receives only bytes accepted by the limiter.
+ onProgress?: (bytes: number) => void
+}
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index e1259234..a903f62e 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -25,6 +25,7 @@ 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 type { DeleteFileOptions } from '../interfaces/delete-file.interface'
import { FileDBProps } from '../interfaces/file-db-props.interface'
import { FileLock } from '../interfaces/file-lock.interface'
import { FileLockProps } from '../interfaces/file-props.interface'
@@ -76,10 +77,6 @@ import { createTar } from '../utils/tar-file'
import { createZip } from '../utils/zip-file'
import { FILE_ERROR } from '../constants/errors'
-interface DeleteOptions {
- protectedTrashPath?: string
-}
-
@Injectable()
export class FilesManager {
// Spaces permissions are checked in the space guard, except for the copy/move destination
@@ -557,7 +554,7 @@ export class FilesManager {
}
}
- async delete(user: UserModel, space: SpaceEnv, dav?: { lockTokens: string[] }, signal?: AbortSignal, options?: DeleteOptions): Promise<void> {
+ async delete(user: UserModel, space: SpaceEnv, dav?: { lockTokens: string[] }, signal?: AbortSignal, options?: DeleteFileOptions): Promise<void> {
const isTaskContext = Boolean(space.task?.cacheKey)
if (!(await isPathExists(space.realPath))) {
throw new FileError(HttpStatus.NOT_FOUND, 'Location not found')
diff --git a/backend/src/applications/files/services/files-scheduler.service.ts b/backend/src/applications/files/services/files-scheduler.service.ts
index ff5448a7..025bd614 100644
--- a/backend/src/applications/files/services/files-scheduler.service.ts
+++ b/backend/src/applications/files/services/files-scheduler.service.ts
@@ -18,6 +18,7 @@ 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 type { TemporaryDirectoriesByUser, TemporaryDirectory, TemporaryDirectorySnapshot } from '../interfaces/file-scheduler.interface'
import { FileTask, FileTaskStatus } from '../models/file-task'
import { filesRecents } from '../schemas/files-recents.schema'
import { files } from '../schemas/files.schema'
@@ -28,18 +29,6 @@ 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
diff --git a/backend/src/applications/files/utils/download-file.ts b/backend/src/applications/files/utils/download-file.ts
index 105ff4ec..454258d0 100644
--- a/backend/src/applications/files/utils/download-file.ts
+++ b/backend/src/applications/files/utils/download-file.ts
@@ -11,19 +11,12 @@ import { HTTP_METHOD } from '../../applications.constants'
import type { SpaceEnv } from '../../spaces/models/space-env.model'
import type { DownloadFileDto } from '../dto/file-operations.dto'
import { FileTaskEvent } from '../events/file-events'
-import type { DownloadFileContentInfo, DownloadFileOptions } from '../interfaces/download-file.interface'
+import type { DownloadFileContentInfo, DownloadFileOptions, DownloadFileRequestOptions, DownloadStage } from '../interfaces/download-file.interface'
import { FileError } from '../models/file-error'
import { writeUploadFromStream } from './files'
import { FILE_ERROR } from '../constants/errors'
import { createUploadStreamLimiter } from './upload-file'
-interface DownloadFileRequestOptions {
- allowPrivateIP?: boolean
- maxRedirects?: number
-}
-
-type DownloadStage = 'head' | 'validation' | 'get' | 'stream'
-
export class DownloadFile {
private readonly logger = new Logger(DownloadFile.name)
private static readonly dnsOptions = { all: true, order: 'verbatim' } as const
diff --git a/backend/src/applications/files/utils/files.ts b/backend/src/applications/files/utils/files.ts
index 3b7a4351..6b843b0a 100644
--- a/backend/src/applications/files/utils/files.ts
+++ b/backend/src/applications/files/utils/files.ts
@@ -14,9 +14,9 @@ import { DEFAULT_CHECKSUM_ALGORITHM, DEFAULT_HIGH_WATER_MARK, EXTRA_MIMES_TYPE,
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 type { WriteFromStreamOptions, WriteUploadStreamOptions } from '../interfaces/write-stream.interface'
import { FileError } from '../models/file-error'
import { maxFileSizeExceededError } from './errors'
-import type { UploadFileStreamLimiter } from './upload-file'
export function sanitizePath(fPath: string): string {
return path.normalize(fPath).replace(regExpPreventPathTraversal, '')
@@ -304,13 +304,6 @@ export function createProgressTransform(
})
}
-export interface WriteFromStreamOptions {
- start?: number
- signal?: AbortSignal
- // Called before forwarding a chunk to the destination. Throwing aborts the pipeline.
- accountBytes?: (bytes: number) => void
-}
-
export function writeFromStream(rPath: string, stream: Readable, options: WriteFromStreamOptions = {}): Promise<void> {
const { start = 0, signal, accountBytes } = options
const dst: WriteStream = createWriteStream(rPath, { flags: start ? 'a' : 'w', start: start, highWaterMark: DEFAULT_HIGH_WATER_MARK })
@@ -361,13 +354,6 @@ export async function writeFromStreamAndChecksum(
return hash.digest('hex')
}
-export interface WriteUploadStreamOptions {
- limiter: UploadFileStreamLimiter
- signal?: AbortSignal
- // Receives only bytes accepted by the limiter.
- onProgress?: (bytes: number) => void
-}
-
function uploadWriteOptions({ limiter, signal, onProgress }: WriteUploadStreamOptions): WriteFromStreamOptions {
return {
start: limiter.initialFileSize,
diff --git a/backend/src/applications/files/utils/upload-file.ts b/backend/src/applications/files/utils/upload-file.ts
index 802b09ad..5175194a 100644
--- a/backend/src/applications/files/utils/upload-file.ts
+++ b/backend/src/applications/files/utils/upload-file.ts
@@ -1,25 +1,12 @@
import { HttpStatus } from '@nestjs/common'
import { FileError } from '../models/file-error'
+import type { UploadFileStreamLimiter, UploadQuotaSnapshot } from '../interfaces/upload-file.interface'
import { maxFileSizeExceededError, storageQuotaExceededError } from './errors'
import { temporaryFilePath } from './files'
import { FILE_OPERATION } from '../constants/operations'
const FASTIFY_MULTIPART_FILE_TOO_LARGE_CODE = 'FST_REQ_FILE_TOO_LARGE' as const
-export interface UploadFileStreamLimiter {
- readonly initialFileSize: number
- // Checks the number of bytes the current stream is expected to append.
- assertKnownSize: (bytes: number) => void
- // Checks the expected final file size, including the current range offset.
- assertFinalSize: (bytes: number) => void
- consume: (bytes: number) => void
-}
-
-export interface UploadQuotaSnapshot {
- storageQuota?: number | null
- storageUsage?: number
-}
-
/**
* Request-scoped upload limiter. It deliberately does not coordinate quota
* reservations between concurrent requests or workers.
diff --git a/backend/src/applications/spaces/interfaces/space-trash.interface.ts b/backend/src/applications/spaces/interfaces/space-trash.interface.ts
index 628ce8ee..e310a76b 100644
--- a/backend/src/applications/spaces/interfaces/space-trash.interface.ts
+++ b/backend/src/applications/spaces/interfaces/space-trash.interface.ts
@@ -1,3 +1,5 @@
+import type { FileDBProps } from '../../files/interfaces/file-db-props.interface'
+
export interface SpaceTrash {
id: number
name: string
@@ -7,3 +9,17 @@ export interface SpaceTrash {
ctime: number
nb: number
}
+
+export interface TrashTarget {
+ dbScope: Omit<FileDBProps, 'path'>
+ mode: 'trash'
+ path: string
+ temporaryRoot: string
+}
+
+export interface PermanentDeleteTarget {
+ mode: 'permanent'
+ reason: 'external-share'
+}
+
+export type TrashTargetResolution = TrashTarget | PermanentDeleteTarget
diff --git a/backend/src/applications/spaces/utils/paths.ts b/backend/src/applications/spaces/utils/paths.ts
index be69639e..706c9340 100644
--- a/backend/src/applications/spaces/utils/paths.ts
+++ b/backend/src/applications/spaces/utils/paths.ts
@@ -8,6 +8,7 @@ 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 type { TrashTarget, TrashTargetResolution } from '../interfaces/space-trash.interface'
import { SpaceEnv } from '../models/space-env.model'
import { SpaceModel } from '../models/space.model'
@@ -80,20 +81,6 @@ export function realPathFromSpace(user: UserModel, space: SpaceEnv, withBasePath
return withBasePath ? [bPath, rPath] : rPath
}
-export interface TrashTarget {
- dbScope: Omit<FileDBProps, 'path'>
- 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 baseDbScope: Omit<FileDBProps, 'path'> = {
ownerId: null,