Add expiration for versions older than max

This commit is contained in:
Victor Dubiniuk 2015-08-31 23:52:00 +03:00
parent 230029e509
commit 0cefbd76b9
6 changed files with 180 additions and 1 deletions

View File

@ -19,6 +19,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
OCP\Util::addStyle('files_versions', 'versions');
namespace OCA\Files_Versions\AppInfo;
$app = new Application();
$container = $app->getContainer();
\OCP\Util::addStyle('files_versions', 'versions');
\OCA\Files_Versions\Hooks::connectHooks();

View File

@ -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');

View File

@ -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');

View File

@ -0,0 +1,106 @@
<?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\IDBConnection;
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 IDBConnection
*/
private $dbConnection;
/**
* @var IUserManager
*/
private $userManager;
public function __construct(IDBConnection $dbConnection = null, IUserManager $userManager = null, Expiration $expiration = null) {
// Run once per 30 minutes
$this->setInterval(60 * 30);
if (is_null($expiration) || is_null($userManager) || is_null($dbConnection)) {
$this->fixDIForJobs();
} else {
$this->dbConnection = $dbConnection;
$this->expiration = $expiration;
$this->userManager = $userManager;
}
}
protected function fixDIForJobs() {
$application = new Application();
$this->dbConnection = \OC::$server->getDatabaseConnection();
$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::expireOlderThan($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;
}
}

View File

@ -112,6 +112,19 @@ class Expiration {
return $isOlderThanMax || $isMinReached;
}
/**
* 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

View File

@ -403,6 +403,34 @@ class Storage {
return $versions;
}
public static function expireOlderThan($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