check if we can read the file before trying to get it's mime type

This commit is contained in:
Robin Appelman 2011-01-22 02:28:29 +01:00
parent 9cd4c99918
commit 15d15104d4
1 changed files with 87 additions and 85 deletions

View File

@ -208,94 +208,96 @@ class OC_FILESTORAGE_LOCAL extends OC_FILESTORAGE{
} }
public function getMimeType($fspath){ public function getMimeType($fspath){
if (@is_dir($this->datadir.$fspath)) { if($this->is_readable($fspath)){
// directories are easy if (@is_dir($this->datadir.$fspath)) {
return "httpd/unix-directory"; // directories are easy
}elseif (function_exists('finfo_open') and function_exists('finfo_file') and $finfo=finfo_open(FILEINFO_MIME)){ return "httpd/unix-directory";
$mimeType =strtolower(finfo_file($finfo,$this->datadir.$fspath)); }elseif (function_exists('finfo_open') and function_exists('finfo_file') and $finfo=finfo_open(FILEINFO_MIME)){
$mimeType=substr($mimeType,0,strpos($mimeType,';')); $mimeType =strtolower(finfo_file($finfo,$this->datadir.$fspath));
finfo_close($finfo); $mimeType=substr($mimeType,0,strpos($mimeType,';'));
return $mimeType; finfo_close($finfo);
} else if (function_exists("mime_content_type")) { return $mimeType;
// use mime magic extension if available } else if (function_exists("mime_content_type")) {
$mime_type = mime_content_type($this->datadir.$fspath); // use mime magic extension if available
} else if (self::canExecute("file")) { $mime_type = mime_content_type($this->datadir.$fspath);
// it looks like we have a 'file' command, } else if (self::canExecute("file")) {
// lets see it it does have mime support // it looks like we have a 'file' command,
$fp = popen("file -i -b '{$this->datadir}$fspath' 2>/dev/null", "r"); // lets see it it does have mime support
$reply = fgets($fp); $fp = popen("file -i -b '{$this->datadir}$fspath' 2>/dev/null", "r");
pclose($fp); $reply = fgets($fp);
pclose($fp);
//trim the character set from the end of the response //trim the character set from the end of the response
$mime_type=substr($reply,0,strrpos($reply,' ')); $mime_type=substr($reply,0,strrpos($reply,' '));
} }
if (empty($mime_type)) { if (empty($mime_type)) {
// Fallback solution: try to guess the type by the file extension // Fallback solution: try to guess the type by the file extension
// TODO: add more ... // TODO: add more ...
switch (strtolower(strrchr(basename($fspath), "."))) { switch (strtolower(strrchr(basename($fspath), "."))) {
case '.css': case '.css':
$mime_type = 'text/css'; $mime_type = 'text/css';
break; break;
case '.flac': case '.flac':
$mime_type = 'audio/flac'; $mime_type = 'audio/flac';
break; break;
case '.gif': case '.gif':
$mime_type = 'image/gif'; $mime_type = 'image/gif';
break; break;
case '.gzip': case '.gzip':
case '.gz': case '.gz':
$mime_type = 'application/x-gzip'; $mime_type = 'application/x-gzip';
break; break;
case '.htm': case '.htm':
case '.html': case '.html':
$mime_type = 'text/html'; $mime_type = 'text/html';
break; break;
case '.jpeg': case '.jpeg':
case '.jpg': case '.jpg':
$mime_type = 'image/jpeg'; $mime_type = 'image/jpeg';
break; break;
case '.js': case '.js':
$mime_type = 'application/x-javascript'; $mime_type = 'application/x-javascript';
break; break;
case '.oga': case '.oga':
case '.ogg': case '.ogg':
$mime_type = 'audio/ogg'; $mime_type = 'audio/ogg';
break; break;
case '.ogv': case '.ogv':
$mime_type = 'video/ogg'; $mime_type = 'video/ogg';
break; break;
case '.pdf': case '.pdf':
$mime_type = 'application/pdf'; $mime_type = 'application/pdf';
break; break;
case '.png': case '.png':
$mime_type = 'image/png'; $mime_type = 'image/png';
break; break;
case '.svg': case '.svg':
$mime_type = 'image/svg+xml'; $mime_type = 'image/svg+xml';
break; break;
case '.tar': case '.tar':
$mime_type = 'application/x-tar'; $mime_type = 'application/x-tar';
break; break;
case '.tgz': case '.tgz':
$mime_type = 'application/x-compressed'; $mime_type = 'application/x-compressed';
break; break;
case '.tif': case '.tif':
case '.tiff': case '.tiff':
$mime_type = 'image/tiff'; $mime_type = 'image/tiff';
break; break;
case '.txt': case '.txt':
$mime_type = 'text/plain'; $mime_type = 'text/plain';
break; break;
case '.zip': case '.zip':
$mime_type = 'application/zip'; $mime_type = 'application/zip';
break; break;
default: default:
$mime_type = 'application/octet-stream'; $mime_type = 'application/octet-stream';
break; break;
}
} }
}
return $mime_type; return $mime_type;
}
} }
/** /**