diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-09-18 11:11:47 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-09-18 11:11:47 +0200 |
commit | 8fa4ebaf6dcede56968d4ae387f3542385ca2f0d (patch) | |
tree | e4aa6aaa0305e63da2472b0458377d6eea3f01aa /apps/files_versions | |
parent | e37ee9a814f11b9e85be67b68ed3257d2e3239fb (diff) | |
parent | e3c067c2e10dc1faccf798e4c4d95e52cd85ff60 (diff) | |
download | nextcloud-server-8fa4ebaf6dcede56968d4ae387f3542385ca2f0d.tar.gz nextcloud-server-8fa4ebaf6dcede56968d4ae387f3542385ca2f0d.zip |
Merge pull request #18724 from owncloud/expiration-cronjob
Versions expiration cronjob
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/appinfo/app.php | 3 | ||||
-rw-r--r-- | apps/files_versions/appinfo/install.php | 23 | ||||
-rw-r--r-- | apps/files_versions/appinfo/update.php | 3 | ||||
-rw-r--r-- | apps/files_versions/lib/backgroundjob/expireversions.php | 98 | ||||
-rw-r--r-- | apps/files_versions/lib/expiration.php | 13 | ||||
-rw-r--r-- | apps/files_versions/lib/storage.php | 32 |
6 files changed, 171 insertions, 1 deletions
diff --git a/apps/files_versions/appinfo/app.php b/apps/files_versions/appinfo/app.php index 967f2e73a34..eadc7c69a23 100644 --- a/apps/files_versions/appinfo/app.php +++ b/apps/files_versions/appinfo/app.php @@ -19,6 +19,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/> * */ -OCP\Util::addStyle('files_versions', 'versions'); + +\OCP\Util::addStyle('files_versions', 'versions'); \OCA\Files_Versions\Hooks::connectHooks(); diff --git a/apps/files_versions/appinfo/install.php b/apps/files_versions/appinfo/install.php new file mode 100644 index 00000000000..d72ba2f56e5 --- /dev/null +++ b/apps/files_versions/appinfo/install.php @@ -0,0 +1,23 @@ +<?php +/** + * @author Victor Dubiniuk <dubiniuk@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +// Cron job for deleting expired trash items +\OC::$server->getJobList()->add('OCA\Files_Versions\BackgroundJob\ExpireVersions'); diff --git a/apps/files_versions/appinfo/update.php b/apps/files_versions/appinfo/update.php index 524f9bd153f..687e5d3b5d4 100644 --- a/apps/files_versions/appinfo/update.php +++ b/apps/files_versions/appinfo/update.php @@ -24,3 +24,6 @@ $installedVersion=OCP\Config::getAppValue('files_versions', 'installed_version') if (version_compare($installedVersion, '1.0.4', '<')) { \OC_DB::dropTable("files_versions"); } + +// Cron job for deleting expired trash items +\OC::$server->getJobList()->add('OCA\Files_Versions\BackgroundJob\ExpireVersions'); diff --git a/apps/files_versions/lib/backgroundjob/expireversions.php b/apps/files_versions/lib/backgroundjob/expireversions.php new file mode 100644 index 00000000000..afdd5eed57a --- /dev/null +++ b/apps/files_versions/lib/backgroundjob/expireversions.php @@ -0,0 +1,98 @@ +<?php +/** + * @author Victor Dubiniuk <dubiniuk@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OCA\Files_Versions\BackgroundJob; + +use OCP\IUserManager; +use OCA\Files_Versions\AppInfo\Application; +use OCA\Files_Versions\Storage; +use OCA\Files_Versions\Expiration; + +class ExpireVersions extends \OC\BackgroundJob\TimedJob { + + const ITEMS_PER_SESSION = 1000; + + /** + * @var Expiration + */ + private $expiration; + + /** + * @var IUserManager + */ + private $userManager; + + public function __construct(IUserManager $userManager = null, Expiration $expiration = null) { + // Run once per 30 minutes + $this->setInterval(60 * 30); + + if (is_null($expiration) || is_null($userManager)) { + $this->fixDIForJobs(); + } else { + $this->expiration = $expiration; + $this->userManager = $userManager; + } + } + + protected function fixDIForJobs() { + $application = new Application(); + $this->expiration = $application->getContainer()->query('Expiration'); + $this->userManager = \OC::$server->getUserManager(); + } + + protected function run($argument) { + $maxAge = $this->expiration->getMaxAgeAsTimestamp(); + if (!$maxAge) { + return; + } + + $users = $this->userManager->search(''); + $isFSready = false; + foreach ($users as $user) { + $uid = $user->getUID(); + if (!$isFSready) { + if (!$this->setupFS($uid)) { + continue; + } + $isFSready = true; + } + Storage::expireOlderThanMaxForUser($uid); + } + + \OC_Util::tearDownFS(); + } + + /** + * Act on behalf on trash item owner + * @param string $user + * @return boolean + */ + private function setupFS($user){ + if (!$this->userManager->userExists($user)) { + return false; + } + + \OC_Util::tearDownFS(); + \OC_Util::setupFS($user); + + return true; + } +} diff --git a/apps/files_versions/lib/expiration.php b/apps/files_versions/lib/expiration.php index d42c62f0ee3..fba705251e9 100644 --- a/apps/files_versions/lib/expiration.php +++ b/apps/files_versions/lib/expiration.php @@ -113,6 +113,19 @@ class Expiration { } /** + * Get maximal retention obligation as a timestamp + * @return int + */ + public function getMaxAgeAsTimestamp(){ + $maxAge = false; + if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) { + $time = $this->timeFactory->getTime(); + $maxAge = $time - ($this->maxAge * 86400); + } + return $maxAge; + } + + /** * Read versions_retention_obligation, validate it * and set private members accordingly */ diff --git a/apps/files_versions/lib/storage.php b/apps/files_versions/lib/storage.php index ba2b78ff4d2..6aa58c55e9b 100644 --- a/apps/files_versions/lib/storage.php +++ b/apps/files_versions/lib/storage.php @@ -404,6 +404,38 @@ class Storage { } /** + * Expire versions that older than max version retention time + * @param string $uid + */ + public static function expireOlderThanMaxForUser($uid){ + $expiration = self::getExpiration(); + $threshold = $expiration->getMaxAgeAsTimestamp(); + $versions = self::getAllVersions($uid); + if (!$threshold || !array_key_exists('all', $versions)) { + return; + } + + $toDelete = []; + foreach (array_reverse($versions['all']) as $key => $version) { + if (intval($version['version'])<$threshold) { + $toDelete[$key] = $version; + } else { + //Versions are sorted by time - nothing mo to iterate. + break; + } + } + + $view = new \OC\Files\View('/' . $uid . '/files_versions'); + if (!empty($toDelete)) { + foreach ($toDelete as $version) { + \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'])); + self::deleteVersion($view, $version['path'] . '.v' . $version['version']); + \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'])); + } + } + } + + /** * translate a timestamp into a string like "5 days ago" * @param int $timestamp * @return string for example "5 days ago" |