Add fallback to png format when svg logo is not available

This commit is contained in:
Bubka 2025-06-06 09:49:01 +02:00
parent c693c840b2
commit c4acd572f8
4 changed files with 36 additions and 9 deletions

View File

@ -20,6 +20,11 @@ abstract class AbstractLogoLib implements LogoLibInterface
*/
protected string $libUrl = '';
/**
* Base url of the icon collection
*/
protected string $format = 'svg';
/**
* Fetch a logo for the given service and save it as an icon
*
@ -30,9 +35,14 @@ abstract class AbstractLogoLib implements LogoLibInterface
{
$logoFilename = $this->getLogo(strval($serviceName));
if (!$logoFilename) {
// maybe the svg is not available, we try to get the png format
$this->format = 'png';
$logoFilename = $this->getLogo(strval($serviceName));
}
if ($logoFilename) {
// $iconFilename = IconService::getRandomName('svg');
$iconFilename = \Illuminate\Support\Str::random(40) . '.svg';
$iconFilename = \Illuminate\Support\Str::random(40) . '.' . $this->format;
return $this->copyToIconStore($logoFilename, $iconFilename) ? $iconFilename : null;
} else {
@ -49,7 +59,7 @@ abstract class AbstractLogoLib implements LogoLibInterface
protected function getLogo(string $serviceName)
{
$referenceName = $this->sanitizeServiceName(strval($serviceName));
$logoFilename = $referenceName . '.svg';
$logoFilename = $referenceName . '.' . $this->format;
$cachedFilename = $this->cachePrefix . $logoFilename;
if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) {
@ -64,7 +74,7 @@ abstract class AbstractLogoLib implements LogoLibInterface
*/
protected function logoUrl(string $logoFilename) : string
{
return $this->libUrl . $logoFilename;
return $this->libUrl . $this->format . '/' . $logoFilename;
}
/**

View File

@ -4,7 +4,6 @@ namespace App\Services\LogoLib;
use App\Services\LogoLib\AbstractLogoLib;
use App\Services\LogoLib\LogoLibInterface;
use Illuminate\Support\Facades\Storage;
class DashboardiconsLogoLib extends AbstractLogoLib implements LogoLibInterface
{
@ -16,6 +15,6 @@ class DashboardiconsLogoLib extends AbstractLogoLib implements LogoLibInterface
/**
* Base url of the icon collection
*/
protected string $libUrl = 'https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/svg/';
protected string $libUrl = 'https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/';
}

View File

@ -4,7 +4,6 @@ namespace App\Services\LogoLib;
use App\Services\LogoLib\AbstractLogoLib;
use App\Services\LogoLib\LogoLibInterface;
use Illuminate\Support\Facades\Storage;
class SelfhLogoLib extends AbstractLogoLib implements LogoLibInterface
{
@ -16,5 +15,5 @@ class SelfhLogoLib extends AbstractLogoLib implements LogoLibInterface
/**
* Base url of the icon collection
*/
protected string $libUrl = 'https://cdn.jsdelivr.net/gh/selfhst/icons/svg/';
protected string $libUrl = 'https://cdn.jsdelivr.net/gh/selfhst/icons/';
}

View File

@ -39,6 +39,25 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
$this->setTfaCollection();
}
/**
* Fetch a logo for the given service and save it as an icon
*
* @param string|null $serviceName Name of the service to fetch a logo for
* @return string|null The icon filename or null if no logo has been found
*/
public function getIcon(?string $serviceName) : string|null
{
$logoFilename = $this->getLogo(strval($serviceName));
if ($logoFilename) {
$iconFilename = \Illuminate\Support\Str::random(40) . '.' . $this->format;
return $this->copyToIconStore($logoFilename, $iconFilename) ? $iconFilename : null;
} else {
return null;
}
}
/**
* Return the logo's filename for a given service
*
@ -48,7 +67,7 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
protected function getLogo(string $serviceName)
{
$referenceName = $this->tfas->get($this->sanitizeServiceName(strval($serviceName)));
$logoFilename = $referenceName . '.svg';
$logoFilename = $referenceName . '.' . $this->format;
$cachedFilename = $this->cachePrefix . $logoFilename;
if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) {