Some more fixes in detecting the mimetype from the content

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2019-11-29 11:04:20 +01:00
parent 1336dedd5d
commit da44c2a414
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
1 changed files with 17 additions and 15 deletions

View File

@ -200,7 +200,7 @@ class Detection implements IMimeTypeDetector {
if (strpos($fileName, '.') > 0) { if (strpos($fileName, '.') > 0) {
// remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part // remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part
$fileName = preg_replace('!((\.v\d+)|((.ocTransferId\d+)?.part))$!', '', $fileName); $fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName);
//try to guess the type by the file extension //try to guess the type by the file extension
$extension = strtolower(strrchr($fileName, '.')); $extension = strtolower(strrchr($fileName, '.'));
@ -225,45 +225,47 @@ class Detection implements IMimeTypeDetector {
return 'httpd/unix-directory'; return 'httpd/unix-directory';
} }
$mimeType = 'application/octet-stream';
if (function_exists('finfo_open') if (function_exists('finfo_open')
&& function_exists('finfo_file') && function_exists('finfo_file')
&& $finfo = finfo_open(FILEINFO_MIME)) { && $finfo = finfo_open(FILEINFO_MIME)) {
$info = @strtolower(finfo_file($finfo, $path)); $info = @finfo_file($finfo, $path);
finfo_close($finfo); finfo_close($finfo);
if ($info) { if ($info) {
$info = strtolower($info);
$mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info; $mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
return empty($mimeType) ? 'application/octet-stream' : $mimeType; return $this->getSecureMimeType($mimeType);
} }
} }
if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) { if (strpos($path, '://') !== false && strpos($path, 'file://') === 0) {
// Is the file wrapped in a stream? // Is the file wrapped in a stream?
return $mimeType; return 'application/octet-stream';
} }
if (function_exists('mime_content_type')) { if (function_exists('mime_content_type')) {
// use mime magic extension if available // use mime magic extension if available
$mimeType = mime_content_type($path); $mimeType = mime_content_type($path);
if ($mimeType !== false) {
return $this->getSecureMimeType($mimeType);
}
} }
if ($mimeType === 'application/octet-stream' && \OC_Helper::canExecute('file')) {
if (\OC_Helper::canExecute('file')) {
// it looks like we have a 'file' command, // it looks like we have a 'file' command,
// lets see if it does have mime support // lets see if it does have mime support
$path = escapeshellarg($path); $path = escapeshellarg($path);
$fp = popen("file -b --mime-type $path 2>/dev/null", 'r'); $fp = popen("test -f $path && file -b --mime-type $path", 'r');
$reply = fgets($fp); $mimeType = fgets($fp);
pclose($fp); pclose($fp);
//trim the newline if ($mimeType !== false) {
$mimeType = trim($reply); //trim the newline
$mimeType = trim($mimeType);
if (empty($mimeType)) { return $this->getSecureMimeType($mimeType);
$mimeType = 'application/octet-stream';
} }
} }
return $mimeType; return 'application/octet-stream';
} }
/** /**