aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Appelman <icewind@owncloud.com>2012-02-15 21:44:58 +0100
committerRobin Appelman <icewind@owncloud.com>2012-02-21 20:48:48 +0100
commitd9c7e4c333f858efaaee35d26ea12733d29bd694 (patch)
treef7180a1491409efc5dc7e5442326c6928ed33ff1
parent325858e9e239b726a207ac150404863df509935c (diff)
downloadnextcloud-server-d9c7e4c333f858efaaee35d26ea12733d29bd694.tar.gz
nextcloud-server-d9c7e4c333f858efaaee35d26ea12733d29bd694.zip
proper mimetypes for encrypted files
-rw-r--r--apps/files_encryption/lib/proxy.php4
-rw-r--r--lib/filestorage/local.php47
-rw-r--r--lib/helper.php58
-rw-r--r--tests/lib/streamwrappers.php47
4 files changed, 112 insertions, 44 deletions
diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php
index 7974d68d487..c53d567ad10 100644
--- a/apps/files_encryption/lib/proxy.php
+++ b/apps/files_encryption/lib/proxy.php
@@ -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');
+ }
}
diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php
index de1f83e3e14..6f4f68c503d 100644
--- a/lib/filestorage/local.php
+++ b/lib/filestorage/local.php
@@ -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;
}
diff --git a/lib/helper.php b/lib/helper.php
index 2f71bdad2dc..6dea4a6a8cd 100644
--- a/lib/helper.php
+++ b/lib/helper.php
@@ -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
index 00000000000..c4784a62976
--- /dev/null
+++ b/tests/lib/streamwrappers.php
@@ -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