]> source.dussan.org Git - nextcloud-server.git/commitdiff
some more clean-up, isVersioned() is no longer needed; rename hook fixed if a complet...
authorBjörn Schießle <schiessle@owncloud.com>
Fri, 11 Jan 2013 13:23:28 +0000 (14:23 +0100)
committerBjörn Schießle <schiessle@owncloud.com>
Fri, 11 Jan 2013 13:23:28 +0000 (14:23 +0100)
apps/files_versions/ajax/getVersions.php
apps/files_versions/ajax/rollbackVersion.php
apps/files_versions/history.php
apps/files_versions/lib/hooks.php
apps/files_versions/lib/versions.php

index 8476e5e8a51c54312954e5e357d8ca7906defcb0..600e69cf798054bca6f9fe63a140c0e2c0cb0eca 100644 (file)
@@ -4,10 +4,9 @@ OCP\JSON::checkAppEnabled('files_versions');
 $userDirectory = "/".OCP\USER::getUser()."/files";
 $source = $_GET['source'];
 
-if( OCA_Versions\Storage::isversioned( $source ) ) {
+$count = 5; //show the newest revisions
+if( ($versions = OCA_Versions\Storage::getVersions( $source, $count)) ) {
 
-       $count=5; //show the newest revisions
-       $versions = OCA_Versions\Storage::getVersions( $source, $count);
        $versionsFormatted = array();
 
        foreach ( $versions AS $version ) {
index f1b02eb4b928647fbc567fa543890a9a521f5901..f2c211d9c1ec944247c0469c5d1ca654e8331f13 100644 (file)
@@ -8,10 +8,9 @@ $userDirectory = "/".OCP\USER::getUser()."/files";
 $file = $_GET['file'];
 $revision=(int)$_GET['revision'];
 
-if( OCA_Versions\Storage::isversioned( $file ) ) {
-       if(OCA_Versions\Storage::rollback( $file, $revision )) {
-               OCP\JSON::success(array("data" => array( "revision" => $revision, "file" => $file )));
-       }else{
-               OCP\JSON::error(array("data" => array( "message" => "Could not revert:" . $file )));
-       }
+if(OCA_Versions\Storage::rollback( $file, $revision )) {
+       OCP\JSON::success(array("data" => array( "revision" => $revision, "file" => $file )));
+}else{
+       OCP\JSON::error(array("data" => array( "message" => "Could not revert:" . $file )));
 }
+
index d4c278ebd85f0692b362c362bd820c0d484c76ab..6e27f43d576aebdcccf3c63e120176986a5ac868 100644 (file)
@@ -52,10 +52,8 @@ if ( isset( $_GET['path'] ) ) {
        }
 
        // show the history only if there is something to show
-       if( OCA_Versions\Storage::isversioned( $path ) ) {
-
-               $count = 999; //show the newest revisions
-               $versions = OCA_Versions\Storage::getVersions( $path, $count);
+       $count = 999; //show the newest revisions
+       if( ($versions = OCA_Versions\Storage::getVersions( $path, $count)) ) {
 
                $tmpl->assign( 'versions', array_reverse( $versions ) );
 
index 736d9cd86876e5fcc5a21f2390dd287f791cd270..5fb9dc3c3c550631fd669d7d9557782cde012394 100644 (file)
@@ -58,18 +58,16 @@ class Hooks {
         * of the stored versions along the actual file
         */
        public static function rename_hook($params) {
-               $versions_fileview = \OCP\Files::getStorage('files_versions');
-               $rel_oldpath =  $params['oldpath'];
-               $abs_oldpath = \OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath('').$rel_oldpath.'.v';
-               $abs_newpath = \OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath('').$params['newpath'].'.v';
-               if(Storage::isversioned($rel_oldpath)) {
-                       $info=pathinfo($abs_newpath);
-                       if(!file_exists($info['dirname'])) mkdir($info['dirname'], 0750, true);
-                       $versions = Storage::getVersions($rel_oldpath);
-                       foreach ($versions as $v) {
-                               rename($abs_oldpath.$v['version'], $abs_newpath.$v['version']);
-                       }
+               if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {\r
+               \r
+                       $versions = new Storage( new \OC_FilesystemView('') );\r
+               \r
+                       $oldpath = $params['oldpath'];
+                       $newpath = $params['newpath'];\r
+               \r
+                       if($oldpath<>'' && $newpath<>'') $versions->rename( $oldpath, $newpath );\r
+               \r
                }
        }
-
+       
 }
index 7b5f4755b2e3c353efcf58c2a887a0ee523c5876..23aa1319ff0b27a2496ac01e4d4e33d21ded8779 100644 (file)
@@ -19,7 +19,8 @@ class Storage {
        const DEFAULTENABLED=true;
        const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota
        
-       private static $max_versions_per_interval = array(1 => array('intervalEndsAfter' => 10,     //first 10sec, one version every 2sec
+       private static $max_versions_per_interval = array(
+                                                                                                               1 => array('intervalEndsAfter' => 10,     //first 10sec, one version every 2sec
                                                                                                                                        'step' => 2),
                                                                                                                2 => array('intervalEndsAfter' => 60,     //next minute, one version every 10sec\r
                                                                                                                                        'step' => 10),
@@ -106,8 +107,7 @@ class Storage {
                $versions_fileview = new \OC_FilesystemView('/'.$uid .'/files_versions');
                \r
                $abs_path = \OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath('').$filename.'.v';\r
-               if(Storage::isversioned($filename)) {\r
-                       $versions = self::getVersions($filename);\r
+               if( ($versions = self::getVersions($filename)) ) {\r
                        if (  ($versionsSize = \OCP\Config::getAppValue('files_versions', 'size')) === null ) {\r
                                $versionsSize = self::calculateSize($uid);\r
                        }\r
@@ -119,6 +119,30 @@ class Storage {
                }
        }
        
+       /**\r
+        * Delete versions of a file\r
+        */\r
+       public static function rename($oldpath, $newpath) {
+               error_log("oldpath: $oldpath");
+               error_log("newpath: $newpath");\r
+               list($uid, $oldpath) = self::getUidAndFilename($oldpath);
+               list($uidn, $newpath) = self::getUidAndFilename($newpath);\r
+               $versions_view = new \OC_FilesystemView('/'.$uid .'/files_versions');
+               $files_view = new \OC_FilesystemView('/'.$uid .'/files');\r
+               \r
+               if ( $files_view->is_dir($oldpath) && $versions_view->is_dir($oldpath) ) {
+                       $versions_view->rename($oldpath, $newpath);
+               } else  if ( ($versions = Storage::getVersions($oldpath)) ) {\r
+                       $info=pathinfo($abs_newpath);\r
+                       if(!file_exists($info['dirname'])) mkdir($info['dirname'], 0750, true);\r
+                       $versions = Storage::getVersions($oldpath);\r
+                       foreach ($versions as $v) {
+                               error_log("rename(".$oldpath.'.v'.$v['version'].", ". $newpath.'.v'.$v['version'].")");
+                               $versions_view->rename($oldpath.'.v'.$v['version'], $newpath.'.v'.$v['version']);\r
+                       }\r
+               }\r
+       }
+       
        /**
         * rollback to an old version of a file.
         */
@@ -129,9 +153,10 @@ class Storage {
                        $users_view = new \OC_FilesystemView('/'.$uid);
 
                        //first create a new version
-                       if ( !$users_view->file_exists('files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename))) {
-                               $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
+                       $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
+                       if ( !$users_view->file_exists($version)) {
                                $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
+                               $versionCreated = true;
                        }
                        
                        // rollback
@@ -140,39 +165,14 @@ class Storage {
                                Storage::expire($filename);
                                return true;
 
-                       }else{
-                               if (isset($version) ) {
-                                       $users_view->unlink($version);
-                                       return false;
-                               }
+                       }else if ( $versionCreated ) {
+                               $users_view->unlink($version);
                        }
                }
+               return false;
 
        }
 
-       /**
-        * check if old versions of a file exist.
-        */
-       public static function isversioned($filename) {
-               if(\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED)=='true') {
-                       list($uid, $filename) = self::getUidAndFilename($filename);
-                       $versions_fileview = new \OC_FilesystemView('/'.$uid.'/files_versions');
-
-                       $versionsName=\OCP\Config::getSystemValue('datadirectory').$versions_fileview->getAbsolutePath($filename);
-
-                       // check for old versions
-                       $matches=glob($versionsName.'.v*');
-                       if(count($matches)>0) {
-                               return true;
-                       }else{
-                               return false;
-                       }
-               }else{
-                       return(false);
-               }
-       }
-
-
 
        /**
         * @brief get a list of all available versions of a file in descending chronological order
@@ -391,7 +391,7 @@ class Storage {
                                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
+                                                       //distance between two version too small, delete version
                                                        $versions_fileview->unlink($versions[$i]['path'].'.v'.$versions[$i]['version']);
                                                        $availableSpace += $versions[$i]['size'];
                                                        $versionsSize -= $versions[$i]['size'];