Add info message if an exception occurs during the svg to png conversion

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-06-14 16:55:44 +02:00
parent 5b0ce806a3
commit ceee91d9d4
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
3 changed files with 19 additions and 3 deletions

View File

@ -31,6 +31,7 @@ use OCP\IConfig;
use OCP\Files\IAppData; use OCP\Files\IAppData;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
class ImageManager { class ImageManager {
@ -45,6 +46,8 @@ class ImageManager {
private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon']; private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
/** @var ICacheFactory */ /** @var ICacheFactory */
private $cacheFactory; private $cacheFactory;
/** @var ILogger */
private $logger;
/** /**
* ImageManager constructor. * ImageManager constructor.
@ -53,16 +56,19 @@ class ImageManager {
* @param IAppData $appData * @param IAppData $appData
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param ICacheFactory $cacheFactory * @param ICacheFactory $cacheFactory
* @param ILogger $logger
*/ */
public function __construct(IConfig $config, public function __construct(IConfig $config,
IAppData $appData, IAppData $appData,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
ICacheFactory $cacheFactory ICacheFactory $cacheFactory,
ILogger $logger
) { ) {
$this->config = $config; $this->config = $config;
$this->appData = $appData; $this->appData = $appData;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->cacheFactory = $cacheFactory; $this->cacheFactory = $cacheFactory;
$this->logger = $logger;
} }
public function getImageUrl(string $key, bool $useSvg = true): string { public function getImageUrl(string $key, bool $useSvg = true): string {
@ -95,6 +101,7 @@ class ImageManager {
* @throws NotPermittedException * @throws NotPermittedException
*/ */
public function getImage(string $key, bool $useSvg = true): ISimpleFile { public function getImage(string $key, bool $useSvg = true): ISimpleFile {
$pngFile = null;
$logo = $this->config->getAppValue('theming', $key . 'Mime', false); $logo = $this->config->getAppValue('theming', $key . 'Mime', false);
$folder = $this->appData->getFolder('images'); $folder = $this->appData->getFolder('images');
if ($logo === false || !$folder->fileExists($key)) { if ($logo === false || !$folder->fileExists($key)) {
@ -110,10 +117,14 @@ class ImageManager {
$pngFile = $folder->newFile($key . '.png'); $pngFile = $folder->newFile($key . '.png');
$pngFile->putContent($finalIconFile->getImageBlob()); $pngFile->putContent($finalIconFile->getImageBlob());
} catch (\ImagickException $e) { } catch (\ImagickException $e) {
$this->logger->info('The image was requested to be no SVG file, but converting it to PNG failed.', $e->getMessage());
$pngFile = null;
} }
} else { } else {
$pngFile = $folder->getFile($key . '.png'); $pngFile = $folder->getFile($key . '.png');
} }
}
if ($pngFile !== null) {
return $pngFile; return $pngFile;
} }
return $folder->getFile($key); return $folder->getFile($key);

View File

@ -28,6 +28,7 @@ use OCA\Theming\ThemingDefaults;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use Test\TestCase; use Test\TestCase;
use OCP\Files\SimpleFS\ISimpleFolder; use OCP\Files\SimpleFS\ISimpleFolder;
@ -46,6 +47,8 @@ class ImageManagerTest extends TestCase {
private $urlGenerator; private $urlGenerator;
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */ /** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
private $cacheFactory; private $cacheFactory;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
@ -53,11 +56,13 @@ class ImageManagerTest extends TestCase {
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class); $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->logger = $this->createMock(ILogger::class);
$this->imageManager = new ImageManager( $this->imageManager = new ImageManager(
$this->config, $this->config,
$this->appData, $this->appData,
$this->urlGenerator, $this->urlGenerator,
$this->cacheFactory $this->cacheFactory,
$this->logger
); );
} }

View File

@ -954,7 +954,7 @@ class Server extends ServerContainer implements IServerContainer {
$c->getURLGenerator(), $c->getURLGenerator(),
$c->getMemCacheFactory(), $c->getMemCacheFactory(),
new Util($c->getConfig(), $this->getAppManager(), $c->getAppDataDir('theming')), new Util($c->getConfig(), $this->getAppManager(), $c->getAppDataDir('theming')),
new ImageManager($c->getConfig(), $c->getAppDataDir('theming'), $c->getURLGenerator(), $this->getMemCacheFactory()), new ImageManager($c->getConfig(), $c->getAppDataDir('theming'), $c->getURLGenerator(), $this->getMemCacheFactory(), $this->getLogger()),
$c->getAppManager() $c->getAppManager()
); );
} }