bug fix when starting multiply uploads while the old ones arent finsihed, detect file actions on mimetype not on extention

This commit is contained in:
Robin 2010-04-25 15:04:13 +02:00
parent c2bdd6134b
commit afc0ef420b
4 changed files with 102 additions and 20 deletions

View File

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

View File

@ -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=''){

View File

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

View File

@ -49,7 +49,7 @@ OC_FILES.getdirectorycontent_parse=function(req){
if(fileElements.length>0){
for(index=0;index<fileElements.length;index++){
var file=new Array();
var attributes=Array('size','name','type','directory','date');
var attributes=Array('size','name','type','directory','date','mime');
for(i in attributes){
var name=attributes[i];
file[name]=fileElements.item(index).getAttribute(name);
@ -106,6 +106,11 @@ OC_FILES.upload=function(dir,iframeId){
return false;
}
}
var mime='';
if(fileSelector.files && fileSelector.files[0].type){
var mime=fileSelector.files[0].type;
}
file.dir=dir;
file.dir=dir;
file.name=name;
file.type='file';
@ -118,9 +123,10 @@ OC_FILES.upload=function(dir,iframeId){
OC_FILES.cache.incomplete[dir][name]['name']=name;
OC_FILES.cache.incomplete[dir][name]['type']='incomplete';
OC_FILES.cache.incomplete[dir][name]['size']=size;
OC_FILES.cache.incomplete[dir][name]['mime']=mime;
OC_FILES.uploadIFrames[iframeId].file=file;
OC_FILES.uploadIFrames[iframeId].addEvent('onload',new callBack(OC_FILES.upload_callback,OC_FILES.uploadIFrames[iframeId]));
OC_FILES.browser.files.add(name,'incomplete',size);
OC_FILES.browser.files.add(name,'incomplete',size,null,mime);
OC_FILES.uploadForm.submit();
if(OC_FILES.uploadForm.parentElement){
OC_FILES.uploadForm.className='hidden';
@ -137,11 +143,11 @@ OC_FILES.upload_callback=function(iframeId){
if(OC_FILES.cache.incomplete[file.dir][file.name]){
OC_FILES.browser.files.remove(file.name);
OC_FILES.cache.files[file.name]=OC_FILES.cache.incomplete[file.dir][file.name]
OC_FILES.cache.incomplete[file.dir][file.name]=null;
delete OC_FILES.cache.incomplete[file.dir][file.name];
OC_FILES.cache.files[file.name]['type']=file.type;
this.uploadForm.parentNode.removeChild(this.uploadForm);
this.parentNode.removeChild(this);
delete OC_FILES.uploadIFrames[file.iframeId];
OC_FILES.uploadIFrames[file.iframeId]=null;
OC_FILES.browser.show(file.dir);
}
}
@ -305,11 +311,17 @@ OC_FILES.actions_selected['delete']=function(){
OC_FILES.files=Array();
OC_FILES.file=function(dir,file,type){
OC_FILES.file=function(dir,file,type,mime){
if(file){
this.type=type;
this.file=file;
this.dir=dir;
this.mime=mime;
if(mime){
var mimeParts=mime.split('/');
this.mime1=mimeParts[0];
this.mime2=mimeParts[1];
}
this.actions=new Object();
if(file.lastIndexOf('.')){
this.extention=file.substr(file.lastIndexOf('.')+1);
@ -328,10 +340,17 @@ OC_FILES.file=function(dir,file,type){
}
}
}
if(OC_FILES.fileActions[this.extention]){
for(index in OC_FILES.fileActions[this.extention]){
if(OC_FILES.fileActions[this.extention][index].call){
this.actions[index]=OC_FILES.fileActions[this.extention][index];
if(OC_FILES.fileActions[this.mime1]){
for(index in OC_FILES.fileActions[this.mime1]){
if(OC_FILES.fileActions[this.mime1][index].call){
this.actions[index]=OC_FILES.fileActions[this.mime1][index];
}
}
}
if(OC_FILES.fileActions[this.mime]){
for(index in OC_FILES.fileActions[this.mime]){
if(OC_FILES.fileActions[this.mime][index].call){
this.actions[index]=OC_FILES.fileActions[this.mime][index];
}
}
}
@ -372,15 +391,10 @@ OC_FILES.fileActions.dir.dropOn=function(file){
OC_FILES.move(file.file,file.file,file.dir,this.dir+'/'+this.file);
}
OC_FILES.fileActions.jpg=new Object()
OC_FILES.fileActions.image=new Object()
OC_FILES.fileActions.jpg.show=function(){
OC_FILES.fileActions.image.show=function(){
OC_FILES.browser.showImage(this.dir,this.file);
}
OC_FILES.fileActions.jpg['default']=OC_FILES.fileActions.jpg.show;
OC_FILES.fileActions.jpeg=OC_FILES.fileActions.jpg
OC_FILES.fileActions.png=OC_FILES.fileActions.jpg
OC_FILES.fileActions.gif=OC_FILES.fileActions.jpg
OC_FILES.fileActions.bmp=OC_FILES.fileActions.jpg
OC_FILES.fileActions.image['default']=OC_FILES.fileActions.image.show;