Move to more generic image handling and add extra images

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2018-02-26 13:54:00 +01:00
parent 0bae516c66
commit 272b392cac
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
7 changed files with 192 additions and 179 deletions

View File

@ -39,8 +39,8 @@ return ['routes' => [
'verb' => 'POST' 'verb' => 'POST'
], ],
[ [
'name' => 'Theming#updateLogo', 'name' => 'Theming#uploadImage',
'url' => '/ajax/updateLogo', 'url' => '/ajax/uploadImage',
'verb' => 'POST' 'verb' => 'POST'
], ],
[ [
@ -49,13 +49,8 @@ return ['routes' => [
'verb' => 'GET', 'verb' => 'GET',
], ],
[ [
'name' => 'Theming#getLogo', 'name' => 'Theming#getImage',
'url' => '/logo', 'url' => '/image/{key}',
'verb' => 'GET',
],
[
'name' => 'Theming#getLoginBackground',
'url' => '/loginbackground',
'verb' => 'GET', 'verb' => 'GET',
], ],
[ [

View File

@ -33,21 +33,16 @@ use OCP\AppFramework\Http;
use OCP\AppFramework\Http\NotFoundResponse; use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\FileDisplayResponse;
use OCP\AppFramework\Http\DataDisplayResponse; use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Utility\ITimeFactory; use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
use OCP\IRequest; use OCP\IRequest;
use OCA\Theming\Util;
use OCP\IConfig;
class IconController extends Controller { class IconController extends Controller {
/** @var ThemingDefaults */ /** @var ThemingDefaults */
private $themingDefaults; private $themingDefaults;
/** @var Util */
private $util;
/** @var ITimeFactory */ /** @var ITimeFactory */
private $timeFactory; private $timeFactory;
/** @var IConfig */
private $config;
/** @var IconBuilder */ /** @var IconBuilder */
private $iconBuilder; private $iconBuilder;
/** @var ImageManager */ /** @var ImageManager */
@ -61,19 +56,16 @@ class IconController extends Controller {
* @param string $appName * @param string $appName
* @param IRequest $request * @param IRequest $request
* @param ThemingDefaults $themingDefaults * @param ThemingDefaults $themingDefaults
* @param Util $util
* @param ITimeFactory $timeFactory * @param ITimeFactory $timeFactory
* @param IConfig $config
* @param IconBuilder $iconBuilder * @param IconBuilder $iconBuilder
* @param ImageManager $imageManager * @param ImageManager $imageManager
* @param FileAccessHelper $fileAccessHelper
*/ */
public function __construct( public function __construct(
$appName, $appName,
IRequest $request, IRequest $request,
ThemingDefaults $themingDefaults, ThemingDefaults $themingDefaults,
Util $util,
ITimeFactory $timeFactory, ITimeFactory $timeFactory,
IConfig $config,
IconBuilder $iconBuilder, IconBuilder $iconBuilder,
ImageManager $imageManager, ImageManager $imageManager,
FileAccessHelper $fileAccessHelper FileAccessHelper $fileAccessHelper
@ -81,9 +73,7 @@ class IconController extends Controller {
parent::__construct($appName, $request); parent::__construct($appName, $request);
$this->themingDefaults = $themingDefaults; $this->themingDefaults = $themingDefaults;
$this->util = $util;
$this->timeFactory = $timeFactory; $this->timeFactory = $timeFactory;
$this->config = $config;
$this->iconBuilder = $iconBuilder; $this->iconBuilder = $iconBuilder;
$this->imageManager = $imageManager; $this->imageManager = $imageManager;
$this->fileAccessHelper = $fileAccessHelper; $this->fileAccessHelper = $fileAccessHelper;
@ -96,16 +86,17 @@ class IconController extends Controller {
* @param $app string app name * @param $app string app name
* @param $image string image file name (svg required) * @param $image string image file name (svg required)
* @return FileDisplayResponse|NotFoundResponse * @return FileDisplayResponse|NotFoundResponse
* @throws \Exception
*/ */
public function getThemedIcon($app, $image) { public function getThemedIcon(string $app, string $image): Response {
try { try {
$iconFile = $this->imageManager->getCachedImage("icon-" . $app . '-' . str_replace("/","_",$image)); $iconFile = $this->imageManager->getCachedImage('icon-' . $app . '-' . str_replace('/', '_',$image));
} catch (NotFoundException $exception) { } catch (NotFoundException $exception) {
$icon = $this->iconBuilder->colorSvg($app, $image); $icon = $this->iconBuilder->colorSvg($app, $image);
if ($icon === false || $icon === "") { if ($icon === false || $icon === '') {
return new NotFoundResponse(); 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) { if ($iconFile !== false) {
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']); $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('Expires', $expires->format(\DateTime::RFC2822));
$response->addHeader('Pragma', 'cache'); $response->addHeader('Pragma', 'cache');
return $response; return $response;
} else {
return new NotFoundResponse();
} }
return new NotFoundResponse();
} }
/** /**
@ -129,10 +120,17 @@ class IconController extends Controller {
* *
* @param $app string app name * @param $app string app name
* @return FileDisplayResponse|DataDisplayResponse * @return FileDisplayResponse|DataDisplayResponse
* @throws \Exception
*/ */
public function getFavicon($app = "core") { public function getFavicon(string $app = 'core'): Response {
$response = null; $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 { try {
$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app); $iconFile = $this->imageManager->getCachedImage('favIcon-' . $app);
} catch (NotFoundException $exception) { } catch (NotFoundException $exception) {
@ -164,9 +162,15 @@ class IconController extends Controller {
* *
* @param $app string app name * @param $app string app name
* @return FileDisplayResponse|NotFoundResponse * @return FileDisplayResponse|NotFoundResponse
* @throws \Exception
*/ */
public function getTouchIcon($app = "core") { public function getTouchIcon(string $app = 'core'): Response {
$response = null; $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()) { if ($this->themingDefaults->shouldReplaceIcons()) {
try { try {
$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app); $iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app);

View File

@ -33,6 +33,7 @@
namespace OCA\Theming\Controller; namespace OCA\Theming\Controller;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http; use OCP\AppFramework\Http;
@ -81,6 +82,8 @@ class ThemingController extends Controller {
private $urlGenerator; private $urlGenerator;
/** @var IAppManager */ /** @var IAppManager */
private $appManager; private $appManager;
/** @var ImageManager */
private $imageManager;
/** /**
* ThemingController constructor. * ThemingController constructor.
@ -110,7 +113,8 @@ class ThemingController extends Controller {
IAppData $appData, IAppData $appData,
SCSSCacher $scssCacher, SCSSCacher $scssCacher,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IAppManager $appManager IAppManager $appManager,
ImageManager $imageManager
) { ) {
parent::__construct($appName, $request); parent::__construct($appName, $request);
@ -124,13 +128,15 @@ class ThemingController extends Controller {
$this->scssCacher = $scssCacher; $this->scssCacher = $scssCacher;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->appManager = $appManager; $this->appManager = $appManager;
$this->imageManager = $imageManager;
} }
/** /**
* @param string $setting * @param string $setting
* @param string $value * @param string $value
* @return DataResponse * @return DataResponse
* @internal param string $color * @throws NotFoundException
* @throws NotPermittedException
*/ */
public function updateStylesheet($setting, $value) { public function updateStylesheet($setting, $value) {
$value = trim($value); $value = trim($value);
@ -195,27 +201,15 @@ class ThemingController extends Controller {
} }
/** /**
* Update the logos and background image
*
* @return DataResponse * @return DataResponse
* @throws NotPermittedException
*/ */
public function updateLogo() { public function uploadImage(): DataResponse {
$backgroundColor = $this->request->getParam('backgroundColor', false); // logo / background
if($backgroundColor) { // new: favicon logo-header
$this->themingDefaults->set('backgroundMime', 'backgroundColor'); //
return new DataResponse( $key = $this->request->getParam('key');
[ $image = $this->request->getUploadedFile('image');
'data' =>
[
'name' => 'backgroundColor',
'message' => $this->l10n->t('Saved')
],
'status' => 'success'
]
);
}
$newLogo = $this->request->getUploadedFile('uploadlogo');
$newBackgroundLogo = $this->request->getUploadedFile('upload-login-background');
$error = null; $error = null;
$phpFileUploadErrors = [ $phpFileUploadErrors = [
UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), 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_CANT_WRITE => $this->l10n->t('Could not write file to disk'),
UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), 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'); $error = $this->l10n->t('No file uploaded');
} }
if (!empty($newLogo) && array_key_exists('error', $newLogo) && $newLogo['error'] !== UPLOAD_ERR_OK) { if (!empty($image) && array_key_exists('error', $image) && $image['error'] !== UPLOAD_ERR_OK) {
$error = $phpFileUploadErrors[$newLogo['error']]; $error = $phpFileUploadErrors[$image['error']];
}
if (!empty($newBackgroundLogo) && array_key_exists('error', $newBackgroundLogo) && $newBackgroundLogo['error'] !== UPLOAD_ERR_OK) {
$error = $phpFileUploadErrors[$newBackgroundLogo['error']];
} }
if ($error !== null) { if ($error !== null) {
@ -256,61 +247,53 @@ class ThemingController extends Controller {
$folder = $this->appData->newFolder('images'); $folder = $this->appData->newFolder('images');
} }
if (!empty($newLogo)) { $target = $folder->newFile($key);
$target = $folder->newFile('logo'); $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'text/svg'];
$supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'text/svg']; if (!in_array($image['type'], $supportedFormats)) {
if (!in_array($newLogo['type'], $supportedFormats)) { return new DataResponse(
return new DataResponse( [
[ 'data' => [
'data' => [ 'message' => $this->l10n->t('Unsupported image type'),
'message' => $this->l10n->t('Unsupported image type'),
],
'status' => 'failure',
], ],
Http::STATUS_UNPROCESSABLE_ENTITY 'status' => 'failure',
); ],
} Http::STATUS_UNPROCESSABLE_ENTITY
$target->putContent(file_get_contents($newLogo['tmp_name'], 'r')); );
$this->themingDefaults->set('logoMime', $newLogo['type']);
$name = $newLogo['name'];
} }
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 // Optimize the image since some people may upload images that will be
// either to big or are not progressive rendering. // either to big or are not progressive rendering.
$tmpFile = $this->tempManager->getTemporaryFile(); $newImage = @imagecreatefromstring(file_get_contents($image['tmp_name'], 'r'));
$newWidth = imagesx($image) < 4096 ? imagesx($image) : 4096;
$newHeight = imagesy($image) / (imagesx($image) / $newWidth);
$image = imagescale($image, $newWidth, $newHeight);
imageinterlace($image, 1); $tmpFile = $this->tempManager->getTemporaryFile();
imagejpeg($image, $tmpFile, 75); $newWidth = imagesx($newImage) < 4096 ? imagesx($newImage) : 4096;
imagedestroy($image); $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')); $target->putContent(file_get_contents($tmpFile, 'r'));
$this->themingDefaults->set('backgroundMime', $newBackgroundLogo['type']); } else {
$name = $newBackgroundLogo['name']; $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( return new DataResponse(
[ [
'data' => 'data' =>
[ [
'name' => $name, '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' 'status' => 'success'
] ]
@ -322,23 +305,17 @@ class ThemingController extends Controller {
* *
* @param string $setting setting which should be reverted * @param string $setting setting which should be reverted
* @return DataResponse * @return DataResponse
* @throws NotPermittedException
*/ */
public function undo($setting) { public function undo(string $setting): DataResponse {
$value = $this->themingDefaults->undo($setting); $value = $this->themingDefaults->undo($setting);
// reprocess server scss for preview // reprocess server scss for preview
$cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/server.scss', 'core'); $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 { try {
$file = $this->appData->getFolder('images')->getFile('logo'); $file = $this->appData->getFolder('images')->getFile($imageKey);
$file->delete();
} catch (NotFoundException $e) {
} catch (NotPermittedException $e) {
}
}
if($setting === 'backgroundMime') {
try {
$file = $this->appData->getFolder('images')->getFile('background');
$file->delete(); $file->delete();
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
} catch (NotPermittedException $e) { } catch (NotPermittedException $e) {
@ -362,12 +339,14 @@ class ThemingController extends Controller {
* @PublicPage * @PublicPage
* @NoCSRFRequired * @NoCSRFRequired
* *
* @param string $key
* @return FileDisplayResponse|NotFoundResponse * @return FileDisplayResponse|NotFoundResponse
* @throws \Exception
*/ */
public function getLogo() { public function getImage(string $key) {
try { try {
/** @var File $file */ /** @var File $file */
$file = $this->appData->getFolder('images')->getFile('logo'); $file = $this->appData->getFolder('images')->getFile($key);
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
return new NotFoundResponse(); return new NotFoundResponse();
} }
@ -379,32 +358,7 @@ class ThemingController extends Controller {
$expires->add(new \DateInterval('PT24H')); $expires->add(new \DateInterval('PT24H'));
$response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); $response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
$response->addHeader('Pragma', 'cache'); $response->addHeader('Pragma', 'cache');
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, 'logoMime', '')); $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', ''));
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', ''));
return $response; return $response;
} }
@ -413,6 +367,9 @@ class ThemingController extends Controller {
* @PublicPage * @PublicPage
* *
* @return FileDisplayResponse|NotFoundResponse * @return FileDisplayResponse|NotFoundResponse
* @throws NotPermittedException
* @throws \Exception
* @throws \OCP\App\AppPathNotFoundException
*/ */
public function getStylesheet() { public function getStylesheet() {
$appPath = $this->appManager->getAppPath('theming'); $appPath = $this->appManager->getAppPath('theming');

View File

@ -24,11 +24,16 @@
namespace OCA\Theming; namespace OCA\Theming;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig; 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\IURLGenerator;
/**
* @property IURLGenerator urlGenerator
*/
class ImageManager { class ImageManager {
/** @var IConfig */ /** @var IConfig */
@ -36,27 +41,79 @@ class ImageManager {
/** @var IAppData */ /** @var IAppData */
private $appData; private $appData;
/** @var array */
private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
/** /**
* ImageManager constructor. * ImageManager constructor.
* *
* @param IConfig $config * @param IConfig $config
* @param IAppData $appData * @param IAppData $appData
* @param IURLGenerator $urlGenerator
*/ */
public function __construct(IConfig $config, public function __construct(IConfig $config,
IAppData $appData IAppData $appData,
IURLGenerator $urlGenerator
) { ) {
$this->config = $config; $this->config = $config;
$this->appData = $appData; $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 * Get folder for current theming files
* *
* @return \OCP\Files\SimpleFS\ISimpleFolder * @return ISimpleFolder
* @throws NotPermittedException * @throws NotPermittedException
* @throws \RuntimeException
*/ */
public function getCacheFolder() { public function getCacheFolder(): ISimpleFolder {
$cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0');
try { try {
$folder = $this->appData->getFolder($cacheBusterValue); $folder = $this->appData->getFolder($cacheBusterValue);
@ -73,8 +130,9 @@ class ImageManager {
* @param string $filename * @param string $filename
* @throws NotFoundException * @throws NotFoundException
* @return \OCP\Files\SimpleFS\ISimpleFile * @return \OCP\Files\SimpleFS\ISimpleFile
* @throws NotPermittedException
*/ */
public function getCachedImage($filename) { public function getCachedImage($filename): ISimpleFile {
$currentFolder = $this->getCacheFolder(); $currentFolder = $this->getCacheFolder();
return $currentFolder->getFile($filename); return $currentFolder->getFile($filename);
} }
@ -85,8 +143,10 @@ class ImageManager {
* @param string $filename * @param string $filename
* @param string $data * @param string $data
* @return \OCP\Files\SimpleFS\ISimpleFile * @return \OCP\Files\SimpleFS\ISimpleFile
* @throws NotFoundException
* @throws NotPermittedException
*/ */
public function setCachedImage($filename, $data) { public function setCachedImage($filename, $data): ISimpleFile {
$currentFolder = $this->getCacheFolder(); $currentFolder = $this->getCacheFolder();
if ($currentFolder->fileExists($filename)) { if ($currentFolder->fileExists($filename)) {
$file = $currentFolder->getFile($filename); $file = $currentFolder->getFile($filename);

View File

@ -29,6 +29,7 @@
namespace OCA\Theming\Settings; namespace OCA\Theming\Settings;
use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig; use OCP\IConfig;
@ -45,23 +46,25 @@ class Admin implements ISettings {
private $themingDefaults; private $themingDefaults;
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/** @var ImageManager */
private $imageManager;
public function __construct(IConfig $config, public function __construct(IConfig $config,
IL10N $l, IL10N $l,
ThemingDefaults $themingDefaults, ThemingDefaults $themingDefaults,
IURLGenerator $urlGenerator) { IURLGenerator $urlGenerator,
ImageManager $imageManager) {
$this->config = $config; $this->config = $config;
$this->l = $l; $this->l = $l;
$this->themingDefaults = $themingDefaults; $this->themingDefaults = $themingDefaults;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->imageManager = $imageManager;
} }
/** /**
* @return TemplateResponse * @return TemplateResponse
*/ */
public function getForm() { public function getForm(): TemplateResponse {
$path = $this->urlGenerator->linkToRoute('theming.Theming.updateLogo');
$themable = true; $themable = true;
$errorMessage = ''; $errorMessage = '';
$theme = $this->config->getSystemValue('theme', ''); $theme = $this->config->getSystemValue('theme', '');
@ -77,13 +80,10 @@ class Admin implements ISettings {
'url' => $this->themingDefaults->getBaseUrl(), 'url' => $this->themingDefaults->getBaseUrl(),
'slogan' => $this->themingDefaults->getSlogan(), 'slogan' => $this->themingDefaults->getSlogan(),
'color' => $this->themingDefaults->getColorPrimary(), 'color' => $this->themingDefaults->getColorPrimary(),
'logo' => $this->themingDefaults->getLogo(), 'uploadLogoRoute' => $this->urlGenerator->linkToRoute('theming.Theming.uploadImage'),
'logoMime' => $this->config->getAppValue('theming', 'logoMime', ''),
'background' => $this->themingDefaults->getBackground(),
'backgroundMime' => $this->config->getAppValue('theming', 'backgroundMime', ''),
'uploadLogoRoute' => $path,
'canThemeIcons' => $this->themingDefaults->shouldReplaceIcons(), '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, ''); return new TemplateResponse('theming', 'settings-admin', $parameters, '');
@ -92,7 +92,7 @@ class Admin implements ISettings {
/** /**
* @return string the section ID, e.g. 'sharing' * @return string the section ID, e.g. 'sharing'
*/ */
public function getSection() { public function getSection(): string {
return 'theming'; return 'theming';
} }
@ -103,7 +103,7 @@ class Admin implements ISettings {
* *
* E.g.: 70 * E.g.: 70
*/ */
public function getPriority() { public function getPriority(): int {
return 5; return 5;
} }

View File

@ -36,7 +36,6 @@ namespace OCA\Theming;
use OCP\App\AppPathNotFoundException; use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
@ -48,10 +47,10 @@ class ThemingDefaults extends \OC_Defaults {
private $config; private $config;
/** @var IL10N */ /** @var IL10N */
private $l; private $l;
/** @var ImageManager */
private $imageManager;
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/** @var IAppData */
private $appData;
/** @var ICacheFactory */ /** @var ICacheFactory */
private $cacheFactory; private $cacheFactory;
/** @var Util */ /** @var Util */
@ -83,9 +82,8 @@ class ThemingDefaults extends \OC_Defaults {
* *
* @param IConfig $config * @param IConfig $config
* @param IL10N $l * @param IL10N $l
* @param ImageManager $imageManager
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param \OC_Defaults $defaults
* @param IAppData $appData
* @param ICacheFactory $cacheFactory * @param ICacheFactory $cacheFactory
* @param Util $util * @param Util $util
* @param IAppManager $appManager * @param IAppManager $appManager
@ -93,16 +91,16 @@ class ThemingDefaults extends \OC_Defaults {
public function __construct(IConfig $config, public function __construct(IConfig $config,
IL10N $l, IL10N $l,
IURLGenerator $urlGenerator, IURLGenerator $urlGenerator,
IAppData $appData,
ICacheFactory $cacheFactory, ICacheFactory $cacheFactory,
Util $util, Util $util,
ImageManager $imageManager,
IAppManager $appManager IAppManager $appManager
) { ) {
parent::__construct(); parent::__construct();
$this->config = $config; $this->config = $config;
$this->l = $l; $this->l = $l;
$this->imageManager = $imageManager;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->appData = $appData;
$this->cacheFactory = $cacheFactory; $this->cacheFactory = $cacheFactory;
$this->util = $util; $this->util = $util;
$this->appManager = $appManager; $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 * @param bool $useSvg Whether to point to the SVG image or a fallback
* @return string * @return string
*/ */
public function getLogo($useSvg = true) { public function getLogo($useSvg = true): string {
$logo = $this->config->getAppValue('theming', 'logoMime', false); $logo = $this->config->getAppValue('theming', 'logoMime', false);
$logoExists = true; $logoExists = true;
try { try {
$this->appData->getFolder('images')->getFile('logo'); $this->imageManager->getImage('logo');
} catch (\Exception $e) { } catch (\Exception $e) {
$logoExists = false; $logoExists = false;
} }
@ -187,7 +185,7 @@ class ThemingDefaults extends \OC_Defaults {
return $logo . '?v=' . $cacheBusterCounter; 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 * @return string
*/ */
public function getBackground() { public function getBackground(): string {
$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0'); $this->imageManager->getImageUrl('background');
if($this->util->isBackgroundThemed()) {
return $this->urlGenerator->linkToRoute('theming.Theming.getLoginBackground') . '?v=' . $cacheBusterCounter;
}
return $this->urlGenerator->imagePath('core','background.png') . '?v=' . $cacheBusterCounter;
} }
/** /**
@ -238,12 +230,16 @@ class ThemingDefaults extends \OC_Defaults {
$variables = [ $variables = [
'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'", 'theming-cachebuster' => "'" . $this->config->getAppValue('theming', 'cachebuster', '0') . "'",
'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime', '') . "'", 'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
'theming-background-mime' => "'" . $this->config->getAppValue('theming', 'backgroundMime', '') . "'" '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-logo'] = "'".$this->imageManager->getImageUrl('logo')."'";
$variables['image-login-background'] = "'".$this->getBackground()."'"; $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'; $variables['image-login-plain'] = 'false';
if ($this->config->getAppValue('theming', 'color', null) !== null) { if ($this->config->getAppValue('theming', 'color', null) !== null) {

View File

@ -114,6 +114,7 @@ use OC\SystemTag\ManagerFactory as SystemTagManagerFactory;
use OC\Tagging\TagMapper; use OC\Tagging\TagMapper;
use OC\Template\JSCombiner; use OC\Template\JSCombiner;
use OC\Template\SCSSCacher; use OC\Template\SCSSCacher;
use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\App\IAppManager; use OCP\App\IAppManager;
@ -943,10 +944,10 @@ class Server extends ServerContainer implements IServerContainer {
$c->getConfig(), $c->getConfig(),
$c->getL10N('theming'), $c->getL10N('theming'),
$c->getURLGenerator(), $c->getURLGenerator(),
$c->getAppDataDir('theming'),
$c->getMemCacheFactory(), $c->getMemCacheFactory(),
new Util($c->getConfig(), $this->getAppManager(), $this->getAppDataDir('theming')), new Util($c->getConfig(), $this->getAppManager(), $c->getAppDataDir('theming')),
$this->getAppManager() new ImageManager($c->getConfig(), $c->getAppDataDir('theming'), $c->getURLGenerator()),
$c->getAppManager()
); );
} }
return new \OC_Defaults(); return new \OC_Defaults();