Move custom definition logic into method
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
parent
64aba49461
commit
29575c4d36
|
@ -47,8 +47,8 @@ use OCP\IURLGenerator;
|
||||||
*/
|
*/
|
||||||
class Detection implements IMimeTypeDetector {
|
class Detection implements IMimeTypeDetector {
|
||||||
|
|
||||||
public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
|
private const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
|
||||||
public const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
|
private const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
|
||||||
|
|
||||||
protected $mimetypes = [];
|
protected $mimetypes = [];
|
||||||
protected $secureMimeTypes = [];
|
protected $secureMimeTypes = [];
|
||||||
|
@ -121,6 +121,18 @@ class Detection implements IMimeTypeDetector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function loadCustomDefinitions(string $fileName, array $definitions): array {
|
||||||
|
if (file_exists($this->customConfigDir . '/' . $fileName)) {
|
||||||
|
$custom = json_decode(file_get_contents($this->customConfigDir . '/' . $fileName), true);
|
||||||
|
if (json_last_error() === JSON_ERROR_NONE) {
|
||||||
|
$definitions = array_merge($definitions, $custom);
|
||||||
|
} else {
|
||||||
|
$this->logger->warning('Failed to parse ' . $fileName . ': ' . json_last_error_msg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $definitions;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the mimetype aliases if they are not yet present
|
* Add the mimetype aliases if they are not yet present
|
||||||
*/
|
*/
|
||||||
|
@ -130,15 +142,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
|
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
|
||||||
|
$this->mimeTypeAlias = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEALIASES, $this->mimeTypeAlias);
|
||||||
if (file_exists($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEALIASES)) {
|
|
||||||
$custom = json_decode(file_get_contents($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEALIASES), true);
|
|
||||||
if (json_last_error() === JSON_ERROR_NONE) {
|
|
||||||
$this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom);
|
|
||||||
} else {
|
|
||||||
$this->logger->warning('Failed to parse ' . self::CUSTOM_MIMETYPEALIASES . ': ' . json_last_error_msg());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -164,16 +168,7 @@ class Detection implements IMimeTypeDetector {
|
||||||
}
|
}
|
||||||
|
|
||||||
$mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
|
$mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
|
||||||
|
$mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimetypeMapping);
|
||||||
//Check if need to load custom mappings
|
|
||||||
if (file_exists($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEMAPPING)) {
|
|
||||||
$custom = json_decode(file_get_contents($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEMAPPING), true);
|
|
||||||
if (json_last_error() === JSON_ERROR_NONE) {
|
|
||||||
$mimetypeMapping = array_merge($mimetypeMapping, $custom);
|
|
||||||
} else {
|
|
||||||
$this->logger->warning('Failed to parse ' . self::CUSTOM_MIMETYPEMAPPING . ': ' . json_last_error_msg());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->registerTypeArray($mimetypeMapping);
|
$this->registerTypeArray($mimetypeMapping);
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,9 +116,8 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
->getMock();
|
->getMock();
|
||||||
|
|
||||||
$logger = $this->getMockBuilder(ILogger::class)
|
/** @var ILogger $logger */
|
||||||
->disableOriginalConstructor()
|
$logger = $this->createMock(ILogger::class);
|
||||||
->getMock();
|
|
||||||
|
|
||||||
//Only call the url generator once
|
//Only call the url generator once
|
||||||
$urlGenerator->expects($this->once())
|
$urlGenerator->expects($this->once())
|
||||||
|
|
Loading…
Reference in New Issue