Skip to content

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

refactor(files): improve file locking with `FileLockProps` and centralized timeout handling

johaven3071709d0bfc
diff --git a/backend/src/applications/files/constants/cache.ts b/backend/src/applications/files/constants/cache.ts
index 658b1f58..de2ac3f3 100644
--- a/backend/src/applications/files/constants/cache.ts
+++ b/backend/src/applications/files/constants/cache.ts
@@ -5,9 +5,10 @@
*/
// cache task key = `ftask-$(userId}-${taskId}` => FileTask
-export const CACHE_TASK_PREFIX = 'ftask'
-export const CACHE_TASK_TTL = 86400 // 1 day
+export const CACHE_TASK_PREFIX = 'ftask' as const
+export const CACHE_TASK_TTL = 86400 as const // one day
// cache token key = `flock|token?:${uuid}|path:${path}|ownerId?:${number}|spaceId?:${number}|...props` => FileLock
-export const CACHE_LOCK_PREFIX = 'flock'
+export const CACHE_LOCK_PREFIX = 'flock' as const
+export const CACHE_LOCK_DEFAULT_TIMEOUT = 28800 as const // 8 hours in seconds
// cache only office = `office|${fileId}` => docKey
-export const CACHE_ONLY_OFFICE = 'foffice'
+export const CACHE_ONLY_OFFICE = 'foffice' as const
diff --git a/backend/src/applications/files/interfaces/file-db-props.interface.ts b/backend/src/applications/files/interfaces/file-db-props.interface.ts
index 3932b631..dc291b09 100644
--- a/backend/src/applications/files/interfaces/file-db-props.interface.ts
+++ b/backend/src/applications/files/interfaces/file-db-props.interface.ts
@@ -7,7 +7,7 @@
import type { File } from '../schemas/file.interface'
export interface FileDBProps extends Partial<Pick<File, 'ownerId' | 'spaceId' | 'spaceExternalRootId' | 'shareExternalId' | 'inTrash' | 'path'>> {
- // warn: used during lock creation, new fields will be used in lock key
+ // warn: used during lock creation, new fields will be used in the lock key
ownerId?: number
spaceId?: number
spaceExternalRootId?: number
diff --git a/backend/src/applications/files/interfaces/file-props.interface.ts b/backend/src/applications/files/interfaces/file-props.interface.ts
index 3c4d09f1..a3acc836 100644
--- a/backend/src/applications/files/interfaces/file-props.interface.ts
+++ b/backend/src/applications/files/interfaces/file-props.interface.ts
@@ -11,6 +11,12 @@ import type { SyncPath } from '../../sync/schemas/sync-path.interface'
import type { Owner } from '../../users/interfaces/owner.interface'
import type { File } from '../schemas/file.interface'
+export interface FileLockProps {
+ owner: string
+ ownerLogin: string
+ isExclusive: boolean
+}
+
export interface FileProps extends Omit<File, 'ownerId' | 'spaceId' | 'spaceExternalRootId' | 'shareExternalId' | 'inTrash'> {
id: number
name: string
@@ -24,14 +30,14 @@ export interface FileProps extends Omit<File, 'ownerId' | 'spaceId' | 'spaceExte
// used with shares
origin?: { ownerLogin: string; spaceAlias: string; spaceRootExternalPath?: string }
// root can be a share or a space root
- // enabled & description are only used for shares
+ // enabled, and description are only used for shares
root?: Pick<SpaceRoot, 'id' | 'alias' | 'permissions'> &
Partial<Pick<SpaceRoot, 'name' | 'externalPath'>> &
Partial<Pick<Share, 'enabled' | 'description'>> & {
owner: Owner
}
- lock?: { owner: string; ownerLogin: string; isExclusive: boolean }
- // used by the files browser to enrich files
+ lock?: FileLockProps
+ // used by the file browser to enrich files
spaces?: Pick<Space, 'id' | 'alias' | 'name'>[]
shares?: Pick<Share, 'id' | 'alias' | 'name' | 'type'>[]
syncs?: Pick<SyncPath, 'clientId' | 'id'> & { clientName: string }[]
diff --git a/backend/src/applications/files/services/files-lock-manager.service.ts b/backend/src/applications/files/services/files-lock-manager.service.ts
index c8ab4e68..63daef5b 100644
--- a/backend/src/applications/files/services/files-lock-manager.service.ts
+++ b/backend/src/applications/files/services/files-lock-manager.service.ts
@@ -12,9 +12,10 @@ import { Cache } from '../../../infrastructure/cache/services/cache.service'
import { UserModel } from '../../users/models/user.model'
import { DEPTH, LOCK_DEPTH, LOCK_PREFIX, LOCK_SCOPE } from '../../webdav/constants/webdav'
import { WebDAVLock } from '../../webdav/interfaces/webdav.interface'
-import { CACHE_LOCK_PREFIX } from '../constants/cache'
+import { CACHE_LOCK_DEFAULT_TIMEOUT, CACHE_LOCK_PREFIX } from '../constants/cache'
import { FileDBProps } from '../interfaces/file-db-props.interface'
import { FileLock } from '../interfaces/file-lock.interface'
+import { FileLockProps } from '../interfaces/file-props.interface'
import { LockConflict } from '../models/file-lock-error'
import { files } from '../schemas/files.schema'
import { dirName, fileName } from '../utils/files'
@@ -34,12 +35,11 @@ export class FilesLockManager {
* If created with OnlyOffice, they are stored with no token and a davLock property whose `locktoken` & `lockroot` properties are null
Cache token key format = `flock|token?:${uuid}|path:${path}|ownerId?:${number}|spaceId?:${number}|...props` => FileLock
*/
- public static readonly defaultLockTimeoutSeconds = 86400 // 1 day
private readonly logger = new Logger(FilesLockManager.name)
constructor(private readonly cache: Cache) {}
- async create(user: UserModel, dbFile: FileDBProps, depth: LOCK_DEPTH, ttl?: number, davLock?: WebDAVLock): Promise<[boolean, FileLock]> {
+ async create(user: UserModel, dbFile: FileDBProps, depth: LOCK_DEPTH, davLock?: WebDAVLock, ttl?: number): Promise<[boolean, FileLock]> {
let token: string
let lockscope: LOCK_SCOPE
if (davLock) {
@@ -60,7 +60,7 @@ export class FilesLockManager {
}
throw new Error(e)
}
- ttl ??= FilesLockManager.defaultLockTimeoutSeconds
+ ttl ??= CACHE_LOCK_DEFAULT_TIMEOUT
const key = `${CACHE_LOCK_PREFIX}|${this.genSuffixKey(dbFile, { depth: depth, token: token })}`
const expiration = Math.floor(currentTimeStamp() + ttl)
const lock: FileLock = {
@@ -158,7 +158,7 @@ export class FilesLockManager {
options?: { userId?: number; lockScope?: LOCK_SCOPE; lockTokens?: string[] }
): Promise<void> {
/* Checks if a file could be modified, created, moved, or deleted
- Throws an `LockConflict` error when there are parent locks (depth: 0) or child locks (depth: infinite) that prevent modifications
+ Throws an `LockConflict` error when there are parent locks (depth: 0) or child locks (depth: infinite) that prevent modification
Returns on the first conflict (compliant with the RFC 4918)
*/
for await (const l of this.searchParentLocks(dbFile, { includeRoot: true, depth: DEPTH.INFINITY })) {
@@ -187,6 +187,28 @@ export class FilesLockManager {
}
}
+ async *searchChildLocks(dbFile: FileDBProps) {
+ const props = this.genSuffixKey(dbFile, { ignorePath: true })
+ const path = dbFile.path === '.' ? '*' : `${dbFile.path}/*`
+ this.logger.verbose(`${this.searchChildLocks.name} - ${path} (${props})`)
+ const keys = await this.searchKeysByPath(path, props)
+ if (keys.length) {
+ for (const l of (await this.cache.mget(keys)).filter(Boolean) as FileLock[]) {
+ this.logger.verbose(`-> ${l.dbFilePath} (owner: ${l.owner.login})`)
+ yield l
+ }
+ }
+ }
+
+ convertLockToFileLockProps(lock: FileLock): FileLockProps {
+ if (!lock) return null
+ return {
+ owner: lock?.davLock?.owner || `${lock.owner.fullName} (${lock.owner.email})`,
+ ownerLogin: lock.owner.login,
+ isExclusive: lock?.davLock?.lockscope ? lock?.davLock?.lockscope === LOCK_SCOPE.EXCLUSIVE : true
+ }
+ }
+
private async *searchParentLocks(dbFile: FileDBProps, options: { includeRoot?: boolean; depth?: LOCK_DEPTH } = {}): AsyncGenerator<FileLock> {
const props = this.genSuffixKey(dbFile, { ignorePath: true })
let path = dbFile.path
@@ -209,19 +231,6 @@ export class FilesLockManager {
}
}
- async *searchChildLocks(dbFile: FileDBProps) {
- const props = this.genSuffixKey(dbFile, { ignorePath: true })
- const path = dbFile.path === '.' ? '*' : `${dbFile.path}/*`
- this.logger.verbose(`${this.searchChildLocks.name} - ${path} (${props})`)
- const keys = await this.searchKeysByPath(path, props)
- if (keys.length) {
- for (const l of (await this.cache.mget(keys)).filter(Boolean) as FileLock[]) {
- this.logger.verbose(`-> ${l.dbFilePath} (owner: ${l.owner.login})`)
- yield l
- }
- }
- }
-
private async searchKeyByToken(token: string): Promise<string> {
const keys = await this.cache.keys(`${CACHE_LOCK_PREFIX}|token:${token}|*`)
if (!keys.length) {
diff --git a/backend/src/applications/files/services/files-only-office-manager.service.ts b/backend/src/applications/files/services/files-only-office-manager.service.ts
index 869dcc5a..dc5824be 100644
--- a/backend/src/applications/files/services/files-only-office-manager.service.ts
+++ b/backend/src/applications/files/services/files-only-office-manager.service.ts
@@ -14,7 +14,6 @@ import os from 'node:os'
import path from 'node:path'
import { JwtIdentityPayload } from '../../../authentication/interfaces/jwt-payload.interface'
import { convertHumanTimeToSeconds, generateShortUUID } from '../../../common/functions'
-import { SERVER_NAME } from '../../../common/shared'
import { configuration } from '../../../configuration/config.environment'
import { Cache } from '../../../infrastructure/cache/services/cache.service'
import { ContextManager } from '../../../infrastructure/context/services/context-manager.service'
@@ -26,6 +25,7 @@ import { haveSpaceEnvPermissions } from '../../spaces/utils/permissions'
import type { UserModel } from '../../users/models/user.model'
import { getAvatarBase64 } from '../../users/utils/avatar'
import { DEPTH, LOCK_SCOPE } from '../../webdav/constants/webdav'
+import { WebDAVLock } from '../../webdav/interfaces/webdav.interface'
import { CACHE_ONLY_OFFICE } from '../constants/cache'
import {
ONLY_OFFICE_CONVERT_ERROR,
@@ -253,12 +253,18 @@ export class FilesOnlyOfficeManager {
}
private async genFileLock(user: UserModel, space: SpaceEnv): Promise<void> {
- const [ok, _fileLock] = await this.filesLockManager.create(user, space.dbFile, DEPTH.RESOURCE, this.expiration, {
- lockroot: null,
- locktoken: null,
- lockscope: LOCK_SCOPE.SHARED,
- owner: `${SERVER_NAME} - ${user.fullName} (${user.email})`
- })
+ const [ok, _fileLock] = await this.filesLockManager.create(
+ user,
+ space.dbFile,
+ DEPTH.RESOURCE,
+ {
+ lockroot: null,
+ locktoken: null,
+ lockscope: LOCK_SCOPE.SHARED,
+ owner: `OnlyOffice - ${user.fullName} (${user.email})`
+ } satisfies WebDAVLock,
+ this.expiration
+ )
if (!ok) {
throw new Error('document is locked')
}
diff --git a/backend/src/applications/webdav/guards/webdav-protocol.guard.spec.ts b/backend/src/applications/webdav/guards/webdav-protocol.guard.spec.ts
index ee22d603..5486292f 100644
--- a/backend/src/applications/webdav/guards/webdav-protocol.guard.spec.ts
+++ b/backend/src/applications/webdav/guards/webdav-protocol.guard.spec.ts
@@ -4,12 +4,12 @@ import { FastifyReply } from 'fastify'
import { urlToPath } from '../../../common/functions'
import { decodeUrl } from '../../../common/shared'
import { HTTP_METHOD } from '../../applications.constants'
-import { FilesLockManager } from '../../files/services/files-lock-manager.service'
+import { CACHE_LOCK_DEFAULT_TIMEOUT } from '../../files/constants/cache'
import { DEPTH, HEADER, LOCK_SCOPE, OPTIONS_HEADERS, PROPSTAT } from '../constants/webdav'
import { FastifyDAVRequest } from '../interfaces/webdav.interface'
+import * as IfHeaderUtils from '../utils/if-header'
import { PROPFIND_ALL_PROP } from '../utils/webdav'
import { WebDAVProtocolGuard } from './webdav-protocol.guard'
-import * as IfHeaderUtils from '../utils/if-header'
// Keep these mocks to control path transforms in COPY/MOVE tests
jest.mock('../../../common/shared', () => ({
@@ -232,7 +232,7 @@ describe(WebDAVProtocolGuard.name, () => {
const res = makeRes()
const ctx = makeCtx(req, res)
await expect(guard.canActivate(ctx)).resolves.toBe(true)
- expect(req.dav.lock.timeout).toBe(FilesLockManager.defaultLockTimeoutSeconds)
+ expect(req.dav.lock.timeout).toBe(CACHE_LOCK_DEFAULT_TIMEOUT)
expect(req.dav.lock.lockscope).toBe(LOCK_SCOPE.EXCLUSIVE)
expect(req.dav.lock.owner).toContain('Custom Owner')
expect(req.dav.depth).toBe(DEPTH.RESOURCE)
@@ -253,7 +253,7 @@ describe(WebDAVProtocolGuard.name, () => {
})
it('clamps timeout Second-N to default when N exceeds default', async () => {
- const big = FilesLockManager.defaultLockTimeoutSeconds + 1000
+ const big = CACHE_LOCK_DEFAULT_TIMEOUT + 1000
const req = baseReq(HTTP_METHOD.LOCK, {
headers: { [HEADER.TIMEOUT]: `Second-${big}` },
body: '<lockinfo xmlns="DAV:"><lockscope><exclusive/></lockscope></lockinfo>'
@@ -261,7 +261,7 @@ describe(WebDAVProtocolGuard.name, () => {
const res = makeRes()
const ctx = makeCtx(req, res)
await expect(guard.canActivate(ctx)).resolves.toBe(true)
- expect(req.dav.lock.timeout).toBe(FilesLockManager.defaultLockTimeoutSeconds)
+ expect(req.dav.lock.timeout).toBe(CACHE_LOCK_DEFAULT_TIMEOUT)
})
it('invalid timeout -> NaN (no fallback), depth=infinity', async () => {
@@ -308,7 +308,7 @@ describe(WebDAVProtocolGuard.name, () => {
try {
await expect(guard.canActivate(ctx)).resolves.toBe(true)
- expect(req.dav.lock.timeout).toBe(FilesLockManager.defaultLockTimeoutSeconds)
+ expect(req.dav.lock.timeout).toBe(CACHE_LOCK_DEFAULT_TIMEOUT)
expect(req.dav.ifHeaders).toBeUndefined()
} finally {
spyIf.mockRestore()
diff --git a/backend/src/applications/webdav/guards/webdav-protocol.guard.ts b/backend/src/applications/webdav/guards/webdav-protocol.guard.ts
index 0914daca..a0fdd81b 100644
--- a/backend/src/applications/webdav/guards/webdav-protocol.guard.ts
+++ b/backend/src/applications/webdav/guards/webdav-protocol.guard.ts
@@ -10,7 +10,7 @@ import { FastifyReply } from 'fastify'
import { urlToPath } from '../../../common/functions'
import { decodeUrl } from '../../../common/shared'
import { HTTP_METHOD } from '../../applications.constants'
-import { FilesLockManager } from '../../files/services/files-lock-manager.service'
+import { CACHE_LOCK_DEFAULT_TIMEOUT } from '../../files/constants/cache'
import { USER_PERMISSION } from '../../users/constants/user'
import {
ALLOW_EMPTY_BODY_METHODS,
@@ -160,15 +160,15 @@ export class WebDAVProtocolGuard implements CanActivate {
// timeout: 'Infinite, Second-4100000000' | 'Second-4100000000' | 'Infinite'
const timeout = req.headers[HEADER.TIMEOUT] as string
if (timeout.toLowerCase() === 'infinite') {
- req.dav.lock.timeout = FilesLockManager.defaultLockTimeoutSeconds
+ req.dav.lock.timeout = CACHE_LOCK_DEFAULT_TIMEOUT
} else {
try {
const timeoutSplit = timeout.split('-')
const seconds = parseInt(timeoutSplit[timeoutSplit.length - 1], 10)
- req.dav.lock.timeout = seconds > FilesLockManager.defaultLockTimeoutSeconds ? FilesLockManager.defaultLockTimeoutSeconds : seconds
+ req.dav.lock.timeout = seconds > CACHE_LOCK_DEFAULT_TIMEOUT ? CACHE_LOCK_DEFAULT_TIMEOUT : seconds
} catch (e) {
this.logger.warn(`${this.lockMethod.name} - unable to set timeout, use the default value : ${e}`)
- req.dav.lock.timeout = FilesLockManager.defaultLockTimeoutSeconds
+ req.dav.lock.timeout = CACHE_LOCK_DEFAULT_TIMEOUT
}
}
}
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 82db7828..3cea21c0 100644
--- a/backend/src/applications/webdav/services/webdav-methods.service.spec.ts
+++ b/backend/src/applications/webdav/services/webdav-methods.service.spec.ts
@@ -267,7 +267,7 @@ describe(WebDAVMethods.name, () => {
const req = baseReq()
const res = makeRes()
- filesLockManager.create.mockImplementation(async (_user, _dbFile, _depth, _timeout, davLock) => {
+ filesLockManager.create.mockImplementation(async (_user, _dbFile, _depth, davLock, _timeout) => {
davLock.locktoken = 'opaquetoken:1'
return [
true,
@@ -301,7 +301,7 @@ describe(WebDAVMethods.name, () => {
const req = baseReq()
const res = makeRes()
- filesLockManager.create.mockImplementation(async (_user, _dbFile, _depth, _timeout, davLock) => {
+ filesLockManager.create.mockImplementation(async (_user, _dbFile, _depth, davLock, _timeout) => {
davLock.locktoken = 'opaquetoken:new'
return [
true,
diff --git a/backend/src/applications/webdav/services/webdav-methods.service.ts b/backend/src/applications/webdav/services/webdav-methods.service.ts
index bb9166ac..4743b73c 100644
--- a/backend/src/applications/webdav/services/webdav-methods.service.ts
+++ b/backend/src/applications/webdav/services/webdav-methods.service.ts
@@ -91,7 +91,7 @@ export class WebDAVMethods {
owner: req.dav.lock.owner
}
- const [ok, fileLock] = await this.filesLockManager.create(req.user, req.space.dbFile, req.dav.depth, req.dav.lock.timeout, davLock)
+ const [ok, fileLock] = await this.filesLockManager.create(req.user, req.space.dbFile, req.dav.depth, davLock, req.dav.lock.timeout)
if (ok) {
// Locking unmapped URLs: must create an empty resource (that is not a collection)
if (!rExists) {
diff --git a/frontend/src/app/applications/files/components/viewers/files-viewer-document.component.ts b/frontend/src/app/applications/files/components/viewers/files-viewer-document.component.ts
index cd8887ec..9ae61199 100644
--- a/frontend/src/app/applications/files/components/viewers/files-viewer-document.component.ts
+++ b/frontend/src/app/applications/files/components/viewers/files-viewer-document.component.ts
@@ -8,7 +8,8 @@ import { HttpClient, HttpParams } from '@angular/common/http'
import { Component, inject, input, Input, OnDestroy, OnInit } from '@angular/core'
import { FILE_MODE } from '@sync-in-server/backend/src/applications/files/constants/operations'
import { API_FILES_ONLY_OFFICE_SETTINGS } from '@sync-in-server/backend/src/applications/files/constants/routes'
-import { OnlyOfficeReqConfig } from '@sync-in-server/backend/src/applications/files/interfaces/only-office-config.interface'
+import type { FileLockProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
+import type { OnlyOfficeReqConfig } from '@sync-in-server/backend/src/applications/files/interfaces/only-office-config.interface'
import { SERVER_NAME } from '@sync-in-server/backend/src/common/shared'
import { LayoutService } from '../../../../layout/layout.service'
import { StoreService } from '../../../../store/store.service'
@@ -72,7 +73,7 @@ export class FilesViewerDocumentComponent implements OnInit, OnDestroy {
owner: `${SERVER_NAME} - ${this.store.user.getValue().fullName} (${this.store.user.getValue().email})`,
ownerLogin: this.store.user.getValue().login,
isExclusive: false
- }
+ } satisfies FileLockProps
}
data.config.editorConfig.lang = this.layout.getCurrentLanguage()
data.config.editorConfig.region = this.layout.getCurrentLanguage()
diff --git a/frontend/src/app/applications/files/models/file.model.ts b/frontend/src/app/applications/files/models/file.model.ts
index f2a859e9..2c579528 100644
--- a/frontend/src/app/applications/files/models/file.model.ts
+++ b/frontend/src/app/applications/files/models/file.model.ts
@@ -10,7 +10,7 @@ import {
API_FILES_OPERATION_THUMBNAIL,
API_FILES_TASK_OPERATION
} from '@sync-in-server/backend/src/applications/files/constants/routes'
-import type { FileProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
+import type { FileLockProps, FileProps } from '@sync-in-server/backend/src/applications/files/interfaces/file-props.interface'
import type { File } from '@sync-in-server/backend/src/applications/files/schemas/file.interface'
import { SPACE_OPERATION } from '@sync-in-server/backend/src/applications/spaces/constants/spaces'
import { popFromObject } from '@sync-in-server/backend/src/common/shared'
@@ -59,7 +59,7 @@ export class FileModel implements File {
enabled?: boolean
description?: string
}
- lock?: FileProps['lock']
+ lock?: FileLockProps
shares: { id: number; alias: string; name: string; type: number }[] = []
links: { id: number; alias: string; name: string; type: number }[] = []
spaces: { id: number; alias: string; name: string }[] = []