proper mimetypes for encrypted files

This commit is contained in:
Robin Appelman 2012-02-15 21:44:58 +01:00
parent 325858e9e2
commit d9c7e4c333
4 changed files with 112 additions and 44 deletions

View 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');
}
}

View 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;
}

View 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.

View File

@ -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));
}
}