From 739fc17e73188bb383fa3ae239cf961ce39960a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Fri, 11 Mar 2022 08:32:58 +0100 Subject: [PATCH] Allow infinite lock through configuration and make it the default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- README.md | 3 ++- lib/Command/Lock.php | 15 +++++++++++++-- lib/Model/FileLock.php | 5 +++++ lib/Service/ConfigService.php | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 70eab09d..93d8b08f 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,11 @@ By default, files locked using this app will be unlocked after 30 minutes. ### Settings -Administrators can change the time of the maximum lock time (30) using the command: +Administrators can change the time of the maximum lock time in minutes (30) using the command: `./occ config:app:set --value '30' files_lock lock_timeout` +Locks have no expiry by default. ### More commands diff --git a/lib/Command/Lock.php b/lib/Command/Lock.php index 624717ff..7b6efd51 100644 --- a/lib/Command/Lock.php +++ b/lib/Command/Lock.php @@ -38,6 +38,7 @@ use OCA\FilesLock\Exceptions\NotFileException; use OCA\FilesLock\Exceptions\SuccessException; use OCA\FilesLock\Exceptions\UnauthorizedUnlockException; +use OCA\FilesLock\Model\FileLock; use OCA\FilesLock\Service\ConfigService; use OCA\FilesLock\Service\FileService; use OCA\FilesLock\Service\LockService; @@ -122,7 +123,7 @@ protected function configure() { * @throws NotFileException * @throws InvalidPathException */ - protected function execute(InputInterface $input, OutputInterface $output) { + protected function execute(InputInterface $input, OutputInterface $output): int { $fileId = (int)$input->getArgument('file_id'); $userId = $input->getArgument('user_id'); @@ -136,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $this->getStatus($input, $output, $fileId); $this->unlockFile($input, $output, $fileId); } catch (SuccessException $e) { - return; + return 0; } if ($userId === '') { @@ -144,6 +145,8 @@ protected function execute(InputInterface $input, OutputInterface $output) { } $this->lockFile($input, $output, $fileId, $userId); + + return 0; } @@ -164,6 +167,14 @@ private function getStatus(InputInterface $input, OutputInterface $output, int $ $output->writeln( 'File #' . $fileId . ' is locked by ' . $lock->getUserId() ); + $output->writeln( + ' - Locked at: ' . date('c', $lock->getCreation()) + ); + if ($lock->getETA() !== FileLock::ETA_INFINITE) { + $output->writeln( + ' - Expiry in seconds: ' . $lock->getETA() + ); + } } catch (LockNotFoundException $e) { $output->writeln('File #' . $fileId . ' is not locked'); } diff --git a/lib/Model/FileLock.php b/lib/Model/FileLock.php index 1e223528..232f5646 100644 --- a/lib/Model/FileLock.php +++ b/lib/Model/FileLock.php @@ -45,6 +45,8 @@ class FileLock implements IQueryRow, JsonSerializable { use TArrayTools; + public const ETA_INFINITE = -1; + /** @var int */ private $id = 0; @@ -196,6 +198,9 @@ public function setTimeout(int $timeout): self { * @return int */ public function getETA(): int { + if ($this->getTimeout() <= 0) { + return self::ETA_INFINITE; + } $end = $this->getCreation() + $this->getTimeout(); $eta = $end - time(); diff --git a/lib/Service/ConfigService.php b/lib/Service/ConfigService.php index 271bf1b8..27f7646a 100644 --- a/lib/Service/ConfigService.php +++ b/lib/Service/ConfigService.php @@ -41,7 +41,7 @@ class ConfigService { const LOCK_TIMEOUT = 'lock_timeout'; public $defaults = [ - self::LOCK_TIMEOUT => '30' + self::LOCK_TIMEOUT => '0' ]; /** @var string */