Add selfh & Dashboardicons icon collections

This commit is contained in:
Bubka 2025-06-05 11:05:18 +02:00
parent bff3bd7182
commit 8b745731e2
7 changed files with 124 additions and 39 deletions

View File

@ -74,7 +74,7 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi
return [ return [
IconService::class, IconService::class,
IconStoreService::class, IconStoreService::class,
LogoLibManager::class, 'logolib',
QrReader::class, QrReader::class,
ReleaseRadarService::class, ReleaseRadarService::class,
]; ];

View File

@ -10,15 +10,70 @@ use Illuminate\Support\Facades\Storage;
abstract class AbstractLogoLib implements LogoLibInterface abstract class AbstractLogoLib implements LogoLibInterface
{ {
/**
* The prefix to be aplied to cached filename.
*/
protected string $cachePrefix = '';
/**
* Base url of the icon collection
*/
protected string $libUrl = '';
/**
* 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 = IconService::getRandomName('svg');
$iconFilename = \Illuminate\Support\Str::random(40) . '.svg';
return $this->copyToIconStore($logoFilename, $iconFilename) ? $iconFilename : null;
} else {
return null;
}
}
/**
* Return the logo's filename for a given service
*
* @param string $serviceName Name of the service to fetch a logo for
* @return string|null The logo filename or null if no logo has been found
*/
protected function getLogo(string $serviceName)
{
$referenceName = $this->sanitizeServiceName(strval($serviceName));
$logoFilename = $referenceName . '.svg';
$cachedFilename = $this->cachePrefix . $logoFilename;
if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) {
$this->fetchLogo($logoFilename);
}
return Storage::disk('logos')->exists($cachedFilename) ? $cachedFilename : null;
}
/** /**
* Url to use in http request to get a specific logo from the logo lib * Url to use in http request to get a specific logo from the logo lib
*/ */
abstract protected function logoUrl(string $logoFilename) : string; protected function logoUrl(string $logoFilename) : string
{
return $this->libUrl . $logoFilename;
}
/** /**
* Prepare service name to match logo libs naming convention * Prepare service name to match logo libs naming convention
*/ */
abstract protected function sanitizeServiceName(string $service) : string; protected function sanitizeServiceName(string $service) : string
{
return preg_replace('/[^0-9a-z]+/', '-', strtolower($service));
}
/** /**
* Fetch and cache a logo from the logo library * Fetch and cache a logo from the logo library
@ -33,9 +88,11 @@ abstract class AbstractLogoLib implements LogoLibInterface
])->retry(3, 100)->get($this->logoUrl($logoFilename)); ])->retry(3, 100)->get($this->logoUrl($logoFilename));
if ($response->successful()) { if ($response->successful()) {
Storage::disk('logos')->put($logoFilename, $response->body()) $filename = $this->cachePrefix . $logoFilename;
? Log::info(sprintf('Logo "%s" saved to logos dir.', $logoFilename))
: Log::notice(sprintf('Cannot save logo "%s" to logos dir', $logoFilename)); Storage::disk('logos')->put($filename, $response->body())
? Log::info(sprintf('Logo file "%s" saved to logos dir.', $filename))
: Log::notice(sprintf('Cannot save logo file "%s" to logos dir', $filename));
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {
Log::error(sprintf('Fetching of logo "%s" failed.', $logoFilename)); Log::error(sprintf('Fetching of logo "%s" failed.', $logoFilename));

View File

@ -0,0 +1,21 @@
<?php
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
{
/**
* The prefix to be aplied to cached filename.
*/
protected string $cachePrefix = 'dashboardicons_';
/**
* Base url of the icon collection
*/
protected string $libUrl = 'https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/svg/';
}

View File

@ -2,7 +2,6 @@
namespace App\Services\LogoLib; namespace App\Services\LogoLib;
use App\Services\LogoLib\TfaLogoLib;
use Illuminate\Support\Manager; use Illuminate\Support\Manager;
class LogoLibManager extends Manager class LogoLibManager extends Manager
@ -17,8 +16,13 @@ class LogoLibManager extends Manager
return new TfaLogoLib(); return new TfaLogoLib();
} }
// public function createSelfhDriver() public function createSelfhDriver() : SelfhLogoLib
// { {
// return new SelfhLogoLib(); return new SelfhLogoLib();
// } }
public function createDashboardiconsDriver() : DashboardiconsLogoLib
{
return new DashboardiconsLogoLib();
}
} }

View File

@ -0,0 +1,20 @@
<?php
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
{
/**
* The prefix to be aplied to cached filename.
*/
protected string $cachePrefix = 'selfh_';
/**
* Base url of the icon collection
*/
protected string $libUrl = 'https://cdn.jsdelivr.net/gh/selfhst/icons/svg/';
}

View File

@ -29,7 +29,7 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
/** /**
* @var string * @var string
*/ */
const IMG_URL = 'https://raw.githubusercontent.com/2factorauth/twofactorauth/master/img/'; protected string $libUrl = 'https://raw.githubusercontent.com/2factorauth/twofactorauth/master/img/';
/** /**
* *
@ -37,27 +37,7 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
public function __construct() public function __construct()
{ {
$this->setTfaCollection(); $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 = IconService::getRandomName('svg');
$iconFilename = \Illuminate\Support\Str::random(40) . '.svg';
return $this->copyToIconStore($logoFilename, $iconFilename) ? $iconFilename : null;
} else {
return null;
}
}
/** /**
* Return the logo's filename for a given service * Return the logo's filename for a given service
@ -67,14 +47,15 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
*/ */
protected function getLogo(string $serviceName) protected function getLogo(string $serviceName)
{ {
$domain = $this->tfas->get($this->sanitizeServiceName(strval($serviceName))); $referenceName = $this->tfas->get($this->sanitizeServiceName(strval($serviceName)));
$logoFilename = $domain . '.svg'; $logoFilename = $referenceName . '.svg';
$cachedFilename = $this->cachePrefix . $logoFilename;
if ($domain && ! Storage::disk('logos')->exists($logoFilename)) { if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) {
$this->fetchLogo($logoFilename); $this->fetchLogo($cachedFilename);
} }
return Storage::disk('logos')->exists($logoFilename) ? $logoFilename : null; return Storage::disk('logos')->exists($cachedFilename) ? $cachedFilename : null;
} }
/** /**
@ -126,7 +107,7 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
*/ */
protected function logoUrl(string $logoFilename) : string protected function logoUrl(string $logoFilename) : string
{ {
return self::IMG_URL . $logoFilename[0] . '/' . $logoFilename; return $this->libUrl . $logoFilename[0] . '/' . $logoFilename;
} }
/** /**

View File

@ -1,5 +1,6 @@
<?php <?php
use App\Services\LogoLib\LogoLibManager;
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
@ -175,6 +176,7 @@ return [
'TwoFAccounts' => App\Facades\TwoFAccounts::class, 'TwoFAccounts' => App\Facades\TwoFAccounts::class,
'Settings' => App\Facades\Settings::class, 'Settings' => App\Facades\Settings::class,
'Helpers' => App\Helpers\Helpers::class, 'Helpers' => App\Helpers\Helpers::class,
'logolib' => LogoLibManager::class,
])->toArray(), ])->toArray(),
]; ];