diff --git a/files/upload.php b/files/upload.php index b5fed2ed5b..0aa435cad6 100644 --- a/files/upload.php +++ b/files/upload.php @@ -22,6 +22,8 @@ */ require_once('../inc/lib_base.php'); +// sleep(5); //immitate slow internet. + $fileName=$_FILES['file']['name']; $source=$_FILES['file']['tmp_name']; $target=$CONFIG_DATADIRECTORY.'/'.$_GET['dir'].'/'.$fileName; diff --git a/inc/lib_files.php b/inc/lib_files.php index a4e1c6a5a4..6188723c02 100755 --- a/inc/lib_files.php +++ b/inc/lib_files.php @@ -59,6 +59,7 @@ class OC_FILES { $file['directory']=$directory; $stat=stat($directory.'/'.$filename); $file=array_merge($file,$stat); + $file['mime']=OC_FILES::getMimeType($directory .'/'. $filename); $file['type']=filetype($directory .'/'. $filename); if($file['type']=='dir'){ $dirs[$file['name']]=$file; @@ -190,6 +191,71 @@ class OC_FILES { } } } + + /** + * try to detect the mime type of a file + * + * @param string file path + * @return string guessed mime type + */ + function getMimeType($fspath){ + if (@is_dir($fspath)) { + // directories are easy + return "httpd/unix-directory"; + } else if (function_exists("mime_content_type")) { + // use mime magic extension if available + $mime_type = mime_content_type($fspath); + } else if ($this->_can_execute("file")) { + // it looks like we have a 'file' command, + // lets see it it does have mime support + $fp = popen("file -i '$fspath' 2>/dev/null", "r"); + $reply = fgets($fp); + pclose($fp); + + // popen will not return an error if the binary was not found + // and find may not have mime support using "-i" + // so we test the format of the returned string + + // the reply begins with the requested filename + if (!strncmp($reply, "$fspath: ", strlen($fspath)+2)) { + $reply = substr($reply, strlen($fspath)+2); + // followed by the mime type (maybe including options) + if (preg_match('/^[[:alnum:]_-]+/[[:alnum:]_-]+;?.*/', $reply, $matches)) { + $mime_type = $matches[0]; + } + } + } + if (empty($mime_type)) { + // Fallback solution: try to guess the type by the file extension + // TODO: add more ... + switch (strtolower(strrchr(basename($fspath), "."))) { + case ".html": + $mime_type = "text/html"; + break; + case ".txt": + $mime_type = "text/plain"; + break; + case ".css": + $mime_type = "text/css"; + break; + case ".gif": + $mime_type = "image/gif"; + break; + case ".jpg": + $mime_type = "image/jpeg"; + break; + case ".jpg": + $mime_type = "png/jpeg"; + break; + default: + $mime_type = "application/octet-stream"; + break; + } + } + + return $mime_type; + } + } function zipAddDir($dir,$zip,$internalDir=''){ diff --git a/js/filebrowser.js b/js/filebrowser.js index f12cec4414..cc03fe3315 100644 --- a/js/filebrowser.js +++ b/js/filebrowser.js @@ -129,16 +129,16 @@ OC_FILES.browser.files.show=function(parent,fileList){ for(name in fileList){ file=fileList[name]; if(!OC_FILES.browser.files.fileNodes[file.name]){ - OC_FILES.browser.files.add(file.name,file.type,file.size,file.date); + OC_FILES.browser.files.add(file.name,file.type,file.size,file.date,file.mime); } } } } -OC_FILES.browser.files.add=function(name,type,size,date){ +OC_FILES.browser.files.add=function(name,type,size,date,mime){ if(name){ if(!size) size=0; if(!date) date=getTimeString(); - OC_FILES.files[name]=new OC_FILES.file(OC_FILES.dir,name,type); + OC_FILES.files[name]=new OC_FILES.file(OC_FILES.dir,name,type,mime); tr=document.createElement('tr'); OC_FILES.browser.files.fileNodes[name]=tr; OC_FILES.browser.files.tbody.appendChild(tr); diff --git a/js/lib_files.js b/js/lib_files.js index 7c23ee16a9..f60b399746 100644 --- a/js/lib_files.js +++ b/js/lib_files.js @@ -49,7 +49,7 @@ OC_FILES.getdirectorycontent_parse=function(req){ if(fileElements.length>0){ for(index=0;index