feat: recover support for ico favicon (#4126)

This commit is contained in:
Sami Mazouz 2024-11-22 16:53:19 +01:00 committed by GitHub
parent 49064f6912
commit 41e5ff2525
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 46 deletions

View File

@ -9,13 +9,8 @@
namespace Flarum\Api\Controller;
use Flarum\Api\JsonApi;
use Flarum\Foundation\ValidationException;
use Flarum\Locale\TranslatorInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
class UploadFaviconController extends UploadImageController
@ -23,28 +18,12 @@ class UploadFaviconController extends UploadImageController
protected string $filePathSettingKey = 'favicon_path';
protected string $filenamePrefix = 'favicon';
public function __construct(
JsonApi $api,
SettingsRepositoryInterface $settings,
Factory $filesystemFactory,
protected TranslatorInterface $translator,
protected ImageManager $imageManager
) {
parent::__construct($api, $settings, $filesystemFactory);
}
protected function makeImage(UploadedFileInterface $file): EncodedImageInterface
protected function makeImage(UploadedFileInterface $file): EncodedImageInterface|StreamInterface
{
$this->fileExtension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
if ($this->fileExtension === 'ico') {
// @todo remove in 2.0
throw new ValidationException([
'message' => strtr($this->translator->trans('validation.mimes'), [
':attribute' => 'favicon',
':values' => 'jpeg,png,gif,webp',
])
]);
return $file->getStream();
}
$encodedImage = $this->imageManager->read($file->getStream()->getMetadata('uri'))

View File

@ -11,14 +11,17 @@ namespace Flarum\Api\Controller;
use Flarum\Api\JsonApi;
use Flarum\Http\RequestUtil;
use Flarum\Locale\TranslatorInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
abstract class UploadImageController extends ShowForumController
@ -31,6 +34,8 @@ abstract class UploadImageController extends ShowForumController
public function __construct(
JsonApi $api,
protected SettingsRepositoryInterface $settings,
protected ImageManager $imageManager,
protected TranslatorInterface $translator,
Factory $filesystemFactory
) {
parent::__construct($api);
@ -42,22 +47,45 @@ abstract class UploadImageController extends ShowForumController
{
RequestUtil::getActor($request)->assertAdmin();
$file = Arr::get($request->getUploadedFiles(), $this->filenamePrefix);
$filenamePrefix = $this->filenamePrefix($request);
$file = Arr::get($request->getUploadedFiles(), $filenamePrefix);
$encodedImage = $this->makeImage($file);
if (($path = $this->settings->get($this->filePathSettingKey)) && $this->uploadDir->exists($path)) {
$filePathSettingKey = $this->filePathSettingKey($request, $file);
if (($path = $this->settings->get($filePathSettingKey)) && $this->uploadDir->exists($path)) {
$this->uploadDir->delete($path);
}
$uploadName = $this->filenamePrefix.'-'.Str::lower(Str::random(8)).'.'.$this->fileExtension;
$uploadName = $filenamePrefix.'-'.Str::lower(Str::random(8)).'.'.$this->fileExtension($request, $file);
$this->uploadDir->put($uploadName, $encodedImage);
$this->settings->set($this->filePathSettingKey, $uploadName);
$this->settings->set($filePathSettingKey, $uploadName);
return parent::handle($request);
return parent::handle(
// The parent controller expects a show forum request.
// `GET /api/forum`
$request->withMethod('GET')->withUri($request->getUri()->withPath('/api/forum'))
);
}
abstract protected function makeImage(UploadedFileInterface $file): EncodedImageInterface;
abstract protected function makeImage(UploadedFileInterface $file): EncodedImageInterface|StreamInterface;
protected function fileExtension(ServerRequestInterface $request, UploadedFileInterface $file): string
{
return $this->fileExtension;
}
protected function filePathSettingKey(ServerRequestInterface $request, UploadedFileInterface $file): string
{
return $this->filePathSettingKey;
}
protected function filenamePrefix(ServerRequestInterface $request): string
{
return $this->filenamePrefix;
}
}

View File

@ -9,10 +9,6 @@
namespace Flarum\Api\Controller;
use Flarum\Api\JsonApi;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Factory;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\EncodedImageInterface;
use Psr\Http\Message\UploadedFileInterface;
@ -21,21 +17,10 @@ class UploadLogoController extends UploadImageController
protected string $filePathSettingKey = 'logo_path';
protected string $filenamePrefix = 'logo';
public function __construct(
JsonApi $api,
SettingsRepositoryInterface $settings,
Factory $filesystemFactory,
protected ImageManager $imageManager
) {
parent::__construct($api, $settings, $filesystemFactory);
}
protected function makeImage(UploadedFileInterface $file): EncodedImageInterface
{
$encodedImage = $this->imageManager->read($file->getStream()->getMetadata('uri'))
return $this->imageManager->read($file->getStream()->getMetadata('uri'))
->scale(height: 60)
->toPng();
return $encodedImage;
}
}