Allow to check for the mimetype by content only

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2019-11-28 15:24:57 +01:00
parent b78a141b0b
commit b92ebb928a
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 30 additions and 8 deletions

View File

@ -213,12 +213,12 @@ class Detection implements IMimeTypeDetector {
} }
/** /**
* detect mimetype based on both filename and content * detect mimetype only based on the content of file
*
* @param string $path * @param string $path
* @return string * @return string
* @since 18.0.0
*/ */
public function detect($path) { public function detectContent(string $path): string {
$this->loadMappings(); $this->loadMappings();
if (@is_dir($path)) { if (@is_dir($path)) {
@ -226,9 +226,7 @@ class Detection implements IMimeTypeDetector {
return "httpd/unix-directory"; return "httpd/unix-directory";
} }
$mimeType = $this->detectPath($path); if (function_exists('finfo_open')
if ($mimeType === 'application/octet-stream' and function_exists('finfo_open')
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME) and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
) { ) {
$info = @strtolower(finfo_file($finfo, $path)); $info = @strtolower(finfo_file($finfo, $path));
@ -240,7 +238,7 @@ class Detection implements IMimeTypeDetector {
} }
$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://'); $isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) { if (!$isWrapped and 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);
} }
@ -263,6 +261,22 @@ class Detection implements IMimeTypeDetector {
return $mimeType; return $mimeType;
} }
/**
* detect mimetype based on both filename and content
*
* @param string $path
* @return string
*/
public function detect($path) {
$mimeType = $this->detectPath($path);
if ($mimeType !== 'application/octet-stream') {
return $mimeType;
}
return $this->detectContent($path);
}
/** /**
* detect mimetype based on the content of a string * detect mimetype based on the content of a string
* *

View File

@ -40,9 +40,17 @@ interface IMimeTypeDetector {
* @param string $path * @param string $path
* @return string * @return string
* @since 8.2.0 * @since 8.2.0
**/ */
public function detectPath($path); public function detectPath($path);
/**
* detect mimetype only based on the content of file
* @param string $path
* @return string
* @since 18.0.0
*/
public function detectContent(string $path): string;
/** /**
* detect mimetype based on both filename and content * detect mimetype based on both filename and content
* *