2015-08-31 16:52:00 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2024-06-02 09:26:54 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-08-31 16:52:00 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files_Versions\BackgroundJob;
|
|
|
|
|
|
|
|
|
|
use OCA\Files_Versions\Expiration;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files_Versions\Storage;
|
2022-06-28 06:55:26 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
|
use OCP\BackgroundJob\TimedJob;
|
2021-10-06 05:54:27 -04:00
|
|
|
use OCP\IConfig;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserManager;
|
2015-08-31 16:52:00 -04:00
|
|
|
|
2022-06-28 06:55:26 -04:00
|
|
|
class ExpireVersions extends TimedJob {
|
2020-04-10 10:54:27 -04:00
|
|
|
public const ITEMS_PER_SESSION = 1000;
|
2015-08-31 16:52:00 -04:00
|
|
|
|
2022-06-28 06:55:26 -04:00
|
|
|
private IConfig $config;
|
|
|
|
|
private Expiration $expiration;
|
|
|
|
|
private IUserManager $userManager;
|
2021-10-06 05:54:27 -04:00
|
|
|
|
2022-06-28 06:55:26 -04:00
|
|
|
public function __construct(IConfig $config, IUserManager $userManager, Expiration $expiration, ITimeFactory $time) {
|
|
|
|
|
parent::__construct($time);
|
2015-08-31 16:52:00 -04:00
|
|
|
// Run once per 30 minutes
|
|
|
|
|
$this->setInterval(60 * 30);
|
|
|
|
|
|
2021-10-06 05:54:27 -04:00
|
|
|
$this->config = $config;
|
2018-05-11 16:25:07 -04:00
|
|
|
$this->expiration = $expiration;
|
|
|
|
|
$this->userManager = $userManager;
|
2015-08-31 16:52:00 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-06 05:54:27 -04:00
|
|
|
public function run($argument) {
|
|
|
|
|
$backgroundJob = $this->config->getAppValue('files_versions', 'background_job_expire_versions', 'yes');
|
|
|
|
|
if ($backgroundJob === 'no') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:52:00 -04:00
|
|
|
$maxAge = $this->expiration->getMaxAgeAsTimestamp();
|
|
|
|
|
if (!$maxAge) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 07:53:40 -04:00
|
|
|
$this->userManager->callForSeenUsers(function (IUser $user) {
|
2015-08-31 16:52:00 -04:00
|
|
|
$uid = $user->getUID();
|
2016-10-19 04:03:29 -04:00
|
|
|
if (!$this->setupFS($uid)) {
|
2016-04-20 04:36:15 -04:00
|
|
|
return;
|
2015-08-31 16:52:00 -04:00
|
|
|
}
|
2015-09-16 10:22:17 -04:00
|
|
|
Storage::expireOlderThanMaxForUser($uid);
|
2016-04-20 04:36:15 -04:00
|
|
|
});
|
2015-08-31 16:52:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Act on behalf on trash item owner
|
|
|
|
|
*/
|
2022-06-28 06:55:26 -04:00
|
|
|
protected function setupFS(string $user): bool {
|
2015-08-31 16:52:00 -04:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
|
\OC_Util::setupFS($user);
|
|
|
|
|
|
2016-04-20 04:36:15 -04:00
|
|
|
// Check if this user has a versions directory
|
|
|
|
|
$view = new \OC\Files\View('/' . $user);
|
|
|
|
|
if (!$view->is_dir('/files_versions')) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:52:00 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-15 14:11:52 -04:00
|
|
|
}
|