]> source.dussan.org Git - nextcloud-server.git/commitdiff
make the versions and encryption app aware of the copy operation
authorBjoern Schiessle <schiessle@owncloud.com>
Thu, 24 Jul 2014 13:30:00 +0000 (15:30 +0200)
committerBjoern Schiessle <schiessle@owncloud.com>
Wed, 30 Jul 2014 13:14:01 +0000 (15:14 +0200)
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/helper.php
apps/files_versions/appinfo/app.php
apps/files_versions/lib/hooks.php
apps/files_versions/lib/versions.php

index 2cde8144757cf72826770a1c88585e9f67b65fa9..667be8b98027524f12870d34433603d679df68e1 100644 (file)
@@ -412,18 +412,44 @@ class Hooks {
                                'uid' => $ownerOld,\r
                                'path' => $pathOld,\r
                                'type' => $type,\r
+                               'operation' => 'rename',\r
                                );\r
+\r
                }\r
        }\r
 \r
        /**\r
-        * after a file is renamed, rename its keyfile and share-keys also fix the file size and fix also the sharing\r
-        * @param array $params array with oldpath and newpath\r
+        * mark file as renamed so that we know the original source after the file was renamed\r
+        * @param array $params with the old path and the new path\r
+        */\r
+       public static function preCopy($params) {\r
+               $user = \OCP\User::getUser();\r
+               $view = new \OC\Files\View('/');\r
+               $util = new Util($view, $user);\r
+               list($ownerOld, $pathOld) = $util->getUidAndFilename($params['oldpath']);\r
+\r
+               // we only need to rename the keys if the rename happens on the same mountpoint\r
+               // otherwise we perform a stream copy, so we get a new set of keys\r
+               $mp1 = $view->getMountPoint('/' . $user . '/files/' . $params['oldpath']);\r
+               $mp2 = $view->getMountPoint('/' . $user . '/files/' . $params['newpath']);\r
+\r
+               $type = $view->is_dir('/' . $user . '/files/' . $params['oldpath']) ? 'folder' : 'file';\r
+\r
+               if ($mp1 === $mp2) {\r
+                       self::$renamedFiles[$params['oldpath']] = array(\r
+                               'uid' => $ownerOld,\r
+                               'path' => $pathOld,\r
+                               'type' => $type,\r
+                               'operation' => 'copy');\r
+               }\r
+       }\r
+\r
+       /**\r
+        * after a file is renamed/copied, rename/copy its keyfile and share-keys also fix the file size and fix also the sharing\r
         *\r
-        * This function is connected to the rename signal of OC_Filesystem and adjust the name and location\r
-        * of the stored versions along the actual file\r
+        * @param array $params array with oldpath and newpath\r
         */\r
