Skip to content

Commit

Permalink
Allow infinite lock through configuration and make it the default
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Mar 25, 2022
1 parent ac1fe33 commit 739fc17
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions lib/Command/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand All @@ -136,14 +137,16 @@ 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 === '') {
throw new InvalidArgumentException('\'Not enough arguments (missing: "user_id")');
}

$this->lockFile($input, $output, $fileId, $userId);

return 0;
}


Expand All @@ -164,6 +167,14 @@ private function getStatus(InputInterface $input, OutputInterface $output, int $
$output->writeln(
'File #' . $fileId . ' is <comment>locked</comment> 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 <info>not locked<info>');
}
Expand Down
5 changes: 5 additions & 0 deletions lib/Model/FileLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class FileLock implements IQueryRow, JsonSerializable {

use TArrayTools;

public const ETA_INFINITE = -1;


/** @var int */
private $id = 0;
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ConfigService {
const LOCK_TIMEOUT = 'lock_timeout';

public $defaults = [
self::LOCK_TIMEOUT => '30'
self::LOCK_TIMEOUT => '0'
];

/** @var string */
Expand Down

0 comments on commit 739fc17

Please sign in to comment.