From 272b392cacdf34d2166105c91b0a06de3e51ed06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 26 Feb 2018 13:54:00 +0100 Subject: [PATCH] Move to more generic image handling and add extra images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- apps/theming/appinfo/routes.php | 13 +- .../theming/lib/Controller/IconController.php | 46 ++--- .../lib/Controller/ThemingController.php | 169 +++++++----------- apps/theming/lib/ImageManager.php | 72 +++++++- apps/theming/lib/Settings/Admin.php | 24 +-- apps/theming/lib/ThemingDefaults.php | 40 ++--- lib/private/Server.php | 7 +- 7 files changed, 192 insertions(+), 179 deletions(-) diff --git a/apps/theming/appinfo/routes.php b/apps/theming/appinfo/routes.php index a69ddc1a6c..f3483dbf99 100644 --- a/apps/theming/appinfo/routes.php +++ b/apps/theming/appinfo/routes.php @@ -39,8 +39,8 @@ return ['routes' => [ 'verb' => 'POST' ], [ - 'name' => 'Theming#updateLogo', - 'url' => '/ajax/updateLogo', + 'name' => 'Theming#uploadImage', + 'url' => '/ajax/uploadImage', 'verb' => 'POST' ], [ @@ -49,13 +49,8 @@ return ['routes' => [ 'verb' => 'GET', ], [ - 'name' => 'Theming#getLogo', - 'url' => '/logo', - 'verb' => 'GET', - ], - [ - 'name' => 'Theming#getLoginBackground', - 'url' => '/loginbackground', + 'name' => 'Theming#getImage', + 'url' => '/image/{key}', 'verb' => 'GET', ], [ diff --git a/apps/theming/lib/Controller/IconController.php b/apps/theming/lib/Controller/IconController.php index 7a5f76de6b..61df7b0b35 100644 --- a/apps/theming/lib/Controller/IconController.php +++ b/apps/theming/lib/Controller/IconController.php @@ -33,21 +33,16 @@ use OCP\AppFramework\Http; use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\DataDisplayResponse; +use OCP\AppFramework\Http\Response; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\NotFoundException; use OCP\IRequest; -use OCA\Theming\Util; -use OCP\IConfig; class IconController extends Controller { /** @var ThemingDefaults */ private $themingDefaults; - /** @var Util */ - private $util; /** @var ITimeFactory */ private $timeFactory; - /** @var IConfig */ - private $config; /** @var IconBuilder */ private $iconBuilder; /** @var ImageManager */ @@ -61,19 +56,16 @@ class IconController extends Controller { * @param string $appName * @param IRequest $request * @param ThemingDefaults $themingDefaults - * @param Util $util * @param ITimeFactory $timeFactory - * @param IConfig $config * @param IconBuilder $iconBuilder * @param ImageManager $imageManager + * @param FileAccessHelper $fileAccessHelper */ public function __construct( $appName, IRequest $request, ThemingDefaults $themingDefaults, - Util $util, ITimeFactory $timeFactory, - IConfig $config, IconBuilder $iconBuilder, ImageManager $imageManager, FileAccessHelper $fileAccessHelper @@ -81,9 +73,7 @@ class IconController extends Controller { parent::__construct($appName, $request); $this->themingDefaults = $themingDefaults; - $this->util = $util; $this->timeFactory = $timeFactory; - $this->config = $config; $this->iconBuilder = $iconBuilder; $this->imageManager = $imageManager; $this->fileAccessHelper = $fileAccessHelper; @@ -96,16 +86,17 @@ class IconController extends Controller { * @param $app string app name * @param $image string image file name (svg required) * @return FileDisplayResponse|NotFoundResponse + * @throws \Exception */ - public function getThemedIcon($app, $image) { + public function getThemedIcon(string $app, string $image): Response { try { - $iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image)); + $iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image)); } catch (NotFoundException $exception) { $icon = $this->iconBuilder->colorSvg($app, $image); - if ($icon === false || $icon === "") { + if ($icon === false || $icon === '') { return new NotFoundResponse(); } - $iconFile = $this->imageManager->setCachedImage("icon-" . $app . '-' . str_replace("/","_",$image), $icon); + $iconFile = $this->imageManager->setCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image), $icon); } if ($iconFile !== false) { $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); @@ -116,9 +107,9 @@ class IconController extends Controller { $response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); $response->addHeader('Pragma', 'cache'); return $response; - } else { - return new NotFoundResponse(); } + + return new NotFoundResponse(); } /** @@ -129,10 +120,17 @@ class IconController extends Controller { * * @param $app string app name * @return FileDisplayResponse|DataDisplayResponse + * @throws \Exception */ - public function getFavicon($app = "core") { + public function getFavicon(string $app = 'core'): Response { $response = null; - if ($this->themingDefaults->shouldReplaceIcons()) { + $iconFile = null; + try { + $iconFile = $this->imageManager->getImage('favicon'); + $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']); + } catch (NotFoundException $e) { + } + if ($iconFile === null && $this->themingDefaults->shouldReplaceIcons()) { try { $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app); } catch (NotFoundException $exception) { @@ -164,9 +162,15 @@ class IconController extends Controller { * * @param $app string app name * @return FileDisplayResponse|NotFoundResponse + * @throws \Exception */ - public function getTouchIcon($app = "core") { + public function getTouchIcon(string $app = 'core'): Response { $response = null; + try { + $iconFile = $this->imageManager->getImage('favicon'); + $response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']); + } catch (NotFoundException $e) { + } if ($this->themingDefaults->shouldReplaceIcons()) { try { $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app); diff --git a/apps/theming/lib/Controller/ThemingController.php b/apps/theming/lib/Controller/ThemingController.php index 47f806d16f..fc282f2088 100644 --- a/apps/theming/lib/Controller/ThemingController.php +++ b/apps/theming/lib/Controller/ThemingController.php @@ -33,6 +33,7 @@ namespace OCA\Theming\Controller; use OC\Template\SCSSCacher; +use OCA\Theming\ImageManager; use OCA\Theming\ThemingDefaults; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -81,6 +82,8 @@ class ThemingController extends Controller { private $urlGenerator; /** @var IAppManager */ private $appManager; + /** @var ImageManager */ + private $imageManager; /** * ThemingController constructor. @@ -110,7 +113,8 @@ class ThemingController extends Controller { IAppData $appData, SCSSCacher $scssCacher, IURLGenerator $urlGenerator, - IAppManager $appManager + IAppManager $appManager, + ImageManager $imageManager ) { parent::__construct($appName, $request); @@ -124,13 +128,15 @@ class ThemingController extends Controller { $this->scssCacher = $scssCacher; $this->urlGenerator = $urlGenerator; $this->appManager = $appManager; + $this->imageManager = $imageManager; } /** * @param string $setting * @param string $value * @return DataResponse - * @internal param string $color + * @throws NotFoundException + * @throws NotPermittedException */ public function updateStylesheet($setting, $value) { $value = trim($value); @@ -195,27 +201,15 @@ class ThemingController extends Controller { } /** - * Update the logos and background image - * * @return DataResponse + * @throws NotPermittedException */ - public function updateLogo() { - $backgroundColor = $this->request->getParam('backgroundColor', false); - if($backgroundColor) { - $this->themingDefaults->set('backgroundMime', 'backgroundColor'); - return new DataResponse( - [ - 'data' => - [ - 'name' => 'backgroundColor', - 'message' => $this->l10n->t('Saved') - ], - 'status' => 'success' - ] - ); - } - $newLogo = $this->request->getUploadedFile('uploadlogo'); - $newBackgroundLogo = $this->request->getUploadedFile('upload-login-background'); + public function uploadImage(): DataResponse { + // logo / background + // new: favicon logo-header + // + $key = $this->request->getParam('key'); + $image = $this->request->getUploadedFile('image'); $error = null; $phpFileUploadErrors = [ UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), @@ -227,14 +221,11 @@ class ThemingController extends Controller { UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), ]; - if (empty($newLogo) && empty($newBackgroundLogo)) { + if (empty($image)) { $error = $this->l10n->t('No file uploaded'); } - if (!empty($newLogo) && array_key_exists('error', $newLogo) && $newLogo['error'] !== UPLOAD_ERR_OK) { - $error = $phpFileUploadErrors[$newLogo['error']]; - } - if (!empty($newBackgroundLogo) && array_key_exists('error', $newBackgroundLogo) && $newBackgroundLogo['error'] !== UPLOAD_ERR_OK) { - $error = $phpFileUploadErrors[$newBackgroundLogo['error']]; + if (!empty($image) && array_key_exists('error', $image) && $image['error'] !== UPLOAD_ERR_OK) { + $error = $phpFileUploadErrors[$image['error']]; } if ($error !== null) { @@ -256,61 +247,53 @@ class ThemingController extends Controller { $folder = $this->appData->newFolder('images'); } - if (!empty($newLogo)) { - $target = $folder->newFile('logo'); - $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'text/svg']; - if (!in_array($newLogo['type'], $supportedFormats)) { - return new DataResponse( - [ - 'data' => [ - 'message' => $this->l10n->t('Unsupported image type'), - ], - 'status' => 'failure', + $target = $folder->newFile($key); + $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'text/svg']; + if (!in_array($image['type'], $supportedFormats)) { + return new DataResponse( + [ + 'data' => [ + 'message' => $this->l10n->t('Unsupported image type'), ], - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } - $target->putContent(file_get_contents($newLogo['tmp_name'], 'r')); - $this->themingDefaults->set('logoMime', $newLogo['type']); - $name = $newLogo['name']; + 'status' => 'failure', + ], + Http::STATUS_UNPROCESSABLE_ENTITY + ); } - if (!empty($newBackgroundLogo)) { - $target = $folder->newFile('background'); - $image = @imagecreatefromstring(file_get_contents($newBackgroundLogo['tmp_name'], 'r')); - if ($image === false) { - return new DataResponse( - [ - 'data' => [ - 'message' => $this->l10n->t('Unsupported image type'), - ], - 'status' => 'failure', - ], - Http::STATUS_UNPROCESSABLE_ENTITY - ); - } + $resizeKeys = ['background']; + if (in_array($key, $resizeKeys, true)) { // Optimize the image since some people may upload images that will be // either to big or are not progressive rendering. - $tmpFile = $this->tempManager->getTemporaryFile(); - $newWidth = imagesx($image) < 4096 ? imagesx($image) : 4096; - $newHeight = imagesy($image) / (imagesx($image) / $newWidth); - $image = imagescale($image, $newWidth, $newHeight); + $newImage = @imagecreatefromstring(file_get_contents($image['tmp_name'], 'r')); - imageinterlace($image, 1); - imagejpeg($image, $tmpFile, 75); - imagedestroy($image); + $tmpFile = $this->tempManager->getTemporaryFile(); + $newWidth = imagesx($newImage) < 4096 ? imagesx($newImage) : 4096; + $newHeight = imagesy($newImage) / (imagesx($newImage) / $newWidth); + $outputImage = imagescale($newImage, $newWidth, $newHeight); + + imageinterlace($outputImage, 1); + imagejpeg($outputImage, $tmpFile, 75); + imagedestroy($outputImage); $target->putContent(file_get_contents($tmpFile, 'r')); - $this->themingDefaults->set('backgroundMime', $newBackgroundLogo['type']); - $name = $newBackgroundLogo['name']; + } else { + $target->putContent(file_get_contents($image['tmp_name'], 'r')); } + $name = $image['name']; + + $this->themingDefaults->set($key.'Mime', $image['type']); + + $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/server.scss', 'core'); return new DataResponse( [ 'data' => [ 'name' => $name, - 'message' => $this->l10n->t('Saved') + 'url' => $this->imageManager->getImageUrl($key), + 'message' => $this->l10n->t('Saved'), + 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/server.scss')) ], 'status' => 'success' ] @@ -322,23 +305,17 @@ class ThemingController extends Controller { * * @param string $setting setting which should be reverted * @return DataResponse + * @throws NotPermittedException */ - public function undo($setting) { + public function undo(string $setting): DataResponse { $value = $this->themingDefaults->undo($setting); // reprocess server scss for preview $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/server.scss', 'core'); - if($setting === 'logoMime') { + if (strpos($setting, 'Mime') !== -1) { + $imageKey = str_replace('Mime', '', $setting); try { - $file = $this->appData->getFolder('images')->getFile('logo'); - $file->delete(); - } catch (NotFoundException $e) { - } catch (NotPermittedException $e) { - } - } - if($setting === 'backgroundMime') { - try { - $file = $this->appData->getFolder('images')->getFile('background'); + $file = $this->appData->getFolder('images')->getFile($imageKey); $file->delete(); } catch (NotFoundException $e) { } catch (NotPermittedException $e) { @@ -362,12 +339,14 @@ class ThemingController extends Controller { * @PublicPage * @NoCSRFRequired * + * @param string $key * @return FileDisplayResponse|NotFoundResponse + * @throws \Exception */ - public function getLogo() { + public function getImage(string $key) { try { /** @var File $file */ - $file = $this->appData->getFolder('images')->getFile('logo'); + $file = $this->appData->getFolder('images')->getFile($key); } catch (NotFoundException $e) { return new NotFoundResponse(); } @@ -379,32 +358,7 @@ class ThemingController extends Controller { $expires->add(new \DateInterval('PT24H')); $response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); $response->addHeader('Pragma', 'cache'); - $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'logoMime', '')); - return $response; - } - - /** - * @PublicPage - * @NoCSRFRequired - * - * @return FileDisplayResponse|NotFoundResponse - */ - public function getLoginBackground() { - try { - /** @var File $file */ - $file = $this->appData->getFolder('images')->getFile('background'); - } catch (NotFoundException $e) { - return new NotFoundResponse(); - } - - $response = new FileDisplayResponse($file); - $response->cacheFor(3600); - $expires = new \DateTime(); - $expires->setTimestamp($this->timeFactory->getTime()); - $expires->add(new \DateInterval('PT24H')); - $response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); - $response->addHeader('Pragma', 'cache'); - $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'backgroundMime', '')); + $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); return $response; } @@ -413,6 +367,9 @@ class ThemingController extends Controller { * @PublicPage * * @return FileDisplayResponse|NotFoundResponse + * @throws NotPermittedException + * @throws \Exception + * @throws \OCP\App\AppPathNotFoundException */ public function getStylesheet() { $appPath = $this->appManager->getAppPath('theming'); diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php index 14dba0d074..06cc37a3d1 100644 --- a/apps/theming/lib/ImageManager.php +++ b/apps/theming/lib/ImageManager.php @@ -24,11 +24,16 @@ namespace OCA\Theming; +use OCP\Files\SimpleFS\ISimpleFile; use OCP\IConfig; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; +use OCP\IURLGenerator; +/** + * @property IURLGenerator urlGenerator + */ class ImageManager { /** @var IConfig */ @@ -36,27 +41,79 @@ class ImageManager { /** @var IAppData */ private $appData; + /** @var array */ + private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon']; + /** * ImageManager constructor. * * @param IConfig $config * @param IAppData $appData + * @param IURLGenerator $urlGenerator */ public function __construct(IConfig $config, - IAppData $appData + IAppData $appData, + IURLGenerator $urlGenerator ) { $this->config = $config; $this->appData = $appData; + $this->urlGenerator = $urlGenerator; + } + + public function getImageUrl(string $key): string { + $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); + try { + $this->getImage($key); + return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $key ]) . '?v=' . $cacheBusterCounter; + } catch (NotFoundException $e) { + } + + switch ($key) { + case 'logo': + case 'logoheader': + case 'favicon': + return $this->urlGenerator->imagePath('core', 'logo.png') . '?v=' . $cacheBusterCounter; + case 'background': + return $this->urlGenerator->imagePath('core', 'background.png') . '?v=' . $cacheBusterCounter; + } + } + + public function getImageUrlAbsolute(string $key): string { + return $this->urlGenerator->getAbsoluteURL($this->getImageUrl($key)); + } + + /** + * @param $key + * @return ISimpleFile + * @throws NotFoundException + */ + public function getImage(string $key): ISimpleFile { + $logo = $this->config->getAppValue('theming', $key . 'Mime', false); + if ($logo === false) { + throw new NotFoundException(); + } + $folder = $this->appData->getFolder('images'); + return $folder->getFile($key); + } + + public function getCustomImages(): array { + $images = []; + foreach ($this->supportedImageKeys as $key) { + $images[$key] = [ + 'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''), + 'url' => $this->getImageUrl($key), + ]; + } + return $images; } /** * Get folder for current theming files * - * @return \OCP\Files\SimpleFS\ISimpleFolder + * @return ISimpleFolder * @throws NotPermittedException - * @throws \RuntimeException */ - public function getCacheFolder() { + public function getCacheFolder(): ISimpleFolder { $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); try { $folder = $this->appData->getFolder($cacheBusterValue); @@ -73,8 +130,9 @@ class ImageManager { * @param string $filename * @throws NotFoundException * @return \OCP\Files\SimpleFS\ISimpleFile + * @throws NotPermittedException */ - public function getCachedImage($filename) { + public function getCachedImage($filename): ISimpleFile { $currentFolder = $this->getCacheFolder(); return $currentFolder->getFile($filename); } @@ -85,8 +143,10 @@ class ImageManager { * @param string $filename * @param string $data * @return \OCP\Files\SimpleFS\ISimpleFile + * @throws NotFoundException + * @throws NotPermittedException */ - public function setCachedImage($filename, $data) { + public function setCachedImage($filename, $data): ISimpleFile { $currentFolder = $this->getCacheFolder(); if ($currentFolder->fileExists($filename)) { $file = $currentFolder->getFile($filename); diff --git a/apps/theming/lib/Settings/Admin.php b/apps/theming/lib/Settings/Admin.php index d26a568063..7c937f1979 100644 --- a/apps/theming/lib/Settings/Admin.php +++ b/apps/theming/lib/Settings/Admin.php @@ -29,6 +29,7 @@ namespace OCA\Theming\Settings; +use OCA\Theming\ImageManager; use OCA\Theming\ThemingDefaults; use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; @@ -45,23 +46,25 @@ class Admin implements ISettings { private $themingDefaults; /** @var IURLGenerator */ private $urlGenerator; + /** @var ImageManager */ + private $imageManager; public function __construct(IConfig $config, IL10N $l, ThemingDefaults $themingDefaults, - IURLGenerator $urlGenerator) { + IURLGenerator $urlGenerator, + ImageManager $imageManager) { $this->config = $config; $this->l = $l; $this->themingDefaults = $themingDefaults; $this->urlGenerator = $urlGenerator; + $this->imageManager = $imageManager; } /** * @return TemplateResponse */ - public function getForm() { - $path = $this->urlGenerator->linkToRoute('theming.Theming.updateLogo'); - + public function getForm(): TemplateResponse { $themable = true; $errorMessage = ''; $theme = $this->config->getSystemValue('theme', ''); @@ -77,13 +80,10 @@ class Admin implements ISettings { 'url' => $this->themingDefaults->getBaseUrl(), 'slogan' => $this->themingDefaults->getSlogan(), 'color' => $this->themingDefaults->getColorPrimary(), - 'logo' => $this->themingDefaults->getLogo(), - 'logoMime' => $this->config->getAppValue('theming', 'logoMime', ''), - 'background' => $this->themingDefaults->getBackground(), - 'backgroundMime' => $this->config->getAppValue('theming', 'backgroundMime', ''), - 'uploadLogoRoute' => $path, + 'uploadLogoRoute' => $this->urlGenerator->linkToRoute('theming.Theming.uploadImage'), 'canThemeIcons' => $this->themingDefaults->shouldReplaceIcons(), - 'iconDocs' => $this->urlGenerator->linkToDocs('admin-theming-icons') + 'iconDocs' => $this->urlGenerator->linkToDocs('admin-theming-icons'), + 'images' => $this->imageManager->getCustomImages(), ]; return new TemplateResponse('theming', 'settings-admin', $parameters, ''); @@ -92,7 +92,7 @@ class Admin implements ISettings { /** * @return string the section ID, e.g. 'sharing' */ - public function getSection() { + public function getSection(): string { return 'theming'; } @@ -103,7 +103,7 @@ class Admin implements ISettings { * * E.g.: 70 */ - public function getPriority() { + public function getPriority(): int { return 5; } diff --git a/apps/theming/lib/ThemingDefaults.php b/apps/theming/lib/ThemingDefaults.php index ce4ab0abb5..4043fa1a05 100644 --- a/apps/theming/lib/ThemingDefaults.php +++ b/apps/theming/lib/ThemingDefaults.php @@ -36,7 +36,6 @@ namespace OCA\Theming; use OCP\App\AppPathNotFoundException; use OCP\App\IAppManager; -use OCP\Files\IAppData; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IL10N; @@ -48,10 +47,10 @@ class ThemingDefaults extends \OC_Defaults { private $config; /** @var IL10N */ private $l; + /** @var ImageManager */ + private $imageManager; /** @var IURLGenerator */ private $urlGenerator; - /** @var IAppData */ - private $appData; /** @var ICacheFactory */ private $cacheFactory; /** @var Util */ @@ -83,9 +82,8 @@ class ThemingDefaults extends \OC_Defaults { * * @param IConfig $config * @param IL10N $l + * @param ImageManager $imageManager * @param IURLGenerator $urlGenerator - * @param \OC_Defaults $defaults - * @param IAppData $appData * @param ICacheFactory $cacheFactory * @param Util $util * @param IAppManager $appManager @@ -93,16 +91,16 @@ class ThemingDefaults extends \OC_Defaults { public function __construct(IConfig $config, IL10N $l, IURLGenerator $urlGenerator, - IAppData $appData, ICacheFactory $cacheFactory, Util $util, + ImageManager $imageManager, IAppManager $appManager ) { parent::__construct(); $this->config = $config; $this->l = $l; + $this->imageManager = $imageManager; $this->urlGenerator = $urlGenerator; - $this->appData = $appData; $this->cacheFactory = $cacheFactory; $this->util = $util; $this->appManager = $appManager; @@ -166,12 +164,12 @@ class ThemingDefaults extends \OC_Defaults { * @param bool $useSvg Whether to point to the SVG image or a fallback * @return string */ - public function getLogo($useSvg = true) { + public function getLogo($useSvg = true): string { $logo = $this->config->getAppValue('theming', 'logoMime', false); $logoExists = true; try { - $this->appData->getFolder('images')->getFile('logo'); + $this->imageManager->getImage('logo'); } catch (\Exception $e) { $logoExists = false; } @@ -187,7 +185,7 @@ class ThemingDefaults extends \OC_Defaults { return $logo . '?v=' . $cacheBusterCounter; } - return $this->urlGenerator->linkToRoute('theming.Theming.getLogo') . '?v=' . $cacheBusterCounter; + return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo' ]) . '?v=' . $cacheBusterCounter; } /** @@ -195,14 +193,8 @@ class ThemingDefaults extends \OC_Defaults { * * @return string */ - public function getBackground() { - $cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); - - if($this->util->isBackgroundThemed()) { - return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground') . '?v=' . $cacheBusterCounter; - } - - return $this->urlGenerator->imagePath('core','background.png') . '?v=' . $cacheBusterCounter; + public function getBackground(): string { + $this->imageManager->getImageUrl('background'); } /** @@ -238,12 +230,16 @@ class ThemingDefaults extends \OC_Defaults { $variables = [ 'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'", - 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime', '') . "'", - 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime', '') . "'" + 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'", + 'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime') . "'", + 'theming-logoheader-mime' => "'" . $this->config->getAppValue('theming', 'logoheaderMime') . "'", + 'theming-favicon-mime' => "'" . $this->config->getAppValue('theming', 'faviconMime') . "'" ]; - $variables['image-logo'] = "'".$this->getLogo()."'"; - $variables['image-login-background'] = "'".$this->getBackground()."'"; + $variables['image-logo'] = "'".$this->imageManager->getImageUrl('logo')."'"; + $variables['image-logoheader'] = "'".$this->imageManager->getImageUrl('logoheader')."'"; + $variables['image-favicon'] = "'".$this->imageManager->getImageUrl('favicon')."'"; + $variables['image-login-background'] = "'".$this->imageManager->getImageUrl('background')."'"; $variables['image-login-plain'] = 'false'; if ($this->config->getAppValue('theming', 'color', null) !== null) { diff --git a/lib/private/Server.php b/lib/private/Server.php index fd32b09033..3786486c2b 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -114,6 +114,7 @@ use OC\SystemTag\ManagerFactory as SystemTagManagerFactory; use OC\Tagging\TagMapper; use OC\Template\JSCombiner; use OC\Template\SCSSCacher; +use OCA\Theming\ImageManager; use OCA\Theming\ThemingDefaults; use OCP\App\IAppManager; @@ -943,10 +944,10 @@ class Server extends ServerContainer implements IServerContainer { $c->getConfig(), $c->getL10N('theming'), $c->getURLGenerator(), - $c->getAppDataDir('theming'), $c->getMemCacheFactory(), - new Util($c->getConfig(), $this->getAppManager(), $this->getAppDataDir('theming')), - $this->getAppManager() + new Util($c->getConfig(), $this->getAppManager(), $c->getAppDataDir('theming')), + new ImageManager($c->getConfig(), $c->getAppDataDir('theming'), $c->getURLGenerator()), + $c->getAppManager() ); } return new \OC_Defaults();