]> source.dussan.org Git - nextcloud-server.git/commitdiff
implement postRename
authorFlorin Peter <github@florin-peter.de>
Thu, 25 Apr 2013 20:49:47 +0000 (22:49 +0200)
committerFlorin Peter <github@florin-peter.de>
Thu, 25 Apr 2013 20:49:47 +0000 (22:49 +0200)
@samtuke no need anymore for fixPartialFilePath this is now handled by rename
share-keys are now handled properly
webdav .part files are handled properly

apps/files_encryption/lib/keymanager.php
apps/files_encryption/lib/proxy.php
apps/files_encryption/lib/stream.php

index ceefe8887ac8600c9eaf39c5a4451a22f4e8b5a6..cfc13ee132d65cdf7ab9bcd479542f13d03e10cc 100755 (executable)
@@ -139,28 +139,6 @@ class Keymanager {
                
        }
        
-       /**
-        * @brief Remove .path extension from a file path
-        * @param string $path Path that may identify a .part file
-        * @return string File path without .part extension
-        */
-       public static function fixPartialFilePath( $path ) {
-       
-               if ( preg_match( '/\.part$/', $path ) ) {
-               
-                       $newLength = strlen( $path ) - 5;
-                       $fPath = substr( $path, 0, $newLength );
-                       
-                       return $fPath;
-                       
-               } else {
-               
-                       return $path;
-                       
-               }
-       
-       }
-       
        /**
         * @brief retrieve keyfile for an encrypted file
         * @param \OC_FilesystemView $view
index 66ea282312e15da5661add3c25cb49dc3e0dcb7b..7294c243664772f2c60ba92162a2e2ff4ad4788d 100644 (file)
@@ -237,8 +237,6 @@ class Proxy extends \OC_FileProxy {
                     return true;
                }
                
-               $path = Keymanager::fixPartialFilePath( $path );
-       
                // Disable encryption proxy to prevent recursive calls
         $proxyStatus = \OC_FileProxy::$enabled;
         \OC_FileProxy::$enabled = false;
@@ -307,6 +305,15 @@ class Proxy extends \OC_FileProxy {
         if (!$view->is_dir($oldKeyfilePath)) {
             $oldKeyfilePath .= '.key';
             $newKeyfilePath .= '.key';
+
+            // handle share-keys
+            $localKeyPath = $view->getLocalFile($userId.'/files_encryption/share-keys/'.$oldRelPath);
+            $matches = glob(preg_quote($localKeyPath).'*.shareKey');
+            foreach ($matches as $src) {
+                $dst = str_replace($oldRelPath, $newRelPath, $src);
+                rename($src, $dst);
+            }
+
         } else {
             // handle share-keys folders
             $oldShareKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'share-keys' . '/' . $oldRelPath;
@@ -314,9 +321,6 @@ class Proxy extends \OC_FileProxy {
             $view->rename($oldShareKeyfilePath, $newShareKeyfilePath);
         }
 
-        //TODO add support for share-keys files
-        //...
-
         // Rename keyfile so it isn't orphaned
         $result = $view->rename($oldKeyfilePath, $newKeyfilePath);
 
@@ -326,6 +330,70 @@ class Proxy extends \OC_FileProxy {
 
     }
 
+    /**
+     * @brief When a file is renamed, rename its keyfile also
+     * @return bool Result of rename()
+     * @note This is pre rather than post because using post didn't work
+     */
+    public function postRename( $oldPath, $newPath )
+    {
+
+        // Disable encryption proxy to prevent recursive calls
+        $proxyStatus = \OC_FileProxy::$enabled;
+        \OC_FileProxy::$enabled = false;
+
+        $view = new \OC_FilesystemView('/');
+        $userId = \OCP\User::getUser();
+        $util = new Util( $view, $userId );
+
+        // Reformat path for use with OC_FSV
+        $newPathSplit = explode( '/', $newPath );
+        $newPathRelative = implode( '/', array_slice( $newPathSplit, 3 ) );
+        $newPathRelativeToUser = implode( '/', array_slice( $newPathSplit, 2 ) );
+
+        // get file info from database/cache
+        //$newFileInfo = \OC\Files\Filesystem::getFileInfo($newPathRelative);
+
+        if ($util->isEncryptedPath($newPath)) {
+            $cached = $view->getFileInfo($newPath);
+            $cached['encrypted'] = 1;
+
+            // get the size from filesystem
+            $size = $view->filesize($newPath);
+
+            // calculate last chunk nr
+            $lastChunckNr = floor($size / 8192);
+
+            // open stream
+            $result = fopen('crypt://' . $newPathRelative, "r");
+
+            if(is_resource($result)) {
+                // calculate last chunk position
+                $lastChunckPos = ($lastChunckNr * 8192);
+
+                // seek to end
+                fseek($result, $lastChunckPos);
+
+                // get the content of the last chunck
+                $lastChunkContent = fread($result, 8192);
+
+                // calc the real file size with the size of the last chunk
+                $realSize = (($lastChunckNr * 6126) + strlen($lastChunkContent));
+
+                // set the size
+                $cached['unencrypted_size'] = $realSize;
+            }
+
+            $view->putFileInfo( $newPath, $cached );
+
+        }
+
+        \OC_FileProxy::$enabled = $proxyStatus;
+
+        return true;
+
+    }
+
     public function postFopen( $path, &$result ){
 
         if ( !$result ) {
@@ -424,11 +492,11 @@ class Proxy extends \OC_FileProxy {
 
         // if path is a folder do nothing
         if(is_array($data) && array_key_exists('size', $data)) {
+
             // Disable encryption proxy to prevent recursive calls
             $proxyStatus = \OC_FileProxy::$enabled;
             \OC_FileProxy::$enabled = false;
 
-
             // get file size
             $data['size'] = self::postFileSize($path, $data['size']);
 
@@ -461,8 +529,6 @@ class Proxy extends \OC_FileProxy {
             return $size;
         }
 
-        $path = Keymanager::fixPartialFilePath( $path );
-
         // Reformat path for use with OC_FSV
         $path_split = explode('/', $path);
         $path_f = implode('/', array_slice($path_split, 3));
@@ -474,7 +540,7 @@ class Proxy extends \OC_FileProxy {
         if(is_array($fileInfo) && $fileInfo['encrypted'] == 1) {
             return $fileInfo['unencrypted_size'];
         } else {
-            return $fileInfo['size'];
+            return $size;
         }
        }
 }
index 7e42627f8ca07f2ffe908ab6c4f115c83e8a25ac..411bcdac92d7200c3ed44b93fd951fbea2d98693 100644 (file)
@@ -87,10 +87,7 @@ class Stream {
                // rawPath is relative to the data directory
                $this->rawPath = $this->userId . '/files/' . $this->relPath;
                
-               // Fix .part filenames
-               $this->rawPath = Keymanager::fixPartialFilePath( $this->rawPath );
-               
-               if ( 
+               if (
                dirname( $this->rawPath ) == 'streams' 
                and isset( self::$sourceStreams[basename( $this->rawPath )] ) 
                ) {
@@ -244,10 +241,7 @@ class Stream {
                
                }
                
-               // Avoid problems with .part file extensions
-               $this->relPath = Keymanager::fixPartialFilePath( $this->relPath );
-
-        // Fetch and decrypt keyfile
+               // Fetch and decrypt keyfile
         // Fetch existing keyfile
         $this->encKeyfile = Keymanager::getFileKey( $this->rootView, $this->userId, $this->relPath );