Skip to content

Commit

Permalink
Fix expire commands and background job when trash and/or version apps…
Browse files Browse the repository at this point in the history
… are disabled

Signed-off-by: Carl Schwan <[email protected]>
  • Loading branch information
CarlSchwan committed Jan 6, 2022
1 parent b9838fc commit bc21eee
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 42 deletions.
3 changes: 2 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ Note: Encrypting the contents of group folders is currently not supported.]]></d

<background-jobs>
<job>OCA\GroupFolders\BackgroundJob\ExpireGroupVersions</job>
<job>OCA\GroupFolders\BackgroundJob\ExpireGroupTrash</job>
</background-jobs>

<commands>
<command>OCA\GroupFolders\Command\ExpireGroupVersions</command>
<command>OCA\GroupFolders\Command\ExpireGroup\ExpireGroupBase</command>
<command>OCA\GroupFolders\Command\ListCommand</command>
<command>OCA\GroupFolders\Command\ACL</command>
<command>OCA\GroupFolders\Command\Quota</command>
Expand Down
63 changes: 51 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
use OCA\GroupFolders\ACL\RuleManager;
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager;
use OCA\GroupFolders\ACL\UserMapping\UserMappingManager;
use OCA\GroupFolders\BackgroundJob\ExpireGroupPlaceholder;
use OCA\GroupFolders\BackgroundJob\ExpireGroupTrash as ExpireGroupTrashJob;
use OCA\GroupFolders\BackgroundJob\ExpireGroupVersions as ExpireGroupVersionsJob;
use OCA\GroupFolders\CacheListener;
use OCA\GroupFolders\Command\ExpireGroupVersions;
use OCA\GroupFolders\Command\ExpireGroupVersionsPlaceholder;
use OCA\GroupFolders\Command\ExpireGroup\ExpireGroupBase;
use OCA\GroupFolders\Command\ExpireGroup\ExpireGroupVersionsTrash;
use OCA\GroupFolders\Command\ExpireGroup\ExpireGroupVersions;
use OCA\GroupFolders\Command\ExpireGroup\ExpireGroupTrash;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Helper\LazyFolder;
use OCA\GroupFolders\Listeners\LoadAdditionalScriptsListener;
Expand Down Expand Up @@ -84,16 +89,20 @@ public function register(IRegistrationContext $context): void {
);
});

