Skip to content
Merged
Show file tree
Hide file tree
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
Next Next commit
Allow to check for the mimetype by content only
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and Backportbot committed Dec 12, 2019
commit a52c84b5b8b322bb4d2ed677fd3625a4297153bb
28 changes: 21 additions & 7 deletions lib/private/Files/Type/Detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,20 @@ public function detectPath($path) {
}

/**
* detect mimetype based on both filename and content
*
* detect mimetype only based on the content of file
* @param string $path
* @return string
* @since 18.0.0
*/
public function detect($path) {
public function detectContent(string $path): string {
$this->loadMappings();

if (@is_dir($path)) {
// directories are easy
return "httpd/unix-directory";
}

$mimeType = $this->detectPath($path);

if ($mimeType === 'application/octet-stream' and function_exists('finfo_open')
if (function_exists('finfo_open')
and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
) {
$info = @strtolower(finfo_file($finfo, $path));
Expand All @@ -219,7 +217,7 @@ public function detect($path) {

}
$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
$mimeType = mime_content_type($path);
}
Expand All @@ -242,6 +240,22 @@ public function detect($path) {
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
*
Expand Down
10 changes: 9 additions & 1 deletion lib/public/Files/IMimeTypeDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ interface IMimeTypeDetector {
* @param string $path
* @return string
* @since 8.2.0
**/
*/
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
*
Expand Down