-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to upload custom image for embedding on the QR Code (#9)
* Allow to upload custom image for embedding on the QR Code * update code * remove console log Co-authored-by: IanM <[email protected]> * format * only serialize attribute in admin * fix coding style * fix review --------- Co-authored-by: IanM <[email protected]>
- Loading branch information
Showing
9 changed files
with
163 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of ianm/twofactor. | ||
* | ||
* Copyright (c) 2023 IanM. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace IanM\TwoFactor\Api; | ||
|
||
use Flarum\Api\Serializer\ForumSerializer; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Contracts\Filesystem\Factory; | ||
|
||
class AddForumAttributes | ||
{ | ||
/** | ||
* @var SettingsRepositoryInterface | ||
*/ | ||
protected $settings; | ||
|
||
protected $assetsFilesystem; | ||
|
||
public function __construct(Factory $filesystemFactory, SettingsRepositoryInterface $settings) | ||
{ | ||
$this->assetsFilesystem = $filesystemFactory->disk('flarum-assets'); | ||
$this->settings = $settings; | ||
} | ||
|
||
public function __invoke(ForumSerializer $serializer, $object, array $attributes): array | ||
{ | ||
$actor = $serializer->getActor(); | ||
|
||
if ($actor->isAdmin()) { | ||
$attributes['ianm_twofactor_logoUrl'] = $this->getLogoUrl(); | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
protected function getLogoUrl(): ?string | ||
{ | ||
$logoPath = $this->settings->get('ianm_twofactor_logo_path'); | ||
|
||
return $logoPath ? $this->getAssetUrl($logoPath) : null; | ||
} | ||
|
||
public function getAssetUrl(string $assetPath): string | ||
{ | ||
return $this->assetsFilesystem->url($assetPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of ianm/twofactor. | ||
* | ||
* Copyright (c) 2023 IanM. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace IanM\TwoFactor\Api\Controller; | ||
|
||
use Flarum\Api\Controller\AbstractDeleteController; | ||
use Flarum\Http\RequestUtil; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Contracts\Filesystem\Factory; | ||
use Illuminate\Contracts\Filesystem\Filesystem; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class DeleteLogoController extends AbstractDeleteController | ||
{ | ||
protected Filesystem $uploadDir; | ||
|
||
public function __construct( | ||
protected SettingsRepositoryInterface $settings, | ||
Factory $filesystemFactory | ||
) { | ||
$this->uploadDir = $filesystemFactory->disk('flarum-assets'); | ||
} | ||
|
||
protected function delete(ServerRequestInterface $request): void | ||
{ | ||
RequestUtil::getActor($request)->assertAdmin(); | ||
|
||
$path = $this->settings->get('ianm_twofactor_logo_path'); | ||
|
||
$this->settings->set('ianm_twofactor_logo_path', null); | ||
|
||
if ($this->uploadDir->exists($path)) { | ||
$this->uploadDir->delete($path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of ianm/twofactor. | ||
* | ||
* Copyright (c) 2023 IanM. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace IanM\TwoFactor\Api\Controller; | ||
|
||
use Flarum\Api\Controller\UploadImageController; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Contracts\Filesystem\Factory; | ||
use Intervention\Image\Constraint; | ||
use Intervention\Image\Image; | ||
use Intervention\Image\ImageManager; | ||
use Psr\Http\Message\UploadedFileInterface; | ||
|
||
class UploadLogoController extends UploadImageController | ||
{ | ||
protected $filePathSettingKey = 'ianm_twofactor_logo_path'; | ||
|
||
protected $filenamePrefix = 'ianm_twofactor_logo'; | ||
|
||
public function __construct( | ||
SettingsRepositoryInterface $settings, | ||
Factory $filesystemFactory, | ||
protected ImageManager $imageManager | ||
) { | ||
parent::__construct($settings, $filesystemFactory); | ||
} | ||
|
||
protected function makeImage(UploadedFileInterface $file): Image | ||
{ | ||
$encodedImage = $this->imageManager | ||
->make($file->getStream()->getMetadata('uri')) | ||
->heighten(60, fn (Constraint $constraint) => $constraint->upsize())->encode('png'); | ||
|
||
return $encodedImage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters