+++ /dev/null
-<?php
-
-/**
- * ownCloud - user_migrate
- *
- * @author Sam Tuke
- * @copyright 2012 Sam Tuke samtuke@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library 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 library. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-// TODO: Allow admins to expire versions of any user
-// TODO: Provide feedback as to how many versions were deleted
-
-// Check user and app status
-OCP\JSON::checkLoggedIn();
-OCP\App::checkAppEnabled('files_versions');
-OCP\JSON::callCheck();
-
-$versions = new OCA_Versions\Storage();
-
-if( $versions->expireAll() ) {
-
- OCP\JSON::success();
- die();
-
-} else {
-
- OCP\JSON::error();
- die();
-
-}
\ No newline at end of file
const DEFAULTMAXFILESIZE=10485760; // 10MB
const DEFAULTMAXVERSIONS=50;
- var $MAX_VERSIONS_PER_INTERVAL = array(1 => array('intervalEndsAfter' => 3600, //first hour, one version every 10sec
- 'step' => 10),
- 2 => array('intervalEndsAfter' => 86400, //next 24h, one version every hour
- 'step' => 3600),
- 3 => array('intervalEndsAfter' => -1, //until the end one version per day\r
- 'step' => 86400),
+ private static $max_versions_per_interval = array(1 => array('intervalEndsAfter' => 3600, //first hour, one version every 10sec
+ 'step' => 10),
+ 2 => array('intervalEndsAfter' => 86400, //next 24h, one version every hour
+ 'step' => 3600),
+ 3 => array('intervalEndsAfter' => -1, //until the end one version per day\r
+ 'step' => 86400),
);
private static function getUidAndFilename($filename)
$matches=glob($versionsName.'.v*');
sort($matches);
$parts=explode('.v', end($matches));
- if((end($parts)+Storage::$MAX_VERSIONS_PER_INTERVAL[1]['step'])>time()) {
+ if((end($parts)+Storage::$max_versions_per_interval[1]['step'])>time()) {
return false;
}
}
*/
public static function expire($filename) {
if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
- list($uid, $filename) = self::getUidAndFilename($filename);
+ list($uid, $filename) = self::getUidAndFilename($filename);
$versions_fileview = new \OC_FilesystemView('/'.$uid.'/files_versions');
// get available disk space for user
$numOfVersions = count($versions);
$interval = 1;
- $step = Storage::$MAX_VERSIONS_PER_INTERVAL[$interval]['step'];
- if (Storage::$MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'] == -1) {
+ $step = Storage::$max_versions_per_interval[$interval]['step'];
+ if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] == -1) {
$nextInterval = -1;
} else {
- $nextInterval = $time - Storage::$MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'];
+ $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
}
- $nextVersion = $versions[0]['versions'] - $step;
+ $nextVersion = $versions[0]['version'] - $step;
for ($i=1; $i<$numOfVersions; $i++) {
if ( $nextInterval == -1 || $versions[$i]['version'] >= $nextInterval ) {
if ( $versions[$i]['version'] > $nextVersion ) {
- //distance between two version to small, delete version (TODO)
+ //distance between two version to small, delete version
+ error_log("next version:" . $nextVersion . " - version: " . $versions[$i]['version']);
+ error_log("unlink: " . $filename.'.v'.$versions[$i]['version']);
+ $versions_fileview->unlink($filename.'.v'.$versions[$i]['version']);
$availableSpace += $versions[$i]['size'];
} else {
$nextVersion = $versions[$i]['version'] - $step;
}
} else { // time to move on to the next interval
$interval++;
- $step = Storage::$MAX_VERSIONS_PER_INTERVAL[$interval]['step'];
+ $step = Storage::$max_versions_per_interval[$interval]['step'];
$nextVersion = $version[$i]['version'] - $step;
- if ( Storage::$MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'] == -1 ) {
+ if ( Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] == -1 ) {
$nextInterval = -1;
} else {\r
- $nextInterval = $time - Storage::$MAX_VERSIONS_PER_INTERVAL[$interval]['intervalEndsAfter'];
+ $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
}
}
}
+
+ // check if enough space is available after versions are rearranged
+ $versions = array_reverse( $versions ); // oldest version first
+ $numOfVersions = count($versions);
+ $i = 0;
+ while ($availableSpace < 0) {
+ $versions_fileview->unlink($filename.'.v'.$versions[$i]['version']);
+ $availableSpace += $versions[$i]['size'];
+ $i++;
+ if ($i = $numOfVersions-2) break; // keep at least the last version
+ }
}
}
- /**
- * @brief Erase all old versions of all user files
- * @return true/false
- */
- public function expireAll() {
- $view = \OCP\Files::getStorage('files_versions');
- return $view->deleteAll('', true);
- }
-}