Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Email Logo #26871

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/theming/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function getCapabilities() {
'color-element-bright' => $this->util->elementColor($color),
'color-element-dark' => $this->util->elementColor($color, false),
'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
'emailLogo' => $this->url->getAbsoluteURL($this->theming->getLogo(false, true)),
'background' => $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $this->theming->getColorPrimary() !== '#0082c9') ?
$this->theming->getColorPrimary() :
$this->url->getAbsoluteURL($this->theming->getBackground()),
Expand Down
2 changes: 1 addition & 1 deletion apps/theming/lib/Command/UpdateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UpdateConfig extends Command {
];

public const SUPPORTED_IMAGE_KEYS = [
'background', 'logo', 'favicon', 'logoheader'
'background', 'logo', 'emailLogo', 'favicon', 'logoheader'
];

private $themingDefaults;
Expand Down
3 changes: 2 additions & 1 deletion apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ImageManager {
/** @var IURLGenerator */
private $urlGenerator;
/** @var array */
private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
private $supportedImageKeys = ['background', 'logo', 'emailLogo', 'logoheader', 'favicon'];
/** @var ICacheFactory */
private $cacheFactory;
/** @var ILogger */
Expand Down Expand Up @@ -86,6 +86,7 @@ public function getImageUrl(string $key, bool $useSvg = true): string {

switch ($key) {
case 'logo':
case 'emailLogo':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since svg is causing issues with email it would make sense to not allow to upload it then for an email logo:

https://github.com/nextcloud/server/pull/26871/files#diff-b9d526f6249d406c08009d9de5802b5637194bb66ec759df798dd397f9a422beR222-R225

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you're absolutely right! The refactored version (see my other comment below) has no support for an SVG version.

case 'logoheader':
case 'favicon':
return $this->urlGenerator->imagePath('core', 'logo/logo.png') . '?v=' . $cacheBusterCounter;
Expand Down
26 changes: 20 additions & 6 deletions apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,10 @@ public function getColorPrimary() {
* @param bool $useSvg Whether to point to the SVG image or a fallback
* @return string
*/
public function getLogo($useSvg = true): string {
$logo = $this->config->getAppValue('theming', 'logoMime', '');
public function getLogo($useSvg = true, $emailLogo = false): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned with just adding another argument here so since this is a totally different logo file that can be uploaded I'd rather go with a separate method also in OCP\Defaults like getMailLogo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That a very good point, thanks for bringing it up! I was actually thinking about that at first, but was afraid to get complains with regards to code duplication. However, having done the refactor now it feels much, much better and is totally worth a little duplication imho. Especially with regards to your other comment about the SVG - the new getEmailLogo doesn't have to be concerned with SVGs now. I'm just not sure how to restrict the email logo upload field to certain mime-types... 🤔 Any idea?

$logoKey = $emailLogo ? 'emailLogo' : 'logo';
$logo = $this->config->getAppValue('theming', $logoKey . 'Mime', '');
$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');

// short cut to avoid setting up the filesystem just to check if the logo is there
//
Expand All @@ -235,14 +237,23 @@ public function getLogo($useSvg = true): string {
$logoExists = true;
} else {
try {
$this->imageManager->getImage('logo', $useSvg);
$this->imageManager->getImage($logoKey, $useSvg);
$logoExists = true;
} catch (\Exception $e) {
$logoExists = false;
}
}

$cacheBusterCounter = $this->config->getAppValue('theming', 'cachebuster', '0');
if (!$logoExists) {
try {
$logoKey = 'logo';
$logo = $this->config->getAppValue('theming', $logoKey . 'Mime', '');
$this->imageManager->getImage($logoKey, $useSvg);
$logoExists = true;
} catch (\Exception $e) {
$logoExists = false;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit more comments on the code please? :)
Maybe something like

Fallback to logo if there is no emailLogo registered

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new refactor based on @juliushaertl suggestion to extract it into a new getEmailLogo function greatly simplified the code, aligning it with the original getLogo function, so I think no additional comments are needed there. You're right though, I should've added some comments in this case. 👌

}

if (!$logo || !$logoExists) {
if ($useSvg) {
Expand All @@ -253,7 +264,7 @@ public function getLogo($useSvg = true): string {
return $logo . '?v=' . $cacheBusterCounter;
}

return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => 'logo', 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
return $this->urlGenerator->linkToRoute('theming.Theming.getImage', [ 'key' => $logoKey, 'useSvg' => $useSvg, 'v' => $cacheBusterCounter ]);
}

/**
Expand Down Expand Up @@ -300,12 +311,14 @@ public function getScssVariables() {
$variables = [
'theming-cachebuster' => "'" . $cacheBuster . "'",
'theming-logo-mime' => "'" . $this->config->getAppValue('theming', 'logoMime') . "'",
'theming-email-logo-mime' => "'" . $this->config->getAppValue('theming', 'emailLogoMime') . "'",
'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'] = "url('".$this->imageManager->getImageUrl('logo')."')";
$variables['image-email-logo'] = "url('".$this->imageManager->getImageUrl('emailLogo')."')";
$variables['image-logoheader'] = "url('".$this->imageManager->getImageUrl('logoheader')."')";
$variables['image-favicon'] = "url('".$this->imageManager->getImageUrl('favicon')."')";
$variables['image-login-background'] = "url('".$this->imageManager->getImageUrl('background')."')";
Expand Down Expand Up @@ -424,6 +437,7 @@ public function undo($setting) {
$returnValue = $this->getColorPrimary();
break;
case 'logo':
case 'emailLogo':
case 'logoheader':
case 'background':
case 'favicon':
Expand Down
10 changes: 10 additions & 0 deletions apps/theming/templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@
<div data-setting="logoMime" data-toggle="tooltip" data-original-title="<?php p($l->t('Reset to default')); ?>" class="theme-undo icon icon-history"></div>
</form>
</div>
<div>
<form class="uploadButton" method="post" action="<?php p($_['uploadLogoRoute']) ?>" data-image-key="emailLogo">
<input type="hidden" id="theming-emailLogoMime" value="<?php p($_['images']['emailLogo']['mime']); ?>" />
<input type="hidden" name="key" value="emailLogo" />
<label for="upload-email-logo"><span><?php p($l->t('Email Logo')) ?></span></label>
<input id="upload-email-logo" class="fileupload" name="image" type="file" />
<label for="upload-email-logo" class="button icon-upload svg" id="upload-email-log" title="<?php p($l->t('Upload new email logo')) ?>"></label>
<div data-setting="emailLogoMime" data-toggle="tooltip" data-original-title="<?php p($l->t('Reset to default')); ?>" class="theme-undo icon icon-history"></div>
</form>
</div>
<div>
<form class="uploadButton" method="post" action="<?php p($_['uploadLogoRoute']) ?>" data-image-key="background">
<input type="hidden" id="theming-backgroundMime" value="<?php p($_['images']['background']['mime']); ?>" />
Expand Down
38 changes: 30 additions & 8 deletions apps/theming/tests/ThemingDefaultsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,24 @@ public function testGetLogoCustom() {
$this->assertEquals('custom-logo' . '?v=0', $this->template->getLogo());
}

public function testGetEmailLogoCustom() {
$this->config
->expects($this->at(0))
->method('getAppValue')
->with('theming', 'emailLogoMime', false)
->willReturn('image/svg+xml');
$this->config
->expects($this->at(1))
->method('getAppValue')
->with('theming', 'cachebuster', '0')
->willReturn('0');
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('theming.Theming.getImage')
->willReturn('custom-email-logo?v=0');
$this->assertEquals('custom-email-logo' . '?v=0', $this->template->getLogo(false, true));
}

public function testGetScssVariablesCached() {
$this->config->expects($this->any())->method('getAppValue')->with('theming', 'cachebuster', '0')->willReturn('1');
$this->cacheFactory->expects($this->once())
Expand All @@ -647,14 +665,15 @@ public function testGetScssVariablesCached() {
public function testGetScssVariables() {
$this->config->expects($this->at(0))->method('getAppValue')->with('theming', 'cachebuster', '0')->willReturn('0');
$this->config->expects($this->at(1))->method('getAppValue')->with('theming', 'logoMime', false)->willReturn('jpeg');
$this->config->expects($this->at(2))->method('getAppValue')->with('theming', 'backgroundMime', false)->willReturn('jpeg');
$this->config->expects($this->at(3))->method('getAppValue')->with('theming', 'logoheaderMime', false)->willReturn('jpeg');
$this->config->expects($this->at(4))->method('getAppValue')->with('theming', 'faviconMime', false)->willReturn('jpeg');
$this->config->expects($this->at(2))->method('getAppValue')->with('theming', 'emailLogoMime', false)->willReturn('jpeg');
$this->config->expects($this->at(3))->method('getAppValue')->with('theming', 'backgroundMime', false)->willReturn('jpeg');
$this->config->expects($this->at(4))->method('getAppValue')->with('theming', 'logoheaderMime', false)->willReturn('jpeg');
$this->config->expects($this->at(5))->method('getAppValue')->with('theming', 'faviconMime', false)->willReturn('jpeg');

$this->config->expects($this->at(5))->method('getAppValue')->with('theming', 'color', null)->willReturn($this->defaults->getColorPrimary());
$this->config->expects($this->at(6))->method('getAppValue')->with('theming', 'color', $this->defaults->getColorPrimary())->willReturn($this->defaults->getColorPrimary());
$this->config->expects($this->at(6))->method('getAppValue')->with('theming', 'color', null)->willReturn($this->defaults->getColorPrimary());
$this->config->expects($this->at(7))->method('getAppValue')->with('theming', 'color', $this->defaults->getColorPrimary())->willReturn($this->defaults->getColorPrimary());
$this->config->expects($this->at(8))->method('getAppValue')->with('theming', 'color', $this->defaults->getColorPrimary())->willReturn($this->defaults->getColorPrimary());
$this->config->expects($this->at(9))->method('getAppValue')->with('theming', 'color', $this->defaults->getColorPrimary())->willReturn($this->defaults->getColorPrimary());

$this->util->expects($this->any())->method('invertTextColor')->with($this->defaults->getColorPrimary())->willReturn(false);
$this->util->expects($this->any())->method('elementColor')->with($this->defaults->getColorPrimary())->willReturn('#aaaaaa');
Expand All @@ -664,15 +683,18 @@ public function testGetScssVariables() {
->willReturn($this->cache);
$this->cache->expects($this->once())->method('get')->with('getScssVariables')->willReturn(null);
$this->imageManager->expects($this->at(0))->method('getImageUrl')->with('logo')->willReturn('custom-logo?v=0');
$this->imageManager->expects($this->at(1))->method('getImageUrl')->with('logoheader')->willReturn('custom-logoheader?v=0');
$this->imageManager->expects($this->at(2))->method('getImageUrl')->with('favicon')->willReturn('custom-favicon?v=0');
$this->imageManager->expects($this->at(3))->method('getImageUrl')->with('background')->willReturn('custom-background?v=0');
$this->imageManager->expects($this->at(1))->method('getImageUrl')->with('emailLogo')->willReturn('custom-emailLogo?v=0');
$this->imageManager->expects($this->at(2))->method('getImageUrl')->with('logoheader')->willReturn('custom-logoheader?v=0');
$this->imageManager->expects($this->at(3))->method('getImageUrl')->with('favicon')->willReturn('custom-favicon?v=0');
$this->imageManager->expects($this->at(4))->method('getImageUrl')->with('background')->willReturn('custom-background?v=0');

$expected = [
'theming-cachebuster' => '\'0\'',
'theming-logo-mime' => '\'jpeg\'',
'theming-email-logo-mime' => '\'jpeg\'',
'theming-background-mime' => '\'jpeg\'',
'image-logo' => "url('custom-logo?v=0')",
'image-email-logo' => "url('custom-emailLogo?v=0')",
'image-login-background' => "url('custom-background?v=0')",
'color-primary' => $this->defaults->getColorPrimary(),
'color-primary-text' => '#ffffff',
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public function addHeader() {
}
$this->headerAdded = true;

$logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false));
$logoUrl = $this->urlGenerator->getAbsoluteURL($this->themingDefaults->getLogo(false, true));
$this->htmlBody .= vsprintf($this->header, [$this->themingDefaults->getColorPrimary(), $logoUrl, $this->themingDefaults->getName()]);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/public/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ public function getiTunesAppId(): string {
* Themed logo url
*
* @param bool $useSvg Whether to point to the SVG image or a fallback
* @param bool $emailLogo Whether to return the email logo (if one exists)
* @return string
* @since 12.0.0
*/
public function getLogo(bool $useSvg = true): string {
return $this->defaults->getLogo($useSvg);
public function getLogo(bool $useSvg = true, $emailLogo = false): string {
return $this->defaults->getLogo($useSvg, $emailLogo);
}

/**
Expand Down