Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions apps/theming/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
'verb' => 'GET',
'defaults' => ['app' => 'core'],
],
[
'name' => 'Icon#getManifestIcon',
'url' => '/manifest-icon',
'verb' => 'GET',
],
[
'name' => 'Icon#getThemedIcon',
'url' => '/img/{app}/{image}',
Expand Down
69 changes: 61 additions & 8 deletions apps/theming/lib/Controller/IconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\AppFramework\Http\Response;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\IConfig;
use OCP\IRequest;

Expand Down Expand Up @@ -84,7 +85,7 @@ public function getThemedIcon(string $app, string $image): Response {
* Return a 32x32 favicon as png
*
* @param string $app ID of the app
* @return DataDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/png'}>|FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/x-icon'}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
* @return DataDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/png'}>|FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
* @throws \Exception
*
* 200: Favicon returned
Expand All @@ -100,10 +101,16 @@ public function getFavicon(string $app = 'core'): Response {

$response = null;
$iconFile = null;
// retrieve instance favicon
// retrieve instance favicon, a raster image is converted to a square ICO file
try {
$iconFile = $this->imageManager->getImage('favicon', false);
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
$iconFile = $this->imageManager->getImage('favicon');
$mimeType = $this->imageManager->getImageMime('favicon');
$icoFile = $this->getCachedIcon('favIcon-favicon', fn () => $this->iconBuilder->getUploadedFavicon());
if ($icoFile !== null) {
$iconFile = $icoFile;
$mimeType = 'image/x-icon';
}
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $mimeType]);
} catch (NotFoundException $e) {
}
// retrieve or generate app specific favicon, but only if no custom favicon was uploaded
Expand Down Expand Up @@ -149,13 +156,20 @@ public function getTouchIcon(string $app = 'core'): Response {

$response = null;
$iconFile = null;
// retrieve instance favicon
// retrieve instance favicon, rendered like the app specific touch icons
try {
$iconFile = $this->imageManager->getImage('favicon');
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $iconFile->getMimeType()]);
$favicon = $this->imageManager->getImage('favicon');
$color = $this->themingDefaults->getColorPrimary();
$iconFile = $this->getCachedIcon('touchIcon-favicon' . $color, fn () => $this->iconBuilder->getUploadedTouchIcon());
if ($iconFile !== null) {
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
} elseif (!$this->imageManager->canConvert('PNG')) {
$iconFile = $favicon;
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => $this->imageManager->getImageMime('favicon')]);
}
} catch (NotFoundException $e) {
}
// retrieve or generate app specific touch icon, but only if no custom favicon was uploaded
// retrieve or generate app specific touch icon, but only if no custom favicon was uploaded or it can not be rendered
if ($iconFile === null && $this->imageManager->canConvert('PNG')) {
$color = $this->themingDefaults->getColorPrimary();
try {
Expand All @@ -177,4 +191,43 @@ public function getTouchIcon(string $app = 'core'): Response {
$response->cacheFor(86400);
return $response;
}

/**
* Return the uploaded favicon as a transparent PNG image for the web app manifest
*
* @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: 'image/png'}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
*
* 200: Icon returned
* 404: No favicon uploaded or it can not be rendered
*/
#[PublicPage]
#[NoCSRFRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)]
public function getManifestIcon(): Response {
if (!$this->imageManager->hasImage('favicon') || !$this->imageManager->canConvert('PNG')) {
return new NotFoundResponse();
}
$iconFile = $this->getCachedIcon('manifestIcon-favicon', fn () => $this->iconBuilder->getUploadedManifestIcon());
if ($iconFile === null) {
return new NotFoundResponse();
}
$response = new FileDisplayResponse($iconFile, Http::STATUS_OK, ['Content-Type' => 'image/png']);
$response->cacheFor(86400);
return $response;
}

/**
* Get a generated icon from the cache, an empty cached file marks a failed generation
*
* @param callable(): (string|false) $generate
*/
private function getCachedIcon(string $name, callable $generate): ?ISimpleFile {
try {
$iconFile = $this->imageManager->getCachedImage($name);
} catch (NotFoundException $e) {
$icon = $generate();
$iconFile = $this->imageManager->setCachedImage($name, $icon === false ? '' : $icon);
}
return $iconFile->getSize() > 0 ? $iconFile : null;
}
}
88 changes: 73 additions & 15 deletions apps/theming/lib/Controller/ThemingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OCA\Theming\Controller;

use InvalidArgumentException;
use OCA\Theming\IconBuilder;
use OCA\Theming\ImageManager;
use OCA\Theming\ITheme;
use OCA\Theming\Service\ThemesService;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function __construct(
private ThemesService $themesService,
private INavigationManager $navigationManager,
private IEventDispatcher $eventDispatcher,
private IconBuilder $iconBuilder,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -506,6 +508,76 @@ public function getThemeStylesheet(string $themeId, bool $plain = false, bool $w
}
}

/**
* Type and size of the icon returned by IconController::getTouchIcon()
*
* @return array{type: string, sizes: string}
*/
private function getTouchIconFormat(): array {
if ($this->imageManager->canConvert('PNG')) {
// the uploaded favicon or the generated touch icon
$size = $this->imageManager->hasImage('favicon') ? $this->iconBuilder->getUploadedTouchIconSize() : null;
$size ??= 512;
return ['type' => 'image/png', 'sizes' => $size . 'x' . $size];
}
if (!$this->imageManager->hasImage('favicon')) {
$size = getimagesize(\OC::$SERVERROOT . '/core/img/favicon-touch.png');
return ['type' => 'image/png', 'sizes' => $size === false ? 'any' : $size[0] . 'x' . $size[1]];
}
// without Imagick the uploaded favicon is returned as it is
$type = $this->imageManager->getImageMime('favicon');
if ($this->imageManager->isSvgImage('favicon')) {
return ['type' => $type, 'sizes' => 'any'];
}
try {
$size = getimagesizefromstring($this->imageManager->getImage('favicon')->getContent());
} catch (NotFoundException|NotPermittedException $e) {
$size = false;
}
return ['type' => $type, 'sizes' => $size === false ? 'any' : $size[0] . 'x' . $size[1]];
}

/**
* Icons of the web app manifest
*
* An uploaded SVG favicon is listed first, followed by a PNG image for
* browsers that do not support SVG icons. The uploaded favicon is used
* without a background, the touch icon with its background is only used
* if the favicon can not be rendered or is too small.
*
* @return list<array{src: non-empty-string, type: string, sizes: string}>
*/
private function getManifestIcons(string $app, string $cacheBusterValue): array {
$touchIcon = fn (): array => [
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon', ['app' => $app]) . '?v=' . $cacheBusterValue,
...$this->getTouchIconFormat(),
];
if (!$this->imageManager->hasImage('favicon')) {
return [$touchIcon()];
}
$icons = [];
$isSvg = $this->imageManager->isSvgImage('favicon');
if ($isSvg) {
$icons[] = [
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getFavicon', ['app' => $app]) . '?v=' . $cacheBusterValue,
'type' => 'image/svg+xml',
'sizes' => 'any',
];
}
if ($this->imageManager->canConvert('PNG')) {
$size = $this->iconBuilder->getUploadedManifestIconSize();
$icons[] = $size === null ? $touchIcon() : [
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getManifestIcon') . '?v=' . $cacheBusterValue,
'type' => 'image/png',
'sizes' => $size . 'x' . $size,
];
} elseif (!$isSvg) {
// without Imagick the touch icon is the uploaded favicon as it is
$icons[] = $touchIcon();
}
return $icons;
}

/**
* Get the manifest for an app
*
Expand Down Expand Up @@ -556,21 +628,7 @@ public function getManifest(string $app): JSONResponse {
'theme_color' => $this->themingDefaults->getColorPrimary(),
'background_color' => $this->themingDefaults->getColorPrimary(),
'description' => $description,
'icons'
=> [
[
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getTouchIcon',
['app' => $app]) . '?v=' . $cacheBusterValue,
'type' => 'image/png',
'sizes' => '512x512'
],
[
'src' => $this->urlGenerator->linkToRoute('theming.Icon.getFavicon',
['app' => $app]) . '?v=' . $cacheBusterValue,
'type' => 'image/svg+xml',
'sizes' => '16x16'
]
],
'icons' => $this->getManifestIcons($app, $cacheBusterValue),
'display_override' => $standalone ? ['minimal-ui'] : [],
'display' => $standalone ? 'standalone' : 'browser'
];
Expand Down
Loading