mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-06-20 19:57:44 +02:00
Add selfh & Dashboardicons icon collections
This commit is contained in:
parent
bff3bd7182
commit
8b745731e2
@ -74,7 +74,7 @@ class TwoFAuthServiceProvider extends ServiceProvider implements DeferrableProvi
|
||||
return [
|
||||
IconService::class,
|
||||
IconStoreService::class,
|
||||
LogoLibManager::class,
|
||||
'logolib',
|
||||
QrReader::class,
|
||||
ReleaseRadarService::class,
|
||||
];
|
||||
|
@ -10,15 +10,70 @@ use Illuminate\Support\Facades\Storage;
|
||||
|
||||
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
|
||||
*/
|
||||
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
|
||||
*/
|
||||
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
|
||||
@ -33,9 +88,11 @@ abstract class AbstractLogoLib implements LogoLibInterface
|
||||
])->retry(3, 100)->get($this->logoUrl($logoFilename));
|
||||
|
||||
if ($response->successful()) {
|
||||
Storage::disk('logos')->put($logoFilename, $response->body())
|
||||
? Log::info(sprintf('Logo "%s" saved to logos dir.', $logoFilename))
|
||||
: Log::notice(sprintf('Cannot save logo "%s" to logos dir', $logoFilename));
|
||||
$filename = $this->cachePrefix . $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) {
|
||||
Log::error(sprintf('Fetching of logo "%s" failed.', $logoFilename));
|
||||
|
21
app/Services/LogoLib/DashboardiconsLogoLib.php
Normal file
21
app/Services/LogoLib/DashboardiconsLogoLib.php
Normal 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/';
|
||||
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
|
||||
namespace App\Services\LogoLib;
|
||||
|
||||
use App\Services\LogoLib\TfaLogoLib;
|
||||
use Illuminate\Support\Manager;
|
||||
|
||||
class LogoLibManager extends Manager
|
||||
@ -17,8 +16,13 @@ class LogoLibManager extends Manager
|
||||
return new TfaLogoLib();
|
||||
}
|
||||
|
||||
// public function createSelfhDriver()
|
||||
// {
|
||||
// return new SelfhLogoLib();
|
||||
// }
|
||||
public function createSelfhDriver() : SelfhLogoLib
|
||||
{
|
||||
return new SelfhLogoLib();
|
||||
}
|
||||
|
||||
public function createDashboardiconsDriver() : DashboardiconsLogoLib
|
||||
{
|
||||
return new DashboardiconsLogoLib();
|
||||
}
|
||||
}
|
||||
|
20
app/Services/LogoLib/SelfhLogoLib.php
Normal file
20
app/Services/LogoLib/SelfhLogoLib.php
Normal 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/';
|
||||
}
|
@ -29,7 +29,7 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
|
||||
/**
|
||||
* @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()
|
||||
{
|
||||
$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
|
||||
@ -67,14 +47,15 @@ class TfaLogoLib extends AbstractLogoLib implements LogoLibInterface
|
||||
*/
|
||||
protected function getLogo(string $serviceName)
|
||||
{
|
||||
$domain = $this->tfas->get($this->sanitizeServiceName(strval($serviceName)));
|
||||
$logoFilename = $domain . '.svg';
|
||||
$referenceName = $this->tfas->get($this->sanitizeServiceName(strval($serviceName)));
|
||||
$logoFilename = $referenceName . '.svg';
|
||||
$cachedFilename = $this->cachePrefix . $logoFilename;
|
||||
|
||||
if ($domain && ! Storage::disk('logos')->exists($logoFilename)) {
|
||||
$this->fetchLogo($logoFilename);
|
||||
if ($referenceName && ! Storage::disk('logos')->exists($cachedFilename)) {
|
||||
$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
|
||||
{
|
||||
return self::IMG_URL . $logoFilename[0] . '/' . $logoFilename;
|
||||
return $this->libUrl . $logoFilename[0] . '/' . $logoFilename;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Services\LogoLib\LogoLibManager;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
@ -175,6 +176,7 @@ return [
|
||||
'TwoFAccounts' => App\Facades\TwoFAccounts::class,
|
||||
'Settings' => App\Facades\Settings::class,
|
||||
'Helpers' => App\Helpers\Helpers::class,
|
||||
'logolib' => LogoLibManager::class,
|
||||
])->toArray(),
|
||||
|
||||
];
|
||||
|
Loading…
x
Reference in New Issue
Block a user