$context->registerService(TrashBackend::class, function (IAppContainer $c) {
return new TrashBackend(
$context->registerService(TrashBackend::class, function (IAppContainer $c): TrashBackend {
$trashBackend = new TrashBackend(
$c->get(FolderManager::class),
$c->get(TrashManager::class),
$c->get('GroupAppFolder'),
$c->get(MountProvider::class),
$c->get(ACLManagerFactory::class),
$c->getServer()->getRootFolder(),
$c->get(VersionsBackend::class)
$c->getServer()->getRootFolder()
);
$hasVersionApp = interface_exists(\OCA\Files_Versions\Versions\IVersionBackend::class);
if ($hasVersionApp) {
$trashBackend->setVersionsBackend($c->get(VersionsBackend::class));
}
return $trashBackend;
});

$context->registerService(VersionsBackend::class, function (IAppContainer $c) {
Expand All @@ -105,27 +114,57 @@ public function register(IRegistrationContext $context): void {
);
});

$context->registerService(ExpireGroupVersions::class, function (IAppContainer $c) {
if (interface_exists('OCA\Files_Versions\Versions\IVersionBackend')) {
$context->registerService(ExpireGroupBase::class, function (IAppContainer $c): ExpireGroupBase {
// Multiple implementation of this class exists depending on if the trash and versions
// backends are enabled.

$hasVersionApp = interface_exists(\OCA\Files_Versions\Versions\IVersionBackend::class);
$hasTrashApp = interface_exists(\OCA\Files_Trashbin\Trash\ITrashBackend::class);

if ($hasVersionApp && $hasTrashApp) {
return new ExpireGroupVersionsTrash(
$c->get(GroupVersionsExpireManager::class),
$c->get(TrashBackend::class),
$c->get(Expiration::class)
);
}

if ($hasVersionApp) {
return new ExpireGroupVersions(
$c->get(GroupVersionsExpireManager::class),
);
}

if ($hasTrashApp) {
return new ExpireGroupTrash(
$c->get(TrashBackend::class),
$c->get(Expiration::class)
);
}
return new ExpireGroupVersionsPlaceholder();

return new ExpireGroupBase();
});

$context->registerService(\OCA\GroupFolders\BackgroundJob\ExpireGroupVersions::class, function (IAppContainer $c) {
if (interface_exists('OCA\Files_Versions\Versions\IVersionBackend')) {
return new \OCA\GroupFolders\BackgroundJob\ExpireGroupVersions(
if (interface_exists(\OCA\Files_Versions\Versions\IVersionBackend::class)) {
return new ExpireGroupVersionsJob(
$c->get(GroupVersionsExpireManager::class),
);
}

return new ExpireGroupPlaceholder();
});

$context->registerService(\OCA\GroupFolders\BackgroundJob\ExpireGroupTrash::class, function (IAppContainer $c) {
if (interface_exists(\OCA\Files_Trashbin\Trash\ITrashBackend::class)) {
return new ExpireGroupTrashJob(
$c->get(TrashBackend::class),
$c->get(Expiration::class),
$c->get(IConfig::class)
);
}
return new \OCA\GroupFolders\BackgroundJob\ExpireGroupVersionsPlaceholder();

return new ExpireGroupPlaceholder();
});

$context->registerService(ACLManagerFactory::class, function (IAppContainer $c) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
* @copyright Copyright (c) 2022 Carl Schwan <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -21,10 +22,10 @@

namespace OCA\GroupFolders\BackgroundJob;

class ExpireGroupVersionsPlaceholder extends \OC\BackgroundJob\TimedJob {
class ExpireGroupPlaceholder extends \OC\BackgroundJob\TimedJob {
public function __construct() {
// Run once per hour
$this->setInterval(60 * 60);
// Run at some point in a far far future :p
$this->setInterval(60 * 60 * 99999999);
}

protected function run($argument) {
Expand Down
60 changes: 60 additions & 0 deletions lib/BackgroundJob/ExpireGroupTrash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
* @copyright Copyright (c) 2021 Carl Schwan <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GroupFolders\BackgroundJob;

use OCA\GroupFolders\Trash\TrashBackend;
use OCA\Files_Trashbin\Expiration;
use OCP\IConfig;

class ExpireGroupTrash extends \OC\BackgroundJob\TimedJob {

/** @var TrashBackend */
private $trashBackend;

/** @var Expiration */
private $expiration;

/** @var IConfig */
private $config;

public function __construct(
TrashBackend $trashBackend,
Expiration $expiration,
IConfig $config
) {
// Run once per hour
$this->setInterval(60 * 60);

$this->trashBackend = $trashBackend;
$this->expiration = $expiration;
$this->config = $config;
}

protected function run($argument) {
$backgroundJob = $this->config->getAppValue('files_trashbin', 'background_job_expire_trash', 'yes');
if ($backgroundJob === 'no') {
return;
}
$this->trashBackend->expire($this->expiration);
}
}
47 changes: 47 additions & 0 deletions lib/Command/ExpireGroup/ExpireGroupBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GroupFolders\Command\ExpireGroup;

use OC\Core\Command\Base;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Base class for the group folder expiration commands.
*/
class ExpireGroupBase extends Base {
public function __construct() {
parent::__construct();
}

protected function configure() {
$this
->setName('groupfolders:expire')
->setDescription('Trigger expiration for files stored in group folders (trash and versions). Currently disabled.');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln('<error>groupfolder expiration handling is currently disabled because there is nothing to expire. Enable the "Delete Files" or/and "Versions" app to enable this feature.</error>');
return 0;
}
}
57 changes: 57 additions & 0 deletions lib/Command/ExpireGroup/ExpireGroupTrash.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Carl Schwan <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\GroupFolders\Command\ExpireGroup;

use OCA\Files_Trashbin\Expiration;
use OCA\GroupFolders\Trash\TrashBackend;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExpireGroupTrash extends ExpireGroupBase {
/** @var TrashBackend */
private $trashBackend;

/** @var Expiration */
private $expiration;

public function __construct(
TrashBackend $trashBackend,
Expiration $expiration
) {
parent::__construct();
$this->trashBackend = $trashBackend;
$this->expiration = $expiration;
}

protected function configure() {
$this
->setName('groupfolders:expire')
->setDescription('Trigger expiration of the trashbin for files stored in group folders');
parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output) {
[$count, $size] = $this->trashBackend->expire($this->expiration);
$output->writeln("<info>Removed $count expired trashbin items</info>");
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
* @copyright Copyright (c) 2021 Carl Schwan <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -19,42 +20,34 @@
*
*/

namespace OCA\GroupFolders\Command;
namespace OCA\GroupFolders\Command\ExpireGroup;

use OC\Core\Command\Base;
use OCA\Files_Trashbin\Expiration;
use OCA\Files_Versions\Versions\IVersion;
use OCA\GroupFolders\Trash\TrashBackend;
use OCA\GroupFolders\Versions\GroupVersionsExpireManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExpireGroupVersions extends Base {
/**
* Trigger expiry of versions for files stored in group folders.
*/
class ExpireGroupVersions extends ExpireGroupBase {
/** @var GroupVersionsExpireManager */
private $expireManager;

/** @var TrashBackend */
private $trashBackend;

/** @var Expiration */
private $expiration;
protected $expireManager;

public function __construct(
GroupVersionsExpireManager $expireManager,
TrashBackend $trashBackend,
Expiration $expiration
GroupVersionsExpireManager $expireManager
) {
parent::__construct();
$this->expireManager = $expireManager;
$this->trashBackend = $trashBackend;
$this->expiration = $expiration;
}

protected function configure() {
parent::configure();
$this
->setName('groupfolders:expire')
->setDescription('Trigger expiry of versions and trashbin for files stored in group folders');
parent::configure();
->setDescription('Trigger expiry of versions for files stored in group folders');
}

protected function execute(InputInterface $input, OutputInterface $output) {
Expand All @@ -71,10 +64,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln("<info>Cleaning up versions for no longer existing file with id $id</info>");
});


list($count, $size) = $this->trashBackend->expire($this->expiration);
$output->writeln("<info>Removed $count expired trashbin items</info>");

$this->expireManager->expireAll();
return 0;
}
Expand Down
Loading

0 comments on commit bc21eee

Please sign in to comment.