diff options
author | Julius Härtl <jus@bitgrid.net> | 2018-04-20 16:21:11 +0200 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2018-06-05 16:47:38 +0200 |
commit | 4a5826cddb19c8a3cd8c112eea29d87630591293 (patch) | |
tree | c6dee5c4245b9dc068115478c8c2951e21cfd264 | |
parent | 88f657b71c9267f90c14665cabd790efb0da9949 (diff) | |
download | nextcloud-server-4a5826cddb19c8a3cd8c112eea29d87630591293.tar.gz nextcloud-server-4a5826cddb19c8a3cd8c112eea29d87630591293.zip |
Generate PNG version of the theming app logo for mails
Signed-off-by: Julius Härtl <jus@bitgrid.net>
-rw-r--r-- | apps/theming/lib/Controller/ThemingController.php | 11 | ||||
-rw-r--r-- | apps/theming/lib/ImageManager.php | 56 | ||||
-rw-r--r-- | apps/theming/lib/ThemingDefaults.php | 21 | ||||
-rw-r--r-- | apps/theming/tests/ImageManagerTest.php | 1 | ||||
-rw-r--r-- | lib/private/Server.php | 2 |
5 files changed, 69 insertions, 22 deletions
diff --git a/apps/theming/lib/Controller/ThemingController.php b/apps/theming/lib/Controller/ThemingController.php index c50e8a18460..f3a7f8cfcc5 100644 --- a/apps/theming/lib/Controller/ThemingController.php +++ b/apps/theming/lib/Controller/ThemingController.php @@ -267,6 +267,8 @@ class ThemingController extends Controller { $folder = $this->appData->newFolder('images'); } + $this->imageManager->delete($key); + $target = $folder->newFile($key); $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'image/svg']; $detectedMimeType = mime_content_type($image['tmp_name']); @@ -359,9 +361,9 @@ class ThemingController extends Controller { * @return FileDisplayResponse|NotFoundResponse * @throws \Exception */ - public function getImage(string $key) { + public function getImage(string $key, bool $asPng = false) { try { - $file = $this->imageManager->getImage($key); + $file = $this->imageManager->getImage($key, $asPng); } catch (NotFoundException $e) { return new NotFoundResponse(); } @@ -370,6 +372,11 @@ class ThemingController extends Controller { $response->cacheFor(3600); $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); $response->addHeader('Content-Disposition', 'attachment; filename="' . $key . '"'); + if ($asPng) { + $response->addHeader('Content-Type', 'image/png'); + } else { + $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); + } return $response; } diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php index 830ed7f34a9..693d2bbe1d4 100644 --- a/apps/theming/lib/ImageManager.php +++ b/apps/theming/lib/ImageManager.php @@ -26,6 +26,7 @@ namespace OCA\Theming; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; +use OCP\ICacheFactory; use OCP\IConfig; use OCP\Files\IAppData; use OCP\Files\NotFoundException; @@ -41,9 +42,12 @@ class ImageManager { private $config; /** @var IAppData */ private $appData; - + /** @var IURLGenerator */ + private $urlGenerator; /** @var array */ private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon']; + /** @var ICacheFactory */ + private $cacheFactory; /** * ImageManager constructor. @@ -51,14 +55,17 @@ class ImageManager { * @param IConfig $config * @param IAppData $appData * @param IURLGenerator $urlGenerator + * @param ThemingDefaults $themingDefaults */ public function __construct(IConfig $config, IAppData $appData, - IURLGenerator $urlGenerator + IURLGenerator $urlGenerator, + ICacheFactory $cacheFactory ) { $this->config = $config; $this->appData = $appData; $this->urlGenerator = $urlGenerator; + $this->cacheFactory = $cacheFactory; } public function getImageUrl(string $key): string { @@ -88,12 +95,28 @@ class ImageManager { * @return ISimpleFile * @throws NotFoundException */ - public function getImage(string $key): ISimpleFile { + public function getImage(string $key, bool $useSvg = false): ISimpleFile { $logo = $this->config->getAppValue('theming', $key . 'Mime', false); if ($logo === false) { throw new NotFoundException(); } $folder = $this->appData->getFolder('images'); + if (!$useSvg && $this->shouldReplaceIcons()) { + if (!$folder->fileExists($key . '.png')) { + try { + $finalIconFile = new \Imagick(); + $finalIconFile->setBackgroundColor('none'); + $finalIconFile->readImageBlob($folder->getFile($key)->getContent()); + $finalIconFile->setImageFormat('png32'); + $pngFile = $folder->newFile($key . '.png'); + $pngFile->putContent($finalIconFile->getImageBlob()); + } catch (\ImagickException $e) { + } + } else { + $pngFile = $folder->getFile($key . '.png'); + } + return $pngFile; + } return $folder->getFile($key); } @@ -165,6 +188,12 @@ class ImageManager { } catch (NotFoundException $e) { } catch (NotPermittedException $e) { } + try { + $file = $this->appData->getFolder('images')->getFile($key . '.png'); + $file->delete(); + } catch (NotFoundException $e) { + } catch (NotPermittedException $e) { + } } /** @@ -182,4 +211,25 @@ class ImageManager { } } } + + /** + * Check if Imagemagick is enabled and if SVG is supported + * otherwise we can't render custom icons + * + * @return bool + */ + public function shouldReplaceIcons() { + $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl()); + if($value = $cache->get('shouldReplaceIcons')) { + return (bool)$value; + } + $value = false; + if(extension_loaded('imagick')) { + if (count(\Imagick::queryFormats('SVG')) >= 1) { + $value = true; + } + } + $cache->set('shouldReplaceIcons', $value); + return $value; + } } diff --git a/apps/theming/lib/ThemingDefaults.php b/apps/theming/lib/ThemingDefaults.php index 00c47676bc8..a46676a43a9 100644 --- a/apps/theming/lib/ThemingDefaults.php +++ b/apps/theming/lib/ThemingDefaults.php @@ -205,7 +205,7 @@ class ThemingDefaults extends \OC_Defaults { $logoExists = true; try { - $this->imageManager->getImage('logo'); + $this->imageManager->getImage('logo', $useSvg); } catch (\Exception $e) { $logoExists = false; } @@ -221,7 +221,7 @@ class ThemingDefaults extends \OC_Defaults { return $logo . '?v=' . $cacheBusterCounter; } - return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo' ]) . '?v=' . $cacheBusterCounter; + return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]); } /** @@ -339,23 +339,12 @@ class ThemingDefaults extends \OC_Defaults { * Check if Imagemagick is enabled and if SVG is supported * otherwise we can't render custom icons * + * TODO: move to usage of image manager + * * @return bool */ public function shouldReplaceIcons() { - $cache = $this->cacheFactory->createDistributed('theming-' . $this->urlGenerator->getBaseUrl()); - if($value = $cache->get('shouldReplaceIcons')) { - return (bool)$value; - } - $value = false; - if(extension_loaded('imagick')) { - $checkImagick = new \Imagick(); - if (count($checkImagick->queryFormats('SVG')) >= 1) { - $value = true; - } - $checkImagick->clear(); - } - $cache->set('shouldReplaceIcons', $value); - return $value; + return $this->imageManager->shouldReplaceIcons(); } /** diff --git a/apps/theming/tests/ImageManagerTest.php b/apps/theming/tests/ImageManagerTest.php index 4e258ce7162..3993efc1c52 100644 --- a/apps/theming/tests/ImageManagerTest.php +++ b/apps/theming/tests/ImageManagerTest.php @@ -24,6 +24,7 @@ namespace OCA\Theming\Tests; use OCA\Theming\ImageManager; +use OCA\Theming\ThemingDefaults; use OCP\Files\SimpleFS\ISimpleFile; use OCP\IConfig; use OCP\IURLGenerator; diff --git a/lib/private/Server.php b/lib/private/Server.php index f230ce5b884..15fb06a2e03 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -954,7 +954,7 @@ class Server extends ServerContainer implements IServerContainer { $c->getURLGenerator(), $c->getMemCacheFactory(), new Util($c->getConfig(), $this->getAppManager(), $c->getAppDataDir('theming')), - new ImageManager($c->getConfig(), $c->getAppDataDir('theming'), $c->getURLGenerator()), + new ImageManager($c->getConfig(), $c->getAppDataDir('theming'), $c->getURLGenerator(), $this->getMemCacheFactory()), $c->getAppManager() ); } |