-       public static function postRename($params) {\r
+       public static function postRenameOrCopy($params) {\r
 \r
                if (\OCP\App::isEnabled('files_encryption') === false) {\r
                        return true;\r
@@ -442,6 +468,7 @@ class Hooks {
                        $ownerOld = self::$renamedFiles[$params['oldpath']]['uid'];\r
                        $pathOld = self::$renamedFiles[$params['oldpath']]['path'];\r
                        $type =  self::$renamedFiles[$params['oldpath']]['type'];\r
+                       $operation = self::$renamedFiles[$params['oldpath']]['operation'];\r
                        unset(self::$renamedFiles[$params['oldpath']]);\r
                } else {\r
                        \OCP\Util::writeLog('Encryption library', "can't get path and owner from the file before it was renamed", \OCP\Util::DEBUG);\r
@@ -484,17 +511,17 @@ class Hooks {
                        $matches = Helper::findShareKeys($oldShareKeyPath, $view);\r
                        foreach ($matches as $src) {\r
                                $dst = \OC\Files\Filesystem::normalizePath(str_replace($pathOld, $pathNew, $src));\r
-                               $view->rename($src, $dst);\r
+                               $view->$operation($src, $dst);\r
                        }\r
 \r
                } else {\r
                        // handle share-keys folders\r
-                       $view->rename($oldShareKeyPath, $newShareKeyPath);\r
+                       $view->$operation($oldShareKeyPath, $newShareKeyPath);\r
                }\r
 \r
                // Rename keyfile so it isn't orphaned\r
                if ($view->file_exists($oldKeyfilePath)) {\r
-                       $view->rename($oldKeyfilePath, $newKeyfilePath);\r
+                       $view->$operation($oldKeyfilePath, $newKeyfilePath);\r
                }\r
 \r
 \r
index fed0788028faff159f71c9f6cb8337bdc45cf960..ed42cec326af743a2b92f723a217cb43ff79d5fb 100755 (executable)
@@ -62,7 +62,9 @@ class Helper {
        public static function registerFilesystemHooks() {
 
                \OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Encryption\Hooks', 'preRename');
-               \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Encryption\Hooks', 'postRename');
+               \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Encryption\Hooks', 'postRenameOrCopy');
+               \OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Encryption\Hooks', 'preCopy');
+               \OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Encryption\Hooks', 'postRenameOrCopy');
                \OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Encryption\Hooks', 'postDelete');
                \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Encryption\Hooks', 'preDelete');
                \OCP\Util::connectHook('OC_Filesystem', 'post_umount', 'OCA\Encryption\Hooks', 'postUmount');
index 371162cd16f1c649632eff37ff89c5029fe382c3..6020d401ef6b341842c1e8938b020addd52028cb 100644 (file)
@@ -14,3 +14,4 @@ OCP\Util::connectHook('OC_Filesystem', 'write', "OCA\Files_Versions\Hooks", "wri
 OCP\Util::connectHook('OC_Filesystem', 'post_delete', "OCA\Files_Versions\Hooks", "remove_hook");
 OCP\Util::connectHook('OC_Filesystem', 'delete', "OCA\Files_Versions\Hooks", "pre_remove_hook");
 OCP\Util::connectHook('OC_Filesystem', 'rename', "OCA\Files_Versions\Hooks", "rename_hook");
+OCP\Util::connectHook('OC_Filesystem', 'copy', "OCA\Files_Versions\Hooks", "copy_hook");
index 990f1403e8d1b2789f87b5e9144dfe19607d8967..17eacc6a6ed3042404c0c7325e4644fe177bcbd5 100644 (file)
@@ -69,7 +69,25 @@ class Hooks {
                        $oldpath = $params['oldpath'];
                        $newpath = $params['newpath'];
                        if($oldpath<>'' && $newpath<>'') {
-                               Storage::rename( $oldpath, $newpath );
+                               Storage::renameOrCopy($oldpath, $newpath, 'rename');
+                       }
+               }
+       }
+
+       /**
+        * copy versions of copied files
+        * @param array $params array with oldpath and newpath
+        *
+        * This function is connected to the copy signal of OC_Filesystem and copies the
+        * the stored versions to the new location
+        */
+       public static function copy_hook($params) {
+
+               if (\OCP\App::isEnabled('files_versions')) {
+                       $oldpath = $params['oldpath'];
+                       $newpath = $params['newpath'];
+                       if($oldpath<>'' && $newpath<>'') {
+                               Storage::renameOrCopy($oldpath, $newpath, 'copy');
                        }
                }
        }
index 2e048416c11c13e56e36d6a3867676994f778320..97926f5bbcb46fded6dcd3665225fa1febefc369 100644 (file)
@@ -174,9 +174,12 @@ class Storage {
        }
 
        /**
-        * rename versions of a file
+        * rename or copy versions of a file
+        * @param string $old_path
+        * @param string $new_path
+        * @param string $operation can be 'copy' or 'rename'
         */
-       public static function rename($old_path, $new_path) {
+       public static function renameOrCopy($old_path, $new_path, $operation) {
                list($uid, $oldpath) = self::getUidAndFilename($old_path);
                list($uidn, $newpath) = self::getUidAndFilename($new_path);
                $versions_view = new \OC\Files\View('/'.$uid .'/files_versions');
@@ -191,13 +194,13 @@ class Storage {
                self::expire($newpath);
 
                if ( $files_view->is_dir($oldpath) && $versions_view->is_dir($oldpath) ) {
-                       $versions_view->rename($oldpath, $newpath);
+                       $versions_view->$operation($oldpath, $newpath);
                } else  if ( ($versions = Storage::getVersions($uid, $oldpath)) ) {
                        // create missing dirs if necessary
                        self::createMissingDirectories($newpath, new \OC\Files\View('/'. $uidn));
 
                        foreach ($versions as $v) {
-                               $versions_view->rename($oldpath.'.v'.$v['version'], $newpath.'.v'.$v['version']);
+                               $versions_view->$operation($oldpath.'.v'.$v['version'], $newpath.'.v'.$v['version']);
                        }
                }
        }