Move custom definition logic into method

Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
Daniel Kesselberg 2019-12-08 22:16:25 +01:00
parent 64aba49461
commit 29575c4d36
No known key found for this signature in database
GPG Key ID: 36E3664E099D0614
2 changed files with 18 additions and 24 deletions

View File

@ -47,8 +47,8 @@ use OCP\IURLGenerator;
*/
class Detection implements IMimeTypeDetector {
public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
public const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
private const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
private const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
protected $mimetypes = [];
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
*/
@ -130,15 +142,7 @@ class Detection implements IMimeTypeDetector {
}
$this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true);
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());
}
}
$this->mimeTypeAlias = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEALIASES, $this->mimeTypeAlias);
}
/**
@ -164,16 +168,7 @@ class Detection implements IMimeTypeDetector {
}
$mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
//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());
}
}
$mimetypeMapping = $this->loadCustomDefinitions(self::CUSTOM_MIMETYPEMAPPING, $mimetypeMapping);
$this->registerTypeArray($mimetypeMapping);
}

View File

@ -116,9 +116,8 @@ class DetectionTest extends \Test\TestCase {
->disableOriginalConstructor()
->getMock();
$logger = $this->getMockBuilder(ILogger::class)
->disableOriginalConstructor()
->getMock();
/** @var ILogger $logger */
$logger = $this->createMock(ILogger::class);
//Only call the url generator once
$urlGenerator->expects($this->once())