diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2019-12-08 17:20:50 +0100 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2019-12-09 12:09:43 +0100 |
commit | 64aba49461114b986952ece89ea9467618a0ab19 (patch) | |
tree | 7db03718f589f8e610836f3221d5a98cd8c1064c /lib | |
parent | 8bc4295cfad1ba65bad3c7c06af1f4a16b9bcba5 (diff) | |
download | nextcloud-server-64aba49461114b986952ece89ea9467618a0ab19.tar.gz nextcloud-server-64aba49461114b986952ece89ea9467618a0ab19.zip |
Ensure that we don't merge broken json.
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Files/Type/Detection.php | 31 | ||||
-rw-r--r-- | lib/private/Server.php | 1 |
2 files changed, 26 insertions, 6 deletions
diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php index 20758a71f87..f58431efee5 100644 --- a/lib/private/Files/Type/Detection.php +++ b/lib/private/Files/Type/Detection.php @@ -35,6 +35,7 @@ namespace OC\Files\Type; use OCP\Files\IMimeTypeDetector; +use OCP\ILogger; use OCP\IURLGenerator; /** @@ -45,6 +46,10 @@ use OCP\IURLGenerator; * @package OC\Files\Type */ class Detection implements IMimeTypeDetector { + + public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json'; + public const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json'; + protected $mimetypes = []; protected $secureMimeTypes = []; @@ -55,6 +60,9 @@ class Detection implements IMimeTypeDetector { /** @var IURLGenerator */ private $urlGenerator; + /** @var ILogger */ + private $logger; + /** @var string */ private $customConfigDir; @@ -63,13 +71,16 @@ class Detection implements IMimeTypeDetector { /** * @param IURLGenerator $urlGenerator + * @param ILogger $logger * @param string $customConfigDir * @param string $defaultConfigDir */ public function __construct(IURLGenerator $urlGenerator, + ILogger $logger, $customConfigDir, $defaultConfigDir) { $this->urlGenerator = $urlGenerator; + $this->logger = $logger; $this->customConfigDir = $customConfigDir; $this->defaultConfigDir = $defaultConfigDir; } @@ -120,9 +131,13 @@ class Detection implements IMimeTypeDetector { $this->mimeTypeAlias = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypealiases.dist.json'), true); - if (file_exists($this->customConfigDir . '/mimetypealiases.json')) { - $custom = json_decode(file_get_contents($this->customConfigDir . '/mimetypealiases.json'), true); - $this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom); + 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()); + } } } @@ -151,9 +166,13 @@ 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 . '/mimetypemapping.json')) { - $custom = json_decode(file_get_contents($this->customConfigDir . '/mimetypemapping.json'), true); - $mimetypeMapping = array_merge($mimetypeMapping, $custom); + 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); diff --git a/lib/private/Server.php b/lib/private/Server.php index d16b55ac215..dcac72369e4 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -882,6 +882,7 @@ class Server extends ServerContainer implements IServerContainer { $this->registerService(\OCP\Files\IMimeTypeDetector::class, function (Server $c) { return new \OC\Files\Type\Detection( $c->getURLGenerator(), + $c->getLogger(), \OC::$configDir, \OC::$SERVERROOT . '/resources/config/' ); |