From 9b634dd55f8ef7914e8c199825446bc9e60bb56a Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Wed, 20 Jul 2022 13:31:51 +0200 Subject: [PATCH] Move method from model to the Logo service --- app/Models/TwoFAccount.php | 15 +++++---------- app/Services/LogoService.php | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/app/Models/TwoFAccount.php b/app/Models/TwoFAccount.php index 5838fae0..15689f0c 100644 --- a/app/Models/TwoFAccount.php +++ b/app/Models/TwoFAccount.php @@ -320,7 +320,7 @@ public function setCounterAttribute($value) */ public function getOTP() { - Log::info(sprintf('OTP requested for TwoFAccount #%s', $this->id)); + Log::info(sprintf('OTP requested for TwoFAccount (%s)', $this->id ? 'id:'.$this->id: 'preview')); // Early exit if the model has an undecipherable secret if (strtolower($this->secret) === __('errors.indecipherable')) { @@ -352,7 +352,7 @@ public function getOTP() } - Log::info(sprintf('New OTP generated for TwoFAccount #%s', $this->id)); + Log::info(sprintf('New OTP generated for TwoFAccount (%s)', $this->id ? 'id:'.$this->id: 'preview')); return $OtpDto; @@ -441,7 +441,7 @@ public function fillWithURI(string $uri, bool $isSteamTotp = false) $this->icon = $this->storeImageAsIcon($this->generator->getParameter('image')); } if (!$this->icon) { - $this->icon = $this->defaultLogo(); + $this->icon = $this->getDefaultIcon(); } Log::info(sprintf('TwoFAccount filled with an URI')); @@ -584,16 +584,11 @@ private function storeImageAsIcon(string $url) * * @return string|null The icon */ - private function defaultLogo() + private function getDefaultIcon() { $logoService = App::make(LogoService::class); - $logoFilename = $logoService->getLogo($this->service); - if ($logoFilename) { - $newFilename = Str::random(40).'.svg'; - return Storage::disk('icons')->put($newFilename, Storage::disk('logos')->get($logoFilename)) ? $newFilename : null; - } - else return null; + return $logoService->getIcon($this->service); } diff --git a/app/Services/LogoService.php b/app/Services/LogoService.php index 90297437..04f821d8 100644 --- a/app/Services/LogoService.php +++ b/app/Services/LogoService.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; class LogoService { @@ -26,22 +27,40 @@ public function __construct() } + /** + * Fetch a logo for the given service and set it as an icon + * + * @param string $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) + { + $logoFilename = $this->getLogo($serviceName); + + if ($logoFilename) { + $newFilename = Str::random(40).'.svg'; + return Storage::disk('icons')->put($newFilename, Storage::disk('logos')->get($logoFilename)) ? $newFilename : 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 */ - public function getLogo(string $serviceName) : string + public function getLogo(string $serviceName) { - $domain = $this->tfas->get(strtolower($serviceName)); + $domain = $this->tfas->get($this->cleanDomain($serviceName)); $logoFilename = $domain.'.svg'; if ($domain && !Storage::disk('logos')->exists($logoFilename)) { $this->fetchLogo($logoFilename); } - return Storage::disk('logos')->exists($logoFilename) ? $logoFilename : ''; + return Storage::disk('logos')->exists($logoFilename) ? $logoFilename : null; } @@ -97,7 +116,7 @@ protected function cacheTfaDirectorySource() : void /** - * Fetch a logo and store it to the disk + * Fetch and cache a logo from 2fa.Directory repository * * @param string $logoFile Logo filename to fetch * @return void @@ -118,4 +137,13 @@ protected function fetchLogo(string $logoFile) : void Log::error(sprintf('Fetching of logo "%s" failed.', $logoFile)); } } + + + /** + * + */ + protected function cleanDomain($domain) : string + { + return strtolower(str_replace(['+'], ['plus'], $domain)); + } } \ No newline at end of file