mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
Merge pull request #54077 from nextcloud/jtr-files-detection-refactor-finfo
refactor(IMimeTypeDetector): streamline + minor tidying
This commit is contained in:
commit
ad85d65064
1 changed files with 16 additions and 17 deletions
|
|
@ -55,7 +55,8 @@ class Detection implements IMimeTypeDetector {
|
|||
* @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
|
||||
|
|
@ -217,14 +218,10 @@ class Detection implements IMimeTypeDetector {
|
|||
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;
|
||||
|
|
@ -240,7 +237,7 @@ class Detection implements IMimeTypeDetector {
|
|||
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;
|
||||
|
|
@ -258,7 +255,7 @@ class Detection implements IMimeTypeDetector {
|
|||
if ($fp !== false) {
|
||||
$mimeType = fgets($fp);
|
||||
pclose($fp);
|
||||
if ($mimeType !== false) {
|
||||
if ($mimeType) {
|
||||
//trim the newline
|
||||
$mimeType = trim($mimeType);
|
||||
$mimeType = $this->getSecureMimeType($mimeType);
|
||||
|
|
@ -293,19 +290,21 @@ class Detection implements IMimeTypeDetector {
|
|||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue