Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Some more fixes in detecting the mimetype from the content
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and Backportbot committed Dec 12, 2019
commit 1fe16ca6f52eaa30649e91d8774234d15ba4af20
40 changes: 24 additions & 16 deletions lib/private/Files/Type/Detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function detectPath($path) {
if (strpos($fileName, '.') > 0) {

// 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
$extension = strtolower(strrchr($fileName, '.'));
Expand Down Expand Up @@ -206,38 +206,46 @@ public function detectContent(string $path): string {
}

if (function_exists('finfo_open')
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
) {
$info = @strtolower(finfo_file($finfo, $path));
&& function_exists('finfo_file')
&& $finfo = finfo_open(FILEINFO_MIME)) {
$info = @finfo_file($finfo, $path);
finfo_close($finfo);
if ($info) {
$info = strtolower($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) {
// Is the file wrapped in a stream?
return 'application/octet-stream';
}
$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
if (!$isWrapped and function_exists("mime_content_type")) {

if (function_exists('mime_content_type')) {
// use mime magic extension if available
$mimeType = mime_content_type($path);
if ($mimeType !== false) {
return $this->getSecureMimeType($mimeType);
}
}
if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {

if (\OC_Helper::canExecute('file')) {
// it looks like we have a 'file' command,
// lets see if it does have mime support
$path = escapeshellarg($path);
$fp = popen("file -b --mime-type $path 2>/dev/null", "r");
$reply = fgets($fp);
$fp = popen("test -f $path && file -b --mime-type $path", 'r');
$mimeType = fgets($fp);
pclose($fp);

//trim the newline
$mimeType = trim($reply);

if (empty($mimeType)) {
$mimeType = 'application/octet-stream';
if ($mimeType !== false) {
//trim the newline
$mimeType = trim($mimeType);
return $this->getSecureMimeType($mimeType);
}

}
return $mimeType;
return 'application/octet-stream';
}

/**
Expand Down