|
10 | 10 | namespace OCA\FilesLock\Service; |
11 | 11 |
|
12 | 12 | use Exception; |
13 | | -use OCA\Files_Sharing\External\Storage as ExternalStorage; |
| 13 | +use OC\Files\Storage\DAV; |
| 14 | +use OCA\FilesLock\AppInfo\Application; |
14 | 15 | use OCA\FilesLock\Db\LocksRequest; |
15 | 16 | use OCA\FilesLock\Exceptions\LockNotFoundException; |
16 | 17 | use OCA\FilesLock\Exceptions\UnauthorizedUnlockException; |
|
25 | 26 | use OCP\Files\Lock\LockContext; |
26 | 27 | use OCP\Files\Lock\OwnerLockedException; |
27 | 28 | use OCP\Files\NotFoundException; |
28 | | -use OCP\Http\Client\IClientService; |
29 | 29 | use OCP\IL10N; |
30 | 30 | use OCP\IRequest; |
31 | 31 | use OCP\IUserManager; |
@@ -68,7 +68,6 @@ public function __construct( |
68 | 68 | IUserSession $userSession, |
69 | 69 | IRequest $request, |
70 | 70 | LoggerInterface $logger, |
71 | | - private IClientService $clientService, |
72 | 71 | private IRootFolder $rootFolder, |
73 | 72 | ) { |
74 | 73 | $this->l10n = $l10n; |
@@ -96,8 +95,8 @@ public function getLockForNodeId(int $nodeId) { |
96 | 95 | try { |
97 | 96 | $this->lockCache[$nodeId] = $this->getLockFromFileId($nodeId); |
98 | 97 | } catch (LockNotFoundException) { |
99 | | - $remoteLock = $this->getRemoteLockForFileId($nodeId); |
100 | | - $this->lockCache[$nodeId] = $remoteLock ?? false; |
| 98 | + $remoteLock = $this->getRemoteLockFromDav($nodeId); |
| 99 | + $this->lockCache[$nodeId] = $remoteLock ?: false; |
101 | 100 | } |
102 | 101 |
|
103 | 102 | return $this->lockCache[$nodeId]; |
@@ -168,13 +167,7 @@ public function lock(LockContext $lockScope): FileLock { |
168 | 167 |
|
169 | 168 | $this->injectMetadata($known); |
170 | 169 | throw new OwnerLockedException($known); |
171 | | - } catch (LockNotFoundException) { |
172 | | - $remoteLock = $this->getRemoteLockForFileId($lockScope->getNode()->getId()); |
173 | | - if ($remoteLock !== null) { |
174 | | - $this->injectMetadata($remoteLock); |
175 | | - throw new OwnerLockedException($remoteLock); |
176 | | - } |
177 | | - |
| 170 | + } catch (LockNotFoundException $e) { |
178 | 171 | $lock = FileLock::fromLockScope($lockScope, $this->configService->getTimeoutSeconds()); |
179 | 172 | $this->generateToken($lock); |
180 | 173 | $lock->setCreation(time()); |
@@ -353,9 +346,6 @@ public function injectMetadata(FileLock $lock): FileLock { |
353 | 346 | } |
354 | 347 |
|
355 | 348 | if ($displayName) { |
356 | | - if ($lock->getRemoteHost()) { |
357 | | - $displayName .= '@' . $lock->getRemoteHost(); |
358 | | - } |
359 | 349 | $lock->setDisplayName($displayName); |
360 | 350 | } |
361 | 351 | return $lock; |
@@ -408,87 +398,60 @@ function (FileLock $lock) { |
408 | 398 | $this->locksRequest->removeIds($ids); |
409 | 399 | } |
410 | 400 |
|
411 | | - private function propagateEtag(LockContext $lockContext): void { |
412 | | - $node = $lockContext->getNode(); |
413 | | - $node->getStorage()->getCache()->update($node->getId(), [ |
414 | | - 'etag' => uniqid(), |
415 | | - ]); |
416 | | - $node->getStorage()->getUpdater()->propagate($node->getInternalPath(), $node->getMTime()); |
417 | | - } |
418 | | - |
419 | | - public function getRemoteLockForFileId(int $fileId): ?FileLock { |
| 401 | + public function getRemoteLockFromDav(int $nodeId): ?FileLock { |
420 | 402 | try { |
421 | 403 | $user = $this->userSession->getUser(); |
422 | 404 | if (!$user) { |
423 | 405 | return null; |
424 | 406 | } |
425 | 407 |
|
426 | 408 | $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
427 | | - $nodes = $userFolder->getById($fileId); |
| 409 | + $nodes = $userFolder->getById($nodeId); |
428 | 410 | if (empty($nodes)) { |
429 | 411 | return null; |
430 | 412 | } |
431 | 413 |
|
432 | 414 | $node = $nodes[0]; |
433 | 415 | $storage = $node->getStorage(); |
434 | | - if (!$storage->instanceOfStorage(ExternalStorage::class)) { |
| 416 | + |
| 417 | + if (!$storage->instanceOfStorage(DAV::class)) { |
435 | 418 | return null; |
436 | 419 | } |
437 | 420 |
|
438 | | - $internalPath = $node->getInternalPath(); |
| 421 | + $path = $node->getInternalPath(); |
| 422 | + $storage->getMetaData($path); |
439 | 423 |
|
440 | | - /** @var ExternalStorage $storage */ |
441 | | - $remoteLockData = $this->getRemoteLock( |
442 | | - $storage->getRemote(), |
443 | | - $storage->getToken(), |
444 | | - $internalPath |
445 | | - ); |
446 | | - |
447 | | - if (!$remoteLockData) { |
| 424 | + $isLocked = $storage->getPropfindPropertyValue($path, Application::DAV_PROPERTY_LOCK); |
| 425 | + if (!$isLocked) { |
448 | 426 | return null; |
449 | 427 | } |
450 | 428 |
|
451 | 429 | $fileLock = new FileLock(); |
452 | | - $fileLock->import($remoteLockData); |
| 430 | + $fileLock->import([ |
| 431 | + 'fileId' => $nodeId, |
| 432 | + 'owner' => (string)($storage->getPropfindPropertyValue($path, Application::DAV_PROPERTY_LOCK_OWNER_DISPLAYNAME) ?? ''), |
| 433 | + 'type' => (int)($storage->getPropfindPropertyValue($path, Application::DAV_PROPERTY_LOCK_OWNER_TYPE) ?? 0), |
| 434 | + 'creation' => (int)($storage->getPropfindPropertyValue($path, Application::DAV_PROPERTY_LOCK_TIME) ?? 0), |
| 435 | + 'ttl' => (int)($storage->getPropfindPropertyValue($path, Application::DAV_PROPERTY_LOCK_TIMEOUT) ?? 0), |
| 436 | + 'token' => (string)($storage->getPropfindPropertyValue($path, Application::DAV_PROPERTY_LOCK_TOKEN) ?? ''), |
| 437 | + ]); |
453 | 438 |
|
454 | 439 | $remoteHost = parse_url($storage->getRemote(), PHP_URL_HOST); |
455 | 440 | $fileLock->setRemoteHost($remoteHost); |
456 | | - |
457 | | - $this->injectMetadata($fileLock); |
458 | | - |
459 | | - $this->lockCache[$fileId] = $fileLock; |
| 441 | + $fileLock->setDisplayName($fileLock->getDisplayName() . '@' . $remoteHost); |
460 | 442 |
|
461 | 443 | return $fileLock; |
462 | 444 | } catch (\Exception $e) { |
463 | | - $this->logger->debug('Failed to get remote lock: ' . $e->getMessage()); |
| 445 | + $this->logger->debug('Failed to get remote lock from DAV: ' . $e->getMessage()); |
464 | 446 | return null; |
465 | 447 | } |
466 | 448 | } |
467 | 449 |
|
468 | | - public function getRemoteLock(string $remoteUrl, string $shareToken, string $path = ''): ?array { |
469 | | - $url = rtrim($remoteUrl, '/') . "/ocs/v2.php/apps/files_lock/lock/token/{$shareToken}"; |
470 | | - |
471 | | - if ($path !== '') { |
472 | | - $url .= '?path=' . urlencode($path); |
473 | | - } |
474 | | - |
475 | | - try { |
476 | | - $response = $this->clientService->newClient()->get($url, [ |
477 | | - 'headers' => [ |
478 | | - 'OCS-APIRequest' => 'true', |
479 | | - 'Accept' => 'application/json' |
480 | | - ], |
481 | | - 'timeout' => 5, |
482 | | - ]); |
483 | | - |
484 | | - $data = json_decode($response->getBody(), true); |
485 | | - if (!isset($data['ocs']['data']['locked']) || !$data['ocs']['data']['locked']) { |
486 | | - return null; |
487 | | - } |
488 | | - |
489 | | - return $data['ocs']['data']['lock']; |
490 | | - } catch (\Exception) { |
491 | | - return null; |
492 | | - } |
| 450 | + private function propagateEtag(LockContext $lockContext): void { |
| 451 | + $node = $lockContext->getNode(); |
| 452 | + $node->getStorage()->getCache()->update($node->getId(), [ |
| 453 | + 'etag' => uniqid(), |
| 454 | + ]); |
| 455 | + $node->getStorage()->getUpdater()->propagate($node->getInternalPath(), $node->getMTime()); |
493 | 456 | } |
494 | 457 | } |
0 commit comments