diff --git a/framework/core/src/Api/Controller/UploadFaviconController.php b/framework/core/src/Api/Controller/UploadFaviconController.php index 35c3d1ad17..1892b6ce1c 100644 --- a/framework/core/src/Api/Controller/UploadFaviconController.php +++ b/framework/core/src/Api/Controller/UploadFaviconController.php @@ -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')) diff --git a/framework/core/src/Api/Controller/UploadImageController.php b/framework/core/src/Api/Controller/UploadImageController.php index ac760f1648..0d712e6efa 100644 --- a/framework/core/src/Api/Controller/UploadImageController.php +++ b/framework/core/src/Api/Controller/UploadImageController.php @@ -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 @@ -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 @@ 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; + } } diff --git a/framework/core/src/Api/Controller/UploadLogoController.php b/framework/core/src/Api/Controller/UploadLogoController.php index 396e14acbf..894b11e4c6 100644 --- a/framework/core/src/Api/Controller/UploadLogoController.php +++ b/framework/core/src/Api/Controller/UploadLogoController.php @@ -21,21 +21,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; } }