Ensure that we don't merge broken json.
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
This commit is contained in:
parent
8bc4295cfa
commit
64aba49461
|
@ -35,6 +35,7 @@
|
||||||
namespace OC\Files\Type;
|
namespace OC\Files\Type;
|
||||||
|
|
||||||
use OCP\Files\IMimeTypeDetector;
|
use OCP\Files\IMimeTypeDetector;
|
||||||
|
use OCP\ILogger;
|
||||||
use OCP\IURLGenerator;
|
use OCP\IURLGenerator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,6 +46,10 @@ use OCP\IURLGenerator;
|
||||||
* @package OC\Files\Type
|
* @package OC\Files\Type
|
||||||
*/
|
*/
|
||||||
class Detection implements IMimeTypeDetector {
|
class Detection implements IMimeTypeDetector {
|
||||||
|
|
||||||
|
public const CUSTOM_MIMETYPEMAPPING = 'mimetypemapping.json';
|
||||||
|
public const CUSTOM_MIMETYPEALIASES = 'mimetypealiases.json';
|
||||||
|
|
||||||
protected $mimetypes = [];
|
protected $mimetypes = [];
|
||||||
protected $secureMimeTypes = [];
|
protected $secureMimeTypes = [];
|
||||||
|
|
||||||
|
@ -55,6 +60,9 @@ class Detection implements IMimeTypeDetector {
|
||||||
/** @var IURLGenerator */
|
/** @var IURLGenerator */
|
||||||
private $urlGenerator;
|
private $urlGenerator;
|
||||||
|
|
||||||
|
/** @var ILogger */
|
||||||
|
private $logger;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $customConfigDir;
|
private $customConfigDir;
|
||||||
|
|
||||||
|
@ -63,13 +71,16 @@ class Detection implements IMimeTypeDetector {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param IURLGenerator $urlGenerator
|
* @param IURLGenerator $urlGenerator
|
||||||
|
* @param ILogger $logger
|
||||||
* @param string $customConfigDir
|
* @param string $customConfigDir
|
||||||
* @param string $defaultConfigDir
|
* @param string $defaultConfigDir
|
||||||
*/
|
*/
|
||||||
public function __construct(IURLGenerator $urlGenerator,
|
public function __construct(IURLGenerator $urlGenerator,
|
||||||
|
ILogger $logger,
|
||||||
$customConfigDir,
|
$customConfigDir,
|
||||||
$defaultConfigDir) {
|
$defaultConfigDir) {
|
||||||
$this->urlGenerator = $urlGenerator;
|
$this->urlGenerator = $urlGenerator;
|
||||||
|
$this->logger = $logger;
|
||||||
$this->customConfigDir = $customConfigDir;
|
$this->customConfigDir = $customConfigDir;
|
||||||
$this->defaultConfigDir = $defaultConfigDir;
|
$this->defaultConfigDir = $defaultConfigDir;
|
||||||
}
|
}
|
||||||
|
@ -120,9 +131,13 @@ 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);
|
||||||
|
|
||||||
if (file_exists($this->customConfigDir . '/mimetypealiases.json')) {
|
if (file_exists($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEALIASES)) {
|
||||||
$custom = json_decode(file_get_contents($this->customConfigDir . '/mimetypealiases.json'), true);
|
$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);
|
$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);
|
$mimetypeMapping = json_decode(file_get_contents($this->defaultConfigDir . '/mimetypemapping.dist.json'), true);
|
||||||
|
|
||||||
//Check if need to load custom mappings
|
//Check if need to load custom mappings
|
||||||
if (file_exists($this->customConfigDir . '/mimetypemapping.json')) {
|
if (file_exists($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEMAPPING)) {
|
||||||
$custom = json_decode(file_get_contents($this->customConfigDir . '/mimetypemapping.json'), true);
|
$custom = json_decode(file_get_contents($this->customConfigDir . '/' . self::CUSTOM_MIMETYPEMAPPING), true);
|
||||||
|
if (json_last_error() === JSON_ERROR_NONE) {
|
||||||
$mimetypeMapping = array_merge($mimetypeMapping, $custom);
|
$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);
|
||||||
|
|
|
@ -882,6 +882,7 @@ class Server extends ServerContainer implements IServerContainer {
|
||||||
$this->registerService(\OCP\Files\IMimeTypeDetector::class, function (Server $c) {
|
$this->registerService(\OCP\Files\IMimeTypeDetector::class, function (Server $c) {
|
||||||
return new \OC\Files\Type\Detection(
|
return new \OC\Files\Type\Detection(
|
||||||
$c->getURLGenerator(),
|
$c->getURLGenerator(),
|
||||||
|
$c->getLogger(),
|
||||||
\OC::$configDir,
|
\OC::$configDir,
|
||||||
\OC::$SERVERROOT . '/resources/config/'
|
\OC::$SERVERROOT . '/resources/config/'
|
||||||
);
|
);
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
namespace Test\Files\Type;
|
namespace Test\Files\Type;
|
||||||
|
|
||||||
use OC\Files\Type\Detection;
|
use OC\Files\Type\Detection;
|
||||||
|
use OCP\ILogger;
|
||||||
use OCP\IURLGenerator;
|
use OCP\IURLGenerator;
|
||||||
|
|
||||||
class DetectionTest extends \Test\TestCase {
|
class DetectionTest extends \Test\TestCase {
|
||||||
|
@ -32,6 +33,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
$this->detection = new Detection(
|
$this->detection = new Detection(
|
||||||
\OC::$server->getURLGenerator(),
|
\OC::$server->getURLGenerator(),
|
||||||
|
\OC::$server->getLogger(),
|
||||||
\OC::$SERVERROOT . '/config/',
|
\OC::$SERVERROOT . '/config/',
|
||||||
\OC::$SERVERROOT . '/resources/config/'
|
\OC::$SERVERROOT . '/resources/config/'
|
||||||
);
|
);
|
||||||
|
@ -114,13 +116,17 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
->getMock();
|
->getMock();
|
||||||
|
|
||||||
|
$logger = $this->getMockBuilder(ILogger::class)
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
|
||||||
//Only call the url generator once
|
//Only call the url generator once
|
||||||
$urlGenerator->expects($this->once())
|
$urlGenerator->expects($this->once())
|
||||||
->method('imagePath')
|
->method('imagePath')
|
||||||
->with($this->equalTo('core'), $this->equalTo('filetypes/folder.png'))
|
->with($this->equalTo('core'), $this->equalTo('filetypes/folder.png'))
|
||||||
->willReturn('folder.svg');
|
->willReturn('folder.svg');
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('dir');
|
$mimeType = $detection->mimeTypeIcon('dir');
|
||||||
$this->assertEquals('folder.svg', $mimeType);
|
$this->assertEquals('folder.svg', $mimeType);
|
||||||
|
|
||||||
|
@ -139,7 +145,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->with($this->equalTo('core'), $this->equalTo('filetypes/folder-shared.png'))
|
->with($this->equalTo('core'), $this->equalTo('filetypes/folder-shared.png'))
|
||||||
->willReturn('folder-shared.svg');
|
->willReturn('folder-shared.svg');
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('dir-shared');
|
$mimeType = $detection->mimeTypeIcon('dir-shared');
|
||||||
$this->assertEquals('folder-shared.svg', $mimeType);
|
$this->assertEquals('folder-shared.svg', $mimeType);
|
||||||
|
|
||||||
|
@ -159,7 +165,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->with($this->equalTo('core'), $this->equalTo('filetypes/folder-external.png'))
|
->with($this->equalTo('core'), $this->equalTo('filetypes/folder-external.png'))
|
||||||
->willReturn('folder-external.svg');
|
->willReturn('folder-external.svg');
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('dir-external');
|
$mimeType = $detection->mimeTypeIcon('dir-external');
|
||||||
$this->assertEquals('folder-external.svg', $mimeType);
|
$this->assertEquals('folder-external.svg', $mimeType);
|
||||||
|
|
||||||
|
@ -179,7 +185,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->with($this->equalTo('core'), $this->equalTo('filetypes/my-type.png'))
|
->with($this->equalTo('core'), $this->equalTo('filetypes/my-type.png'))
|
||||||
->willReturn('my-type.svg');
|
->willReturn('my-type.svg');
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('my-type');
|
$mimeType = $detection->mimeTypeIcon('my-type');
|
||||||
$this->assertEquals('my-type.svg', $mimeType);
|
$this->assertEquals('my-type.svg', $mimeType);
|
||||||
|
|
||||||
|
@ -209,7 +215,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('my-type');
|
$mimeType = $detection->mimeTypeIcon('my-type');
|
||||||
$this->assertEquals('my.svg', $mimeType);
|
$this->assertEquals('my.svg', $mimeType);
|
||||||
|
|
||||||
|
@ -240,7 +246,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
||||||
$this->assertEquals('file.svg', $mimeType);
|
$this->assertEquals('file.svg', $mimeType);
|
||||||
|
|
||||||
|
@ -259,7 +265,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->with($this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png'))
|
->with($this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png'))
|
||||||
->willReturn('foo-bar.svg');
|
->willReturn('foo-bar.svg');
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
||||||
$this->assertEquals('foo-bar.svg', $mimeType);
|
$this->assertEquals('foo-bar.svg', $mimeType);
|
||||||
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
$mimeType = $detection->mimeTypeIcon('foo-bar');
|
||||||
|
@ -285,7 +291,7 @@ class DetectionTest extends \Test\TestCase {
|
||||||
->with($this->equalTo('core'), $this->equalTo('filetypes/foobar-baz.png'))
|
->with($this->equalTo('core'), $this->equalTo('filetypes/foobar-baz.png'))
|
||||||
->willReturn('foobar-baz.svg');
|
->willReturn('foobar-baz.svg');
|
||||||
|
|
||||||
$detection = new Detection($urlGenerator, $confDir->url(), $confDir->url());
|
$detection = new Detection($urlGenerator, $logger, $confDir->url(), $confDir->url());
|
||||||
$mimeType = $detection->mimeTypeIcon('foo');
|
$mimeType = $detection->mimeTypeIcon('foo');
|
||||||
$this->assertEquals('foobar-baz.svg', $mimeType);
|
$this->assertEquals('foobar-baz.svg', $mimeType);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue