Skip to content
Merged
Changes from all commits
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
33 changes: 16 additions & 17 deletions lib/private/Files/Type/Detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function __construct(
* @param string $mimeType
* @param string|null $secureMimeType
*/
public function registerType(string $extension,
public function registerType(
string $extension,
string $mimeType,
?string $secureMimeType = null): void {
// Make sure the extension is a string
Expand Down Expand Up @@ -217,14 +218,10 @@ public function detectContent(string $path): string {
return 'httpd/unix-directory';
}

if (function_exists('finfo_open')
&& function_exists('finfo_file')
&& $finfo = finfo_open(FILEINFO_MIME)) {
$info = @finfo_file($finfo, $path);
finfo_close($finfo);
if ($info) {
$info = strtolower($info);
$mimeType = str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info;
if (class_exists(finfo::class)) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = @$finfo->file($path);
if ($mimeType) {
$mimeType = $this->getSecureMimeType($mimeType);
if ($mimeType !== 'application/octet-stream') {
return $mimeType;
Expand All @@ -240,7 +237,7 @@ public function detectContent(string $path): string {
if (function_exists('mime_content_type')) {
// use mime magic extension if available
$mimeType = mime_content_type($path);
if ($mimeType !== false) {
if ($mimeType) {
$mimeType = $this->getSecureMimeType($mimeType);
if ($mimeType !== 'application/octet-stream') {
return $mimeType;
Expand All @@ -258,7 +255,7 @@ public function detectContent(string $path): string {
if ($fp !== false) {
$mimeType = fgets($fp);
pclose($fp);
if ($mimeType !== false) {
if ($mimeType) {
//trim the newline
$mimeType = trim($mimeType);
$mimeType = $this->getSecureMimeType($mimeType);
Expand Down Expand Up @@ -293,19 +290,21 @@ public function detect($path): string {
* @return string
*/
public function detectString($data): string {
if (function_exists('finfo_open') && function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME);
$info = finfo_buffer($finfo, $data);
return str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info;
if (class_exists(finfo::class)) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($data);
if ($mimeType) {
return $mimeType;
}
}

$tmpFile = \OCP\Server::get(ITempManager::class)->getTemporaryFile();
$fh = fopen($tmpFile, 'wb');
fwrite($fh, $data, 8024);
fclose($fh);
$mime = $this->detect($tmpFile);
$mimeType = $this->detect($tmpFile);
unset($tmpFile);
return $mime;
return $mimeType;
}

/**
Expand Down
Loading