]> source.dussan.org Git - nextcloud-server.git/commitdiff
proper mimetypes for encrypted files
authorRobin Appelman <icewind@owncloud.com>
Wed, 15 Feb 2012 20:44:58 +0000 (21:44 +0100)
committerRobin Appelman <icewind@owncloud.com>
Tue, 21 Feb 2012 19:48:48 +0000 (20:48 +0100)
apps/files_encryption/lib/proxy.php
lib/filestorage/local.php
lib/helper.php
tests/lib/streamwrappers.php [new file with mode: 0644]

index 7974d68d48753977bbdb8930734116b2cd096977..c53d567ad10011c7d968698e08503fe4c6ffcc33 100644 (file)
@@ -115,4 +115,8 @@ class OC_FileProxy_Encryption extends OC_FileProxy{
                        return false;//cancel the original request
                }
        }
+
+       public function postGetMimeType($path,$mime){
+               return OC_Helper::getMimeType('crypt://'.$path,'w');
+       }
 }
index de1f83e3e14079c3ffb100441be478adcf48debf..6f4f68c503d7f22b8f1875885f8563759b570597 100644 (file)
@@ -122,50 +122,9 @@ class OC_Filestorage_Local extends OC_Filestorage{
                return $return;
        }
 
-       public function getMimeType($fspath){
-               if($this->is_readable($fspath)){
-                       $mimeType='application/octet-stream';
-                       if ($mimeType=='application/octet-stream') {
-                               self::$mimetypes = include('mimetypes.fixlist.php');
-                               $extention=strtolower(strrchr(basename($fspath), "."));
-                               $extention=substr($extention,1);//remove leading .
-                               $mimeType=(isset(self::$mimetypes[$extention]))?self::$mimetypes[$extention]:'application/octet-stream';
-                               
-                       }
-                       if (@is_dir($this->datadir.$fspath)) {
-                               // directories are easy
-                               return "httpd/unix-directory";
-                       }
-                       if($mimeType=='application/octet-stream' and function_exists('finfo_open') and function_exists('finfo_file') and $finfo=finfo_open(FILEINFO_MIME)){
-                               $mimeType =strtolower(finfo_file($finfo,$this->datadir.$fspath));
-                               $mimeType=substr($mimeType,0,strpos($mimeType,';'));
-                               finfo_close($finfo);
-                       }
-                       if ($mimeType=='application/octet-stream' && function_exists("mime_content_type")) {
-                               // use mime magic extension if available
-                               $mimeType = mime_content_type($this->datadir.$fspath);
-                       }
-                       if ($mimeType=='application/octet-stream' && OC_Helper::canExecute("file")) {
-                               // it looks like we have a 'file' command,
-                               // lets see it it does have mime support
-                               $fspath=str_replace("'","\'",$fspath);
-                               $fp = popen("file -i -b '{$this->datadir}$fspath' 2>/dev/null", "r");
-                               $reply = fgets($fp);
-                               pclose($fp);
-
-                               //trim the character set from the end of the response
-                               $mimeType=substr($reply,0,strrpos($reply,' '));
-                       }
-                       if ($mimeType=='application/octet-stream') {
-                               // Fallback solution: (try to guess the type by the file extension
-                               if(!self::$mimetypes || self::$mimetypes != include('mimetypes.list.php')){
-                                       self::$mimetypes=include('mimetypes.list.php');
-                               }
-                               $extention=strtolower(strrchr(basename($fspath), "."));
-                               $extention=substr($extention,1);//remove leading .
-                               $mimeType=(isset(self::$mimetypes[$extention]))?self::$mimetypes[$extention]:'application/octet-stream';
-                       }
-                       return $mimeType;
+       public function getMimeType($path){
+               if($this->is_readable($path)){
+                       return OC_Helper::getMimeType($this->datadir.$path);
                }else{
                        return false;
                }
index 2f71bdad2dc8e22e9126dae7d063a92ea894429f..6dea4a6a8cdeef593ef8d1656aab9b2675138ba2 100644 (file)
@@ -25,6 +25,8 @@
  * Collection of useful functions
  */
 class OC_Helper {
+       private static $mimetypes=array();
+       
        /**
         * @brief Creates an url
         * @param $app app
@@ -267,6 +269,62 @@ class OC_Helper {
                        unlink($dir);
                }
        }
+
+       /**
+        * get the mimetype form a local file
+        * @param string path
+        * @return string
+        * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
+        */
+       static function getMimeType($path){
+               $isWrapped=(strpos($path,'://')!==false) and (substr($path,0,7)=='file://');
+               $mimeType='application/octet-stream';
+               if ($mimeType=='application/octet-stream') {
+                       if(count(self::$mimetypes)>0){
+                               self::$mimetypes = include('mimetypes.fixlist.php');
+                       }
+                       $extention=strtolower(strrchr(basename($path), "."));
+                       $extention=substr($extention,1);//remove leading .
+                       $mimeType=(isset(self::$mimetypes[$extention]))?self::$mimetypes[$extention]:'application/octet-stream';
+
+               }
+               if (@is_dir($path)) {
+                       // directories are easy
+                       return "httpd/unix-directory";
+               }
+               if($mimeType=='application/octet-stream' and function_exists('finfo_open') and function_exists('finfo_file') and $finfo=finfo_open(FILEINFO_MIME)){
+                       $info = @strtolower(finfo_file($finfo,$path));
+                       if($info){
+                               $mimeType=substr($info,0,strpos($info,';'));
+                       }
+                       finfo_close($finfo);
+               }
+               if (!$isWrapped and $mimeType=='application/octet-stream' && function_exists("mime_content_type")) {
+                       // use mime magic extension if available
+                       $mimeType = mime_content_type($path);
+               }
+               if (!$isWrapped and $mimeType=='application/octet-stream' && OC_Helper::canExecute("file")) {
+                       // it looks like we have a 'file' command,
+                       // lets see it it does have mime support
+                       $path=str_replace("'","\'",$path);
+                       $fp = popen("file -i -b '$path' 2>/dev/null", "r");
+                       $reply = fgets($fp);
+                       pclose($fp);
+
+                       //trim the character set from the end of the response
+                       $mimeType=substr($reply,0,strrpos($reply,' '));
+               }
+               if ($mimeType=='application/octet-stream') {
+                       // Fallback solution: (try to guess the type by the file extension
+                       if(!self::$mimetypes || self::$mimetypes != include('mimetypes.list.php')){
+                               self::$mimetypes=include('mimetypes.list.php');
+                       }
+                       $extention=strtolower(strrchr(basename($path), "."));
+                       $extention=substr($extention,1);//remove leading .
+                       $mimeType=(isset(self::$mimetypes[$extention]))?self::$mimetypes[$extention]:'application/octet-stream';
+               }
+               return $mimeType;
+       }
        
        /**
         * @brief Checks $_REQUEST contains a var for the $s key. If so, returns the html-escaped value of this var; otherwise returns the default value provided by $d.
diff --git a/tests/lib/streamwrappers.php b/tests/lib/streamwrappers.php
new file mode 100644 (file)
index 0000000..c4784a6
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+/**
+* ownCloud
+*
+* @author Robin Appelman
+* @copyright 2012 Robin Appelman icewind@owncloud.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+*
+* You should have received a copy of the GNU Affero General Public
+* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+class Test_StreamWrappers extends UnitTestCase {
+       public function testFakeDir(){
+               $items=array('foo','bar');
+               OC_FakeDirStream::$dirs['test']=$items;
+               $dh=opendir('fakedir://test');
+               $result=array();
+               while($file=readdir($dh)){
+                       $result[]=$file;
+                       $this->assertNotIdentical(false,array_search($file,$items));
+               }
+               $this->assertEqual(count($items),count($result));
+       }
+
+       public function testStaticStream(){
+               $sourceFile=OC::$SERVERROOT.'/tests/data/lorem.txt';
+               $staticFile='static://test';
+               $this->assertFalse(file_exists($staticFile));
+               file_put_contents($staticFile,file_get_contents($sourceFile));
+               $this->assertTrue(file_exists($staticFile));
+               $this->assertEqual(file_get_contents($sourceFile),file_get_contents($staticFile));
+               unlink($staticFile);
+               clearstatcache();
+               $this->assertFalse(file_exists($staticFile));
+       }
+}
\ No newline at end of file