mirror of
https://github.com/flarum/core.git
synced 2025-08-08 01:16:52 +02:00
feat: recover support for ico favicon (#4126)
This commit is contained in:
@@ -9,13 +9,8 @@
|
|||||||
|
|
||||||
namespace Flarum\Api\Controller;
|
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 Intervention\Image\Interfaces\EncodedImageInterface;
|
||||||
|
use Psr\Http\Message\StreamInterface;
|
||||||
use Psr\Http\Message\UploadedFileInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
|
||||||
class UploadFaviconController extends UploadImageController
|
class UploadFaviconController extends UploadImageController
|
||||||
@@ -23,28 +18,12 @@ class UploadFaviconController extends UploadImageController
|
|||||||
protected string $filePathSettingKey = 'favicon_path';
|
protected string $filePathSettingKey = 'favicon_path';
|
||||||
protected string $filenamePrefix = 'favicon';
|
protected string $filenamePrefix = 'favicon';
|
||||||
|
|
||||||
public function __construct(
|
protected function makeImage(UploadedFileInterface $file): EncodedImageInterface|StreamInterface
|
||||||
JsonApi $api,
|
|
||||||
SettingsRepositoryInterface $settings,
|
|
||||||
Factory $filesystemFactory,
|
|
||||||
protected TranslatorInterface $translator,
|
|
||||||
protected ImageManager $imageManager
|
|
||||||
) {
|
|
||||||
parent::__construct($api, $settings, $filesystemFactory);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function makeImage(UploadedFileInterface $file): EncodedImageInterface
|
|
||||||
{
|
{
|
||||||
$this->fileExtension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
$this->fileExtension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
||||||
|
|
||||||
if ($this->fileExtension === 'ico') {
|
if ($this->fileExtension === 'ico') {
|
||||||
// @todo remove in 2.0
|
return $file->getStream();
|
||||||
throw new ValidationException([
|
|
||||||
'message' => strtr($this->translator->trans('validation.mimes'), [
|
|
||||||
':attribute' => 'favicon',
|
|
||||||
':values' => 'jpeg,png,gif,webp',
|
|
||||||
])
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$encodedImage = $this->imageManager->read($file->getStream()->getMetadata('uri'))
|
$encodedImage = $this->imageManager->read($file->getStream()->getMetadata('uri'))
|
||||||
|
@@ -11,14 +11,17 @@ namespace Flarum\Api\Controller;
|
|||||||
|
|
||||||
use Flarum\Api\JsonApi;
|
use Flarum\Api\JsonApi;
|
||||||
use Flarum\Http\RequestUtil;
|
use Flarum\Http\RequestUtil;
|
||||||
|
use Flarum\Locale\TranslatorInterface;
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Illuminate\Contracts\Filesystem\Factory;
|
use Illuminate\Contracts\Filesystem\Factory;
|
||||||
use Illuminate\Contracts\Filesystem\Filesystem;
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Intervention\Image\ImageManager;
|
||||||
use Intervention\Image\Interfaces\EncodedImageInterface;
|
use Intervention\Image\Interfaces\EncodedImageInterface;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Message\StreamInterface;
|
||||||
use Psr\Http\Message\UploadedFileInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
|
||||||
abstract class UploadImageController extends ShowForumController
|
abstract class UploadImageController extends ShowForumController
|
||||||
@@ -31,6 +34,8 @@ abstract class UploadImageController extends ShowForumController
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
JsonApi $api,
|
JsonApi $api,
|
||||||
protected SettingsRepositoryInterface $settings,
|
protected SettingsRepositoryInterface $settings,
|
||||||
|
protected ImageManager $imageManager,
|
||||||
|
protected TranslatorInterface $translator,
|
||||||
Factory $filesystemFactory
|
Factory $filesystemFactory
|
||||||
) {
|
) {
|
||||||
parent::__construct($api);
|
parent::__construct($api);
|
||||||
@@ -42,22 +47,45 @@ abstract class UploadImageController extends ShowForumController
|
|||||||
{
|
{
|
||||||
RequestUtil::getActor($request)->assertAdmin();
|
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);
|
$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);
|
$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->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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,10 +9,6 @@
|
|||||||
|
|
||||||
namespace Flarum\Api\Controller;
|
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 Intervention\Image\Interfaces\EncodedImageInterface;
|
||||||
use Psr\Http\Message\UploadedFileInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
|
||||||
@@ -21,21 +17,10 @@ class UploadLogoController extends UploadImageController
|
|||||||
protected string $filePathSettingKey = 'logo_path';
|
protected string $filePathSettingKey = 'logo_path';
|
||||||
protected string $filenamePrefix = 'logo';
|
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
|
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)
|
->scale(height: 60)
|
||||||
->toPng();
|
->toPng();
|
||||||
|
|
||||||
return $encodedImage;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user