Skip to content

Commit

Permalink
feat: recover support for ico favicon (#4126)
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 authored Nov 22, 2024
1 parent 49064f6 commit 41e5ff2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 46 deletions.
27 changes: 3 additions & 24 deletions framework/core/src/Api/Controller/UploadFaviconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,21 @@

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
{
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'))
Expand Down
40 changes: 34 additions & 6 deletions framework/core/src/Api/Controller/UploadImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

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
Expand All @@ -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);
Expand All @@ -42,22 +47,45 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
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(
// 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|StreamInterface;

return parent::handle($request);
protected function fileExtension(ServerRequestInterface $request, UploadedFileInterface $file): string
{
return $this->fileExtension;
}

abstract protected function makeImage(UploadedFileInterface $file): EncodedImageInterface;
protected function filePathSettingKey(ServerRequestInterface $request, UploadedFileInterface $file): string
{
return $this->filePathSettingKey;
}

protected function filenamePrefix(ServerRequestInterface $request): string
{
return $this->filenamePrefix;
}
}
17 changes: 1 addition & 16 deletions framework/core/src/Api/Controller/UploadLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}

0 comments on commit 41e5ff2

Please sign in to comment.