@ -9,6 +9,7 @@ declare(strict_types=1);
namespace OC\Files\Type;
use OCP\Files\IMimeTypeDetector;
use OCP\ITempManager;
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
@ -24,10 +25,10 @@ class Detection implements IMimeTypeDetector {
private const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
/** @var array< list { string , string | null } > */
protected array $mimet ypes = [];
protected array $mimeT ypes = [];
protected array $secureMimeTypes = [];
protected array $mimet ypeIcons = [];
protected array $mimeT ypeIcons = [];
/** @var array< string , string > */
protected array $mimeTypeAlias = [];
@ -40,30 +41,30 @@ class Detection implements IMimeTypeDetector {
}
/**
* Add an extension -> mime type mapping
* Add an extension -> MIME type mapping
*
* $mimet ype is the assumed correct mime type
* $mimeT ype is the assumed correct mime type
* The optional $secureMimeType is an alternative to send to send
* to avoid potential XSS.
*
* @param string $extension
* @param string $mimet ype
* @param string $mimeT ype
* @param string|null $secureMimeType
*/
public function registerType(string $extension,
string $mimet ype,
string $mimeT ype,
?string $secureMimeType = null): void {
// Make sure the extension is a string
// https://github.com/nextcloud/server/issues/42902
$this->mimetypes[$extension] = [$mimet ype, $secureMimeType];
$this->secureMimeTypes[$mimetype] = $secureMimeType ?: $mimet ype;
$this->mimeTypes[$extension] = [$mimeT ype, $secureMimeType];
$this->secureMimeTypes[$mimeType] = $secureMimeType ?? $mimeT ype;
}
/**
* Add an array of extension -> mime type mappings
* Add an array of extension -> MIME type mappings
*
* The mimet ype value is in itself an array where the first index is
* the assumed correct mimet ype and the second is either a secure alternative
* The mimeT ype value is in itself an array where the first index is
* the assumed correct mimeT ype and the second is either a secure alternative
* or null if the correct is considered secure.
*
* @param array $types
@ -74,8 +75,8 @@ class Detection implements IMimeTypeDetector {
$this->registerType((string)$extension, $mimeType[0], $mimeType[1] ?? null);
}
// Update the alternative mimet ypes to avoid having to look them up each time.
foreach ($this->mimet ypes as $extension => $mimeType) {
// Update the alternative mimeT ypes to avoid having to look them up each time.
foreach ($this->mimeT ypes as $extension => $mimeType) {
if (str_starts_with((string)$extension, '_comment')) {
continue;
}
@ -100,7 +101,7 @@ class Detection implements IMimeTypeDetector {
}
/**
* Add the mime type aliases if they are not yet present
* Add the MIME type aliases if they are not yet present
*/
private function loadAliases(): void {
if (!empty($this->mimeTypeAlias)) {
@ -126,17 +127,17 @@ class Detection implements IMimeTypeDetector {
}
/**
* Add mime type mappings if they are not yet present
* Add MIME type mappings if they are not yet present
*/
private function loadMappings(): void {
if (!empty($this->mimet ypes)) {
if (!empty($this->mimeT ypes)) {
return;
}
$mimet ypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
$mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimet ypeMapping);
$mimeT ypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
$mimeTypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimeT ypeMapping);
$this->registerTypeArray($mimet ypeMapping);
$this->registerTypeArray($mimeT ypeMapping);
}
/**
@ -144,11 +145,11 @@ class Detection implements IMimeTypeDetector {
*/
public function getAllMappings(): array {
$this->loadMappings();
return $this->mimet ypes;
return $this->mimeT ypes;
}
/**
* detect mime type only based on filename, content of file is not used
* detect MIME type only based on filename, content of file is not used
*
* @param string $path
* @return string
@ -171,7 +172,7 @@ class Detection implements IMimeTypeDetector {
if ($extension !== false) {
$extension = strtolower($extension);
$extension = substr($extension, 1); // remove leading .
return $this->mimet ypes[$extension][0] ?? 'application/octet-stream';
return $this->mimeT ypes[$extension][0] ?? 'application/octet-stream';
}
}
@ -179,7 +180,8 @@ class Detection implements IMimeTypeDetector {
}
/**
* detect mimetype only based on the content of file
* Detect MIME type only based on the content of file.
*
* @param string $path
* @return string
* @since 18.0.0
@ -244,7 +246,7 @@ class Detection implements IMimeTypeDetector {
}
/**
* detect mime type based on both filename and content
* Detect MIME type based on both filename and content
*
* @param string $path
* @return string
@ -260,7 +262,7 @@ class Detection implements IMimeTypeDetector {
}
/**
* detect mime type based on the content of a string
* Detect MIME type based on the content of a string
*
* @param string $data
* @return string
@ -272,7 +274,7 @@ class Detection implements IMimeTypeDetector {
return str_contains($info, ';') ? substr($info, 0, strpos($info, ';')) : $info;
}
$tmpFile = \OC::$server->getTempManager( )->getTemporaryFile();
$tmpFile = \OCP\Server::get(ITempManager::class )->getTemporaryFile();
$fh = fopen($tmpFile, 'wb');
fwrite($fh, $data, 8024);
fclose($fh);
@ -282,7 +284,7 @@ class Detection implements IMimeTypeDetector {
}
/**
* Get a secure mime type that won't expose potential XSS.
* Get a secure MIME type that won't expose potential XSS.
*
* @param string $mimeType
* @return string
@ -295,57 +297,56 @@ class Detection implements IMimeTypeDetector {
/**
* Get path to the icon of a file type
* @param string $mimet ype the MIME type
* @param string $mimeT ype the MIME type
* @return string the url
*/
public function mimeTypeIcon($mimet ype): string {
public function mimeTypeIcon($mimeT ype): string {
$this->loadAliases();
while (isset($this->mimeTypeAlias[$mimet ype])) {
$mimetype = $this->mimeTypeAlias[$mimet ype];
while (isset($this->mimeTypeAlias[$mimeT ype])) {
$mimeType = $this->mimeTypeAlias[$mimeT ype];
}
if (isset($this->mimetypeIcons[$mimet ype])) {
return $this->mimetypeIcons[$mimet ype];
if (isset($this->mimeTypeIcons[$mimeT ype])) {
return $this->mimeTypeIcons[$mimeT ype];
}
// Replace slash and backslash with a minus
$icon = str_replace(['/', '\\'], '-', $mimet ype);
$icon = str_replace(['/', '\\'], '-', $mimeT ype);
// Is it a dir?
if ($mimet ype === 'dir') {
$this->mimetypeIcons[$mimet ype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.svg');
return $this->mimetypeIcons[$mimet ype];
if ($mimeT ype === 'dir') {
$this->mimeTypeIcons[$mimeT ype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.svg');
return $this->mimeTypeIcons[$mimeT ype];
}
if ($mimet ype === 'dir-shared') {
$this->mimetypeIcons[$mimet ype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.svg');
return $this->mimetypeIcons[$mimet ype];
if ($mimeT ype === 'dir-shared') {
$this->mimeTypeIcons[$mimeT ype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.svg');
return $this->mimeTypeIcons[$mimeT ype];
}
if ($mimet ype === 'dir-external') {
$this->mimetypeIcons[$mimet ype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.svg');
return $this->mimetypeIcons[$mimet ype];
if ($mimeT ype === 'dir-external') {
$this->mimeTypeIcons[$mimeT ype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.svg');
return $this->mimeTypeIcons[$mimeT ype];
}
// Icon exists?
try {
$this->mimetypeIcons[$mimet ype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.svg');
return $this->mimetypeIcons[$mimet ype];
$this->mimeTypeIcons[$mimeT ype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.svg');
return $this->mimeTypeIcons[$mimeT ype];
} catch (\RuntimeException $e) {
// Specified image not found
}
// Try only the first part of the filetype
if (strpos($icon, '-')) {
$mimePart = substr($icon, 0, strpos($icon, '-'));
try {
$this->mimetypeIcons[$mimet ype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.svg');
return $this->mimetypeIcons[$mimet ype];
$this->mimeTypeIcons[$mimeT ype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.svg');
return $this->mimeTypeIcons[$mimeT ype];
} catch (\RuntimeException $e) {
// Image for the first part of the mime type not found
// Image for the first part of the MIME type not found
}
}
$this->mimetypeIcons[$mimet ype] = $this->urlGenerator->imagePath('core', 'filetypes/file.svg');
return $this->mimetypeIcons[$mimet ype];
$this->mimeTypeIcons[$mimeT ype] = $this->urlGenerator->imagePath('core', 'filetypes/file.svg');
return $this->mimeTypeIcons[$mimeT ype];
}
}