Skip to content

Commit c49a0e9

Browse files
committed
Let locked files open in read only
Signed-off-by: Julius Härtl <[email protected]>
1 parent b7e89cf commit c49a0e9

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

lib/Service/ApiService.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ public function create($fileId = null, $filePath = null, $token = null, $guestNa
114114
$content = null;
115115
}
116116

117-
$this->documentService->lock($fileId);
117+
$isLocked = $this->documentService->lock($fileId);
118+
if (!$isLocked) {
119+
$readOnly = true;
120+
}
118121

119122
return new DataResponse([
120123
'document' => $document,

lib/Service/DocumentService.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
use OCP\Files\Lock\ILock;
3535
use OCP\Files\Lock\ILockManager;
3636
use OCP\Files\Lock\LockScope;
37+
use OCP\Files\Lock\NoLockProviderException;
38+
use OCP\Files\Lock\OwnerLockedException;
3739
use OCP\IRequest;
3840
use OCP\Lock\ILockingProvider;
41+
use OCP\PreConditionNotMetException;
3942
use function json_encode;
4043
use OC\Files\Node\File;
4144
use OCA\Text\Db\Document;
@@ -477,29 +480,40 @@ private function ensureDocumentsFolder(): bool {
477480
return true;
478481
}
479482

480-
public function lock(int $fileId) {
483+
public function lock(int $fileId): bool {
481484
if (!$this->lockManager->isLockProviderAvailable()) {
482-
return;
485+
return true;
483486
}
484487

485488
$file = $this->getFileById($fileId);
486-
$this->lockManager->lock(new LockScope(
487-
$file,
488-
ILock::TYPE_APP,
489-
Application::APP_NAME
490-
));
489+
try {
490+
$this->lockManager->lock(new LockScope(
491+
$file,
492+
ILock::TYPE_APP,
493+
Application::APP_NAME
494+
));
495+
} catch (NoLockProviderException $e) {
496+
} catch (OwnerLockedException $e) {
497+
return false;
498+
} catch (PreConditionNotMetException $e) {
499+
}
500+
return true;
491501
}
492502

493-
public function unlock(int $fileId) {
503+
public function unlock(int $fileId): void {
494504
if (!$this->lockManager->isLockProviderAvailable()) {
495505
return;
496506
}
497507

498508
$file = $this->getFileById($fileId);
499-
$this->lockManager->unlock(new LockScope(
500-
$file,
501-
ILock::TYPE_APP,
502-
Application::APP_NAME
503-
));
509+
try {
510+
$this->lockManager->unlock(new LockScope(
511+
$file,
512+
ILock::TYPE_APP,
513+
Application::APP_NAME
514+
));
515+
} catch (NoLockProviderException $e) {
516+
} catch (PreConditionNotMetException $e) {
517+
}
504518
}
505519
}

0 commit comments

Comments
 (0)