made initial testing version of expireAll for version control \ngave some old vars new camelcase names

This commit is contained in:
Sam Tuke 2012-05-16 18:30:26 +01:00
parent fefa4412c4
commit 9acd1065b0
5 changed files with 98 additions and 32 deletions

View File

@ -13,7 +13,7 @@ $source = strip_tags( $source );
if( OCA_Versions\Storage::isversioned( $source ) ) { if( OCA_Versions\Storage::isversioned( $source ) ) {
$count=5; //show the newest revisions $count=5; //show the newest revisions
$versions = OCA_Versions\Storage::getversions( $source, $count); $versions = OCA_Versions\Storage::getVersions( $source, $count);
$versionsFormatted = array(); $versionsFormatted = array();
foreach ( $versions AS $version ) { foreach ( $versions AS $version ) {

View File

@ -55,7 +55,7 @@ if ( isset( $_GET['path'] ) ) {
if( OCA_Versions\Storage::isversioned( $path ) ) { if( OCA_Versions\Storage::isversioned( $path ) ) {
$count = 999; //show the newest revisions $count = 999; //show the newest revisions
$versions = OCA_Versions\Storage::getversions( $path, $count); $versions = OCA_Versions\Storage::getVersions( $path, $count);
$tmpl->assign( 'versions', array_reverse( $versions ) ); $tmpl->assign( 'versions', array_reverse( $versions ) );

View File

@ -25,7 +25,7 @@ $(document).ready(function(){
function createVersionsDropdown(filename, files) { function createVersionsDropdown(filename, files) {
var historyUrl = '../apps/files_versions/history.php?path='+encodeURIComponent($('#dir').val()).replace(/%2F/g, '/')+'/'+encodeURIComponent(filename); var historyUrl = OC.linkTo('files_versions', 'history.php?path='+encodeURIComponent( $( '#dir' ).val() ).replace( /%2F/g, '/' )+'/'+encodeURIComponent( filename ) )
var html = '<div id="dropdown" class="drop" data-file="'+files+'">'; var html = '<div id="dropdown" class="drop" data-file="'+files+'">';
html += '<div id="private">'; html += '<div id="private">';

View File

@ -27,7 +27,6 @@ class Storage {
// //
// todo: // todo:
// - port to oc_filesystem to enable network transparency // - port to oc_filesystem to enable network transparency
// - check if it works well together with encryption
// - implement expire all function. And find a place to call it ;-) // - implement expire all function. And find a place to call it ;-)
// - add transparent compression. first test if it´s worth it. // - add transparent compression. first test if it´s worth it.
@ -69,7 +68,7 @@ class Storage {
*/ */
public static function store($filename) { public static function store($filename) {
if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
$versionsfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER); $versionsFolderName=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
$filesfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/files'; $filesfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/files';
Storage::init(); Storage::init();
@ -95,7 +94,7 @@ class Storage {
// check mininterval // check mininterval
$matches=glob($versionsfoldername.$filename.'.v*'); $matches=glob($versionsFolderName.$filename.'.v*');
sort($matches); sort($matches);
$parts=explode('.v',end($matches)); $parts=explode('.v',end($matches));
if((end($parts)+Storage::DEFAULTMININTERVAL)>time()){ if((end($parts)+Storage::DEFAULTMININTERVAL)>time()){
@ -105,13 +104,13 @@ class Storage {
// create all parent folders // create all parent folders
$info=pathinfo($filename); $info=pathinfo($filename);
@mkdir($versionsfoldername.$info['dirname'],0700,true); @mkdir($versionsFolderName.$info['dirname'], 0700, true);
// store a new version of a file // store a new version of a file
copy($filesfoldername.$filename,$versionsfoldername.$filename.'.v'.time()); copy($filesfoldername.$filename,$versionsFolderName.$filename.'.v'.time());
// expire old revisions // expire old revisions if necessary
Storage::expire($filename); Storage::expire($filename);
} }
} }
@ -124,12 +123,12 @@ class Storage {
if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
$versionsfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER); $versionsFolderName=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
$filesfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/files'; $filesfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/files';
// rollback // rollback
if ( @copy($versionsfoldername.$filename.'.v'.$revision,$filesfoldername.$filename) ) { if ( @copy($versionsFolderName.$filename.'.v'.$revision,$filesfoldername.$filename) ) {
return true; return true;
@ -148,10 +147,10 @@ class Storage {
*/ */
public static function isversioned($filename) { public static function isversioned($filename) {
if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
$versionsfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER); $versionsFolderName=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
// check for old versions // check for old versions
$matches=glob($versionsfoldername.$filename.'.v*'); $matches=glob($versionsFolderName.$filename.'.v*');
if(count($matches)>1){ if(count($matches)>1){
return true; return true;
}else{ }else{
@ -170,15 +169,15 @@ class Storage {
* @param $count number of versions to return * @param $count number of versions to return
* @returns array * @returns array
*/ */
public static function getversions( $filename, $count = 0 ) { public static function getVersions( $filename, $count = 0 ) {
if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) { if( \OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true' ) {
$versionsfoldername = \OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER); $versionsFolderName = \OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
$versions = array(); $versions = array();
// fetch for old versions // fetch for old versions
$matches = glob( $versionsfoldername.$filename.'.v*' ); $matches = glob( $versionsFolderName.$filename.'.v*' );
sort( $matches ); sort( $matches );
@ -193,7 +192,7 @@ class Storage {
// if file with modified date exists, flag it in array as currently enabled version // if file with modified date exists, flag it in array as currently enabled version
$curFile['fileName'] = basename( $parts[0] ); $curFile['fileName'] = basename( $parts[0] );
$curFile['filePath'] = \OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/files/'.$curFile['fileName']; $curFile['filePath'] = \OCP\Config::getSystemValue('datadirectory').\OC_Filesystem::getRoot().'/'.$curFile['fileName'];
( \md5_file( $ma ) == \md5_file( $curFile['filePath'] ) ? $versions[$i]['fileMatch'] = 1 : $versions[$i]['fileMatch'] = 0 ); ( \md5_file( $ma ) == \md5_file( $curFile['filePath'] ) ? $versions[$i]['fileMatch'] = 1 : $versions[$i]['fileMatch'] = 0 );
@ -231,6 +230,7 @@ class Storage {
return( array() ); return( array() );
} }
} }
@ -241,28 +241,92 @@ class Storage {
public static function expire($filename) { public static function expire($filename) {
if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') { if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
$versionsfoldername=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER); $versionsFolderName=\OCP\Config::getSystemValue('datadirectory').'/'. \OCP\USER::getUser() .'/'.\OCP\Config::getSystemValue('files_versionsfolder', Storage::DEFAULTFOLDER);
// check for old versions // check for old versions
$matches=glob($versionsfoldername.$filename.'.v*'); $matches = glob( $versionsFolderName.$filename.'.v*' );
if( count( $matches ) > \OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) ) { if( count( $matches ) > \OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) ) {
$numbertodelete=count($matches-\OCP\Config::getSystemValue('files_versionmaxversions', Storage::DEFAULTMAXVERSIONS));
$numberToDelete = count( $matches-\OCP\Config::getSystemValue( 'files_versionmaxversions', Storage::DEFAULTMAXVERSIONS ) );
// delete old versions of a file // delete old versions of a file
$deleteitems=array_slice($matches,0,$numbertodelete); $deleteItems = array_slice( $matches, 0, $numberToDelete );
foreach($deleteitems as $de){
unlink($versionsfoldername.$filename.'.v'.$de); foreach( $deleteItems as $de ) {
unlink( $versionsFolderName.$filename.'.v'.$de );
} }
} }
} }
} }
/** /**
* expire all old versions. * @brief erase all old versions of all user files
* @return
*/ */
public static function expireall($filename) { public static function expireAll() {
// todo this should go through all the versions directories and delete all the not needed files and not needed directories.
// useful to be included in a cleanup cronjob. function deleteAll($directory, $empty = false) {
if(substr($directory,-1) == "/") {
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory)) {
return false;
} elseif(!is_readable($directory)) {
return false;
} else {
$directoryHandle = opendir($directory);
while ($contents = readdir($directoryHandle)) {
if( $contents != '.' && $contents != '..') {
$path = $directory . "/" . $contents;
if( is_dir($path) ) {
deleteAll($path);
} else {
unlink($path);
}
}
}
closedir( $directoryHandle );
if( $empty == false ) {
if(!rmdir($directory)) {
return false;
}
}
return true;
}
}
// FIXME: make this path dynamic
$dir = '/home/samtuke/owncloud/git/oc5/data/admin/versions';
( deleteAll( $dir, 1 ) ? return true : return false );
} }

View File

@ -290,8 +290,10 @@ class OC_Filesystem{
} }
/** /**
* get the fake root * @brief get the relative path of the root data directory for the current user
* @return string * @return string
*
* Returns path like /admin/files
*/ */
static public function getRoot(){ static public function getRoot(){
return self::$defaultInstance->getRoot(); return self::$defaultInstance->getRoot();