Skip to content

Commit 23ab2ba

Browse files
fixup! fix: generate favourite icon without imagick svg support
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent 2287b67 commit 23ab2ba

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

apps/theming/lib/Controller/IconController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ public function getFavicon(string $app = 'core'): Response {
9797
$iconFile = null;
9898
try {
9999
$iconFile = $this->imageManager->getImage('favicon', false);
100-
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
100+
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $iconFile->getMimeType()]);
101101
} catch (NotFoundException $e) {
102102
}
103-
if ($iconFile === null && $this->imageManager->canConvert('PNG')) {
103+
if ($iconFile === null && ($this->imageManager->canConvert('SVG') || $this->imageManager->canConvert('PNG'))) {
104104
$color = $this->themingDefaults->getColorPrimary();
105105
try {
106106
$iconFile = $this->imageManager->getCachedImage('favIcon-' . $app . $color);
@@ -111,11 +111,11 @@ public function getFavicon(string $app = 'core'): Response {
111111
}
112112
$iconFile = $this->imageManager->setCachedImage('favIcon-' . $app . $color, $icon);
113113
}
114-
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
114+
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $iconFile->getMimeType()]);
115115
}
116116
if ($response === null) {
117117
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
118-
$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
118+
$response = new DataDisplayResponse($this->fileAccessHelper->file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
119119
}
120120
$response->cacheFor(86400);
121121
return $response;
@@ -142,10 +142,10 @@ public function getTouchIcon(string $app = 'core'): Response {
142142
$response = null;
143143
try {
144144
$iconFile = $this->imageManager->getImage('favicon');
145-
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
145+
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $iconFile->getMimeType()]);
146146
} catch (NotFoundException $e) {
147147
}
148-
if ($this->imageManager->canConvert('PNG')) {
148+
if ($this->imageManager->canConvert('SVG') || $this->imageManager->canConvert('PNG')) {
149149
$color = $this->themingDefaults->getColorPrimary();
150150
try {
151151
$iconFile = $this->imageManager->getCachedImage('touchIcon-' . $app . $color);
@@ -156,7 +156,7 @@ public function getTouchIcon(string $app = 'core'): Response {
156156
}
157157
$iconFile = $this->imageManager->setCachedImage('touchIcon-' . $app . $color, $icon);
158158
}
159-
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
159+
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $iconFile->getMimeType()]);
160160
}
161161
if ($response === null) {
162162
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';

apps/theming/lib/Controller/ThemingController.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ public function undoAll(): DataResponse {
345345
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
346346
public function getImage(string $key, bool $useSvg = true) {
347347
try {
348+
$useSvg = $useSvg && $this->imageManager->canConvert('SVG');
348349
$file = $this->imageManager->getImage($key, $useSvg);
349350
} catch (NotFoundException $e) {
350351
return new NotFoundResponse();
@@ -355,13 +356,8 @@ public function getImage(string $key, bool $useSvg = true) {
355356
$csp->allowInlineStyle();
356357
$response->setContentSecurityPolicy($csp);
357358
$response->cacheFor(3600);
358-
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', ''));
359+
$response->addHeader('Content-Type', $file->getMimeType());
359360
$response->addHeader('Content-Disposition', 'attachment; filename="' . $key . '"');
360-
if (!$useSvg) {
361-
$response->addHeader('Content-Type', 'image/png');
362-
} else {
363-
$response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', ''));
364-
}
365361
return $response;
366362
}
367363

apps/theming/lib/ImageManager.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,25 @@ public function getImage(string $key, bool $useSvg = true): ISimpleFile {
8989
if ($mime === '' || !$folder->fileExists($key)) {
9090
throw new NotFoundException();
9191
}
92-
92+
// if SVG was requested and is supported
93+
if ($useSvg && $this->canConvert('SVG')) {
94+
if (!$folder->fileExists($key . '.svg')) {
95+
try {
96+
$finalIconFile = new \Imagick();
97+
$finalIconFile->setBackgroundColor('none');
98+
$finalIconFile->readImageBlob($folder->getFile($key)->getContent());
99+
$finalIconFile->setImageFormat('SVG');
100+
$svgFile = $folder->newFile($key . '.svg');
101+
$svgFile->putContent($finalIconFile->getImageBlob());
102+
return $svgFile;
103+
} catch (\ImagickException $e) {
104+
$this->logger->info('The image was requested to be no SVG file, but converting it to SVG failed: ' . $e->getMessage());
105+
}
106+
} else {
107+
return $folder->getFile($key . '.svg');
108+
}
109+
}
110+
// if SVG was not requested, but PNG is supported
93111
if (!$useSvg && $this->canConvert('PNG')) {
94112
if (!$folder->fileExists($key . '.png')) {
95113
try {
@@ -107,7 +125,7 @@ public function getImage(string $key, bool $useSvg = true): ISimpleFile {
107125
return $folder->getFile($key . '.png');
108126
}
109127
}
110-
128+
// fallback to the original file
111129
return $folder->getFile($key);
112130
}
113131

0 commit comments

Comments
 (0)