3131use OCA \Text \Db \Session ;
3232use OCA \Text \Db \SessionMapper ;
3333use OCP \DirectEditing \IManager ;
34+ use OCP \Files \Config \IUserMountCache ;
3435use OCP \Files \Lock \ILock ;
3536use OCP \Files \Lock \ILockManager ;
3637use OCP \Files \Lock \LockContext ;
@@ -82,9 +83,11 @@ class DocumentService {
8283 private IRootFolder $ rootFolder ;
8384 private ICache $ cache ;
8485 private IAppData $ appData ;
86+ private ILockingProvider $ lockingProvider ;
8587 private ILockManager $ lockManager ;
88+ private $ userMountCache ;
8689
87- public function __construct (DocumentMapper $ documentMapper , StepMapper $ stepMapper , SessionMapper $ sessionMapper , IAppData $ appData , $ userId , IRootFolder $ rootFolder , ICacheFactory $ cacheFactory , ILogger $ logger , ShareManager $ shareManager , IRequest $ request , IManager $ directManager , ILockingProvider $ lockingProvider , ILockManager $ lockManager ) {
90+ public function __construct (DocumentMapper $ documentMapper , StepMapper $ stepMapper , SessionMapper $ sessionMapper , IAppData $ appData , $ userId , IRootFolder $ rootFolder , ICacheFactory $ cacheFactory , ILogger $ logger , ShareManager $ shareManager , IRequest $ request , IManager $ directManager , ILockingProvider $ lockingProvider , ILockManager $ lockManager, IUserMountCache $ userMountCache ) {
8891 $ this ->documentMapper = $ documentMapper ;
8992 $ this ->stepMapper = $ stepMapper ;
9093 $ this ->sessionMapper = $ sessionMapper ;
@@ -96,6 +99,7 @@ public function __construct(DocumentMapper $documentMapper, StepMapper $stepMapp
9699 $ this ->shareManager = $ shareManager ;
97100 $ this ->lockingProvider = $ lockingProvider ;
98101 $ this ->lockManager = $ lockManager ;
102+ $ this ->userMountCache = $ userMountCache ;
99103 $ token = $ request ->getParam ('token ' );
100104 if ($ this ->userId === null && $ token !== null ) {
101105 try {
@@ -384,8 +388,21 @@ public function getFileForSession(Session $session, $shareToken) {
384388 * @throws NotFoundException
385389 */
386390 public function getFileById ($ fileId , $ userId = null ): Node {
391+ $ userId = $ userId ?? $ this ->userId ;
392+
393+ // If no user is provided we need to get any file from existing mounts for cleanup jobs
394+ if ($ userId === null ) {
395+ $ mounts = $ this ->userMountCache ->getMountsForFileId ($ fileId );
396+ $ anyMount = array_shift ($ mounts );
397+ if ($ anyMount === null ) {
398+ throw new NotFoundException ('Could not fallback to file from mounts ' );
399+ }
400+
401+ $ userId = $ anyMount ->getUser ()->getUID ();
402+ }
403+
387404 try {
388- $ userFolder = $ this ->rootFolder ->getUserFolder ($ this -> userId ?? $ userId );
405+ $ userFolder = $ this ->rootFolder ->getUserFolder ($ userId );
389406 } catch (\OC \User \NoUserException $ e ) {
390407 // It is a bit hacky to depend on internal exceptions here. But it is the best we can do for now
391408 throw new NotFoundException ();
@@ -502,17 +519,16 @@ public function lock(int $fileId): bool {
502519 return true ;
503520 }
504521
505- $ file = $ this ->getFileById ($ fileId );
506522 try {
523+ $ file = $ this ->getFileById ($ fileId );
507524 $ this ->lockManager ->lock (new LockContext (
508525 $ file ,
509526 ILock::TYPE_APP ,
510527 Application::APP_NAME
511528 ));
512- } catch (NoLockProviderException $ e ) {
529+ } catch (NoLockProviderException | PreConditionNotMetException | NotFoundException $ e ) {
513530 } catch (OwnerLockedException $ e ) {
514531 return false ;
515- } catch (PreConditionNotMetException $ e ) {
516532 }
517533 return true ;
518534 }
@@ -522,15 +538,14 @@ public function unlock(int $fileId): void {
522538 return ;
523539 }
524540
525- $ file = $ this ->getFileById ($ fileId );
526541 try {
542+ $ file = $ this ->getFileById ($ fileId );
527543 $ this ->lockManager ->unlock (new LockContext (
528544 $ file ,
529545 ILock::TYPE_APP ,
530546 Application::APP_NAME
531547 ));
532- } catch (NoLockProviderException $ e ) {
533- } catch (PreConditionNotMetException $ e ) {
548+ } catch (NoLockProviderException | PreConditionNotMetException | NotFoundException $ e ) {
534549 }
535550 }
536551}
0 commit comments