@@ -8,6 +8,7 @@ import { HttpException, Injectable, Logger } from '@nestjs/common'
88import fs from 'node:fs/promises'
99import path from 'node:path'
1010import { configuration } from '../../../configuration/config.environment'
11+ import { FileDBProps } from '../../files/interfaces/file-db-props.interface'
1112import { FileLock } from '../../files/interfaces/file-lock.interface'
1213import { FileProps } from '../../files/interfaces/file-props.interface'
1314import { FilesLockManager } from '../../files/services/files-lock-manager.service'
@@ -17,7 +18,6 @@ import { dirName, fileName, getProps } from '../../files/utils/files'
1718import { SharesQueries } from '../../shares/services/shares-queries.service'
1819import { USER_PERMISSION } from '../../users/constants/user'
1920import { UserModel } from '../../users/models/user.model'
20- import { LOCK_SCOPE } from '../../webdav/constants/webdav'
2121import { SpaceFiles } from '../interfaces/space-files.interface'
2222import { SpaceEnv } from '../models/space-env.model'
2323import { IsRealPathIsDirAndExists , realPathFromRootFile } from '../utils/paths'
@@ -56,7 +56,8 @@ export class SpacesBrowser {
5656 this . parseRootFiles ( user , space , {
5757 withShares : options . withSpacesAndShares ,
5858 withHasComments : options . withHasComments ,
59- withSyncs : options . withSyncs
59+ withSyncs : options . withSyncs ,
60+ withLocks : options . withLocks
6061 } )
6162 ] )
6263 this . updateDBFiles ( user , space , dbFiles , fsFiles , options )
@@ -83,6 +84,7 @@ export class SpacesBrowser {
8384 withShares ?: boolean
8485 withHasComments ?: boolean
8586 withSyncs ?: boolean
87+ withLocks ?: boolean
8688 }
8789 ) : Promise < FileProps [ ] > {
8890 if ( space . inFilesRepository && space . id && ! space . root . alias ) {
@@ -155,9 +157,12 @@ export class SpacesBrowser {
155157 }
156158 }
157159
158- private async updateRootFile ( f : FileProps , options : { withShares ?: boolean ; withHasComments ?: boolean ; withSyncs ?: boolean } ) : Promise < FileProps > {
159- // get realpath
160+ private async updateRootFile (
161+ f : FileProps ,
162+ options : { withShares ?: boolean ; withHasComments ?: boolean ; withSyncs ?: boolean ; withLocks ?: boolean }
163+ ) : Promise < FileProps > {
160164 const realPath = realPathFromRootFile ( f )
165+ const originalPath = f . path
161166 f . path = f . root . name
162167 try {
163168 const fileProps : FileProps = await getProps ( realPath , f . path )
@@ -170,8 +175,28 @@ export class SpacesBrowser {
170175 if ( options . withSyncs ) {
171176 fileProps . syncs = f . syncs
172177 }
178+ if ( options . withLocks && ( f . origin || f . root ?. owner ) ) {
179+ // `f.origin` is used for shares
180+ // `f.root.owner` is used for anchored files in spaces
181+ // all other files are handled in the `enrichWithLocks` function
182+ const dbFile : FileDBProps = {
183+ ...( f . origin ?. spaceId
184+ ? { spaceId : f . origin . spaceId , ...( f . origin . spaceExternalRootId ? { spaceExternalRootId : f . origin . spaceExternalRootId } : { } ) }
185+ : f . origin ?. shareExternalId
186+ ? { shareExternalId : f . origin . shareExternalId }
187+ : { ownerId : f . origin ?. ownerId ?? f . root . owner . id } ) ,
188+ path : originalPath ,
189+ inTrash : f . inTrash
190+ }
191+ const locks = await this . filesLockManager . getLocksByPath ( dbFile )
192+ if ( locks . length > 0 ) {
193+ fileProps . lock = this . filesLockManager . convertLockToFileLockProps ( locks [ 0 ] )
194+ }
195+ }
196+ // `owner.id` is only used in the `withLocks` condition
197+ delete f . root . owner ?. id
198+ // check `f.id`; it can be null for external roots
173199 if ( f . id ) {
174- // f.id is null for external roots
175200 // todo: check if a db file referenced under external roots have an id and correctly parsed here
176201 this . filesQueries . compareAndUpdateFileProps ( f , fileProps ) . catch ( ( e : Error ) => this . logger . error ( `${ this . updateRootFile . name } - ${ e } ` ) )
177202 fileProps . id = f . id
@@ -299,13 +324,8 @@ export class SpacesBrowser {
299324 }
300325 const locks : Record < string , FileLock > = await this . filesLockManager . browseParentChildLocks ( space . dbFile , false )
301326 if ( ! Object . keys ( locks ) . length ) return
302- for ( const f of files . filter ( ( f ) => ( f . root && f . root . alias in locks ) || ( ! f . root && f . name in locks ) ) ) {
303- const lock : FileLock = f . root ? locks [ f . root . alias ] : locks [ f . name ]
304- f . lock = {
305- owner : lock ?. davLock ?. owner || `${ lock . owner . fullName } (${ lock . owner . email } )` ,
306- ownerLogin : lock . owner . login ,
307- isExclusive : lock ?. davLock ?. lockscope ? lock ?. davLock ?. lockscope === LOCK_SCOPE . EXCLUSIVE : true
308- }
327+ for ( const f of files . filter ( ( f ) => ! f . root && ! f . origin && f . name in locks ) ) {
328+ f . lock = this . filesLockManager . convertLockToFileLockProps ( locks [ f . name ] )
309329 }
310330 }
311331}
0 commit comments