]> source.dussan.org Git - nextcloud-server.git/commitdiff
improved file size
authorFlorin Peter <github@florin-peter.de>
Sat, 27 Apr 2013 21:34:25 +0000 (23:34 +0200)
committerFlorin Peter <github@florin-peter.de>
Sat, 27 Apr 2013 21:34:25 +0000 (23:34 +0200)
apps/files_encryption/lib/util.php

index a9996999a3a3cbe623433aaebb53c585404c5bbf..fe040d88775b7f3325f2efb6095e8a4617a583fd 100644 (file)
@@ -440,53 +440,78 @@ class Util {
        }
 
     /**
-     * @brief fix the file size of the encrypted file
+     * @brief get the file size of the unencrypted file
      *
      * @param $path absolute path
      * @return true / false if file is encrypted
      */
 
-    public function fixFileSize($path) {
-        $result = false;
+    public function getFileSize($path) {
+        $result = 0;
 
         // Disable encryption proxy to prevent recursive calls
         $proxyStatus = \OC_FileProxy::$enabled;
         \OC_FileProxy::$enabled = false;
 
-        if ($this->view->file_exists($path) && $this->isEncryptedPath($path)) {
-
-            // Reformat path for use with OC_FSV
-            $pathSplit = explode( '/', $path );
-            $pathRelative = implode( '/', array_slice( $pathSplit, 3 ) );
+        // Reformat path for use with OC_FSV
+        $pathSplit = explode( '/', $path );
+        $pathRelative = implode( '/', array_slice( $pathSplit, 3 ) );
 
-            $cached = $this->view->getFileInfo($path);
-            $cached['encrypted'] = 1;
+        if ($pathSplit[2] == 'files' && $this->view->file_exists($path) && $this->isEncryptedPath($path)) {
 
             // get the size from filesystem
-            $size = $this->view->filesize($path);
+            $fullPath = $this->view->getLocalFile($path);
+            $size = filesize($fullPath);
 
             // calculate last chunk nr
             $lastChunckNr = floor($size / 8192);
 
             // open stream
-            $result = fopen('crypt://' . $pathRelative, "r");
+            $stream = fopen('crypt://' . $pathRelative, "r");
 
-            if(is_resource($result)) {
+            if(is_resource($stream)) {
                 // calculate last chunk position
                 $lastChunckPos = ($lastChunckNr * 8192);
 
                 // seek to end
-                fseek($result, $lastChunckPos);
+                fseek($stream, $lastChunckPos);
 
                 // get the content of the last chunk
-                $lastChunkContent = fread($result, 8192);
+                $lastChunkContent = fread($stream, 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;
+                // store file size
+                $result = $realSize;
             }
+        }
+
+        \OC_FileProxy::$enabled = $proxyStatus;
+
+        return $result;
+    }
+    /**
+     * @brief fix the file size of the encrypted file
+     *
+     * @param $path absolute path
+     * @return true / false if file is encrypted
+     */
+
+    public function fixFileSize($path) {
+        $result = false;
+
+        // Disable encryption proxy to prevent recursive calls
+        $proxyStatus = \OC_FileProxy::$enabled;
+        \OC_FileProxy::$enabled = false;
+
+        $realSize = $this->getFileSize($path);
+        if($realSize > 0) {
+            $cached = $this->view->getFileInfo($path);
+            $cached['encrypted'] = 1;
+
+            // set the size
+            $cached['unencrypted_size'] = $realSize;
 
             // put file info
             $this->view->putFileInfo( $path, $cached );