Generate PNG version of the theming app logo for mails

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-04-20 16:21:11 +02:00
parent 88f657b71c
commit 4a5826cddb
No known key found for this signature in database
GPG key ID: 4C614C6ED2CDE6DF
5 changed files with 69 additions and 22 deletions

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -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();
}
/**

View file

@ -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;

View file

@ -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()
);
}