]> source.dussan.org Git - nextcloud-server.git/commitdiff
added filesystem post rename hook
authorFlorin Peter <github@florin-peter.de>
Sat, 27 Apr 2013 18:18:05 +0000 (20:18 +0200)
committerFlorin Peter <github@florin-peter.de>
Sat, 27 Apr 2013 18:18:05 +0000 (20:18 +0200)
apps/files_encryption/appinfo/app.php
apps/files_encryption/hooks/hooks.php

index c2de9d0b441ec01ad9952dff36a768b9f24360cc..9ae6c8331f85a9a11ae76cda25f9916eed609f33 100644 (file)
@@ -23,6 +23,9 @@ OCP\Util::connectHook( 'OCP\Share', 'post_unshareAll', 'OCA\Encryption\Hooks', '
 // Webdav-related hooks
 OCP\Util::connectHook( 'OC_Webdav_Properties', 'update', 'OCA\Encryption\Hooks', 'updateKeyfileFromClient' );
 
+// filesystem hooks
+OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Encryption\Hooks', 'postRename');
+
 stream_wrapper_register( 'crypt', 'OCA\Encryption\Stream' );
 
 $view = new OC_FilesystemView( '/' );
index 2ac74ad6c44c5b9187a129bd1027109e4603a55b..82aae2272aab99a888bddde125660f5649e7793f 100644 (file)
@@ -310,5 +310,69 @@ class Hooks {
                // we may not need to implement it\r
                \r
        }\r
-       \r
+\r
+\r
+    /**\r
+     * @brief after a file is renamed, rename its keyfile and share-keys also fix the file size and fix also the sharing\r
+     * @param array with oldpath and newpath\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
+     */\r
+    public static function postRename($params) {\r
+        // Disable encryption proxy to prevent recursive calls\r
+        $proxyStatus = \OC_FileProxy::$enabled;\r
+        \OC_FileProxy::$enabled = false;\r
+\r
+        $view = new \OC_FilesystemView('/');\r
+        $session = new Session($view);\r
+        $userId = \OCP\User::getUser();\r
+        $util = new Util( $view, $userId );\r
+\r
+        // Format paths to be relative to user files dir\r
+        $oldKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'keyfiles' . '/' . $params['oldpath'];\r
+        $newKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'keyfiles' . '/' . $params['newpath'];\r
+\r
+        // add key ext if this is not an folder\r
+        if (!$view->is_dir($oldKeyfilePath)) {\r
+            $oldKeyfilePath .= '.key';\r
+            $newKeyfilePath .= '.key';\r
+\r
+            // handle share-keys\r
+            $localKeyPath = $view->getLocalFile($userId.'/files_encryption/share-keys/'.$params['oldpath']);\r
+            $matches = glob(preg_quote($localKeyPath).'*.shareKey');\r
+            foreach ($matches as $src) {\r
+                $dst = str_replace($params['oldpath'], $params['newpath'], $src);\r
+                rename($src, $dst);\r
+            }\r
+\r
+        } else {\r
+            // handle share-keys folders\r
+            $oldShareKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'share-keys' . '/' . $params['oldpath'];\r
+            $newShareKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'share-keys' . '/' . $params['newpath'];\r
+            $view->rename($oldShareKeyfilePath, $newShareKeyfilePath);\r
+        }\r
+\r
+        // Rename keyfile so it isn't orphaned\r
+        if($view->file_exists($oldKeyfilePath)) {\r
+            $view->rename($oldKeyfilePath, $newKeyfilePath);\r
+        }\r
+\r
+        // build the path to the file\r
+        $newPath = '/' . $userId . '/files' .$params['newpath'];\r
+        $newPathRelative = $params['newpath'];\r
+\r
+        if($util->fixFileSize($newPath)) {\r
+            // get sharing app state\r
+            $sharingEnabled = \OCP\Share::isEnabled();\r
+\r
+            // get users\r
+            $usersSharing = $util->getSharingUsersArray($sharingEnabled, $newPathRelative);\r
+\r
+            // update sharing-keys\r
+            $util->setSharedFileKeyfiles($session, $usersSharing, $newPathRelative);\r
+        }\r
+\r
+        \OC_FileProxy::$enabled = $proxyStatus;\r
+    }\r
 }\r