Skip to content

Commit

Permalink
Let locked files open in read only
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Mar 24, 2022
1 parent b7e89cf commit c49a0e9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
5 changes: 4 additions & 1 deletion lib/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ public function create($fileId = null, $filePath = null, $token = null, $guestNa
$content = null;
}

$this->documentService->lock($fileId);
$isLocked = $this->documentService->lock($fileId);
if (!$isLocked) {
$readOnly = true;
}

return new DataResponse([
'document' => $document,
Expand Down
40 changes: 27 additions & 13 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
use OCP\Files\Lock\ILock;
use OCP\Files\Lock\ILockManager;
use OCP\Files\Lock\LockScope;
use OCP\Files\Lock\NoLockProviderException;
use OCP\Files\Lock\OwnerLockedException;
use OCP\IRequest;
use OCP\Lock\ILockingProvider;
use OCP\PreConditionNotMetException;
use function json_encode;
use OC\Files\Node\File;
use OCA\Text\Db\Document;
Expand Down Expand Up @@ -477,29 +480,40 @@ private function ensureDocumentsFolder(): bool {
return true;
}

public function lock(int $fileId) {
public function lock(int $fileId): bool {
if (!$this->lockManager->isLockProviderAvailable()) {
return;
return true;
}

$file = $this->getFileById($fileId);
$this->lockManager->lock(new LockScope(
$file,
ILock::TYPE_APP,
Application::APP_NAME
));
try {
$this->lockManager->lock(new LockScope(
$file,
ILock::TYPE_APP,
Application::APP_NAME
));
} catch (NoLockProviderException $e) {
} catch (OwnerLockedException $e) {
return false;
} catch (PreConditionNotMetException $e) {
}
return true;
}

public function unlock(int $fileId) {
public function unlock(int $fileId): void {
if (!$this->lockManager->isLockProviderAvailable()) {
return;
}

$file = $this->getFileById($fileId);
$this->lockManager->unlock(new LockScope(
$file,
ILock::TYPE_APP,
Application::APP_NAME
));
try {
$this->lockManager->unlock(new LockScope(
$file,
ILock::TYPE_APP,
Application::APP_NAME
));
} catch (NoLockProviderException $e) {
} catch (PreConditionNotMetException $e) {
}
}
}

0 comments on commit c49a0e9

Please sign in to comment.