mirror of
https://github.com/flarum/core.git
synced 2025-07-29 20:50:28 +02:00
DRY up image uploading code (#2477)
This commit is contained in:
@@ -9,69 +9,36 @@
|
|||||||
|
|
||||||
namespace Flarum\Api\Controller;
|
namespace Flarum\Api\Controller;
|
||||||
|
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Intervention\Image\Image;
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use Intervention\Image\ImageManager;
|
use Intervention\Image\ImageManager;
|
||||||
use League\Flysystem\FilesystemInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
use Tobscure\JsonApi\Document;
|
|
||||||
|
|
||||||
class UploadFaviconController extends ShowForumController
|
class UploadFaviconController extends UploadImageController
|
||||||
{
|
{
|
||||||
/**
|
protected $filePathSettingKey = 'favicon_path';
|
||||||
* @var SettingsRepositoryInterface
|
|
||||||
*/
|
|
||||||
protected $settings;
|
|
||||||
|
|
||||||
/**
|
protected $filenamePrefix = 'favicon';
|
||||||
* @var FilesystemInterface
|
|
||||||
*/
|
|
||||||
protected $uploadDir;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param SettingsRepositoryInterface $settings
|
|
||||||
* @param FilesystemInterface $uploadDir
|
|
||||||
*/
|
|
||||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
|
||||||
{
|
|
||||||
$this->settings = $settings;
|
|
||||||
$this->uploadDir = $uploadDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function data(ServerRequestInterface $request, Document $document)
|
protected function makeImage(UploadedFileInterface $file): Image
|
||||||
{
|
{
|
||||||
$request->getAttribute('actor')->assertAdmin();
|
$this->fileExtension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
||||||
|
|
||||||
$file = Arr::get($request->getUploadedFiles(), 'favicon');
|
if ($this->fileExtension === 'ico') {
|
||||||
$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
$encodedImage = $file->getStream();
|
||||||
|
|
||||||
if ($extension === 'ico') {
|
|
||||||
$image = $file->getStream();
|
|
||||||
} else {
|
} else {
|
||||||
$manager = new ImageManager;
|
$manager = new ImageManager();
|
||||||
|
|
||||||
$image = $manager->make($file->getStream())->resize(64, 64, function ($constraint) {
|
$encodedImage = $manager->make($file->getStream())->resize(64, 64, function ($constraint) {
|
||||||
$constraint->aspectRatio();
|
$constraint->aspectRatio();
|
||||||
$constraint->upsize();
|
$constraint->upsize();
|
||||||
})->encode('png');
|
})->encode('png');
|
||||||
|
|
||||||
$extension = 'png';
|
$this->fileExtension = 'png';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($path = $this->settings->get('favicon_path')) && $this->uploadDir->has($path)) {
|
return $encodedImage;
|
||||||
$this->uploadDir->delete($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$uploadName = 'favicon-'.Str::lower(Str::random(8)).'.'.$extension;
|
|
||||||
|
|
||||||
$this->uploadDir->write($uploadName, $image);
|
|
||||||
|
|
||||||
$this->settings->set('favicon_path', $uploadName);
|
|
||||||
|
|
||||||
return parent::data($request, $document);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
87
framework/core/src/Api/Controller/UploadImageController.php
Normal file
87
framework/core/src/Api/Controller/UploadImageController.php
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Api\Controller;
|
||||||
|
|
||||||
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Intervention\Image\Image;
|
||||||
|
use League\Flysystem\FilesystemInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
|
use Tobscure\JsonApi\Document;
|
||||||
|
|
||||||
|
abstract class UploadImageController extends ShowForumController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var SettingsRepositoryInterface
|
||||||
|
*/
|
||||||
|
protected $settings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var FilesystemInterface
|
||||||
|
*/
|
||||||
|
protected $uploadDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $fileExtension = 'png';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $filePathSettingKey = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $filenamePrefix = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param SettingsRepositoryInterface $settings
|
||||||
|
* @param FilesystemInterface $uploadDir
|
||||||
|
*/
|
||||||
|
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||||
|
{
|
||||||
|
$this->settings = $settings;
|
||||||
|
$this->uploadDir = $uploadDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function data(ServerRequestInterface $request, Document $document)
|
||||||
|
{
|
||||||
|
$request->getAttribute('actor')->assertAdmin();
|
||||||
|
|
||||||
|
$file = Arr::get($request->getUploadedFiles(), $this->filenamePrefix);
|
||||||
|
|
||||||
|
$encodedImage = $this->makeImage($file);
|
||||||
|
|
||||||
|
if (($path = $this->settings->get($this->filePathSettingKey)) && $this->uploadDir->has($path)) {
|
||||||
|
$this->uploadDir->delete($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$uploadName = $this->filenamePrefix.'-'.Str::lower(Str::random(8)).'.'.$this->fileExtension;
|
||||||
|
|
||||||
|
$this->uploadDir->write($uploadName, $encodedImage);
|
||||||
|
|
||||||
|
$this->settings->set($this->filePathSettingKey, $uploadName);
|
||||||
|
|
||||||
|
return parent::data($request, $document);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param UploadedFileInterface $file
|
||||||
|
* @return Image
|
||||||
|
*/
|
||||||
|
abstract protected function makeImage(UploadedFileInterface $file): Image;
|
||||||
|
}
|
@@ -9,61 +9,27 @@
|
|||||||
|
|
||||||
namespace Flarum\Api\Controller;
|
namespace Flarum\Api\Controller;
|
||||||
|
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Intervention\Image\Image;
|
||||||
use Illuminate\Support\Arr;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use Intervention\Image\ImageManager;
|
use Intervention\Image\ImageManager;
|
||||||
use League\Flysystem\FilesystemInterface;
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
use Tobscure\JsonApi\Document;
|
|
||||||
|
|
||||||
class UploadLogoController extends ShowForumController
|
class UploadLogoController extends UploadImageController
|
||||||
{
|
{
|
||||||
/**
|
protected $filePathSettingKey = 'logo_path';
|
||||||
* @var SettingsRepositoryInterface
|
|
||||||
*/
|
|
||||||
protected $settings;
|
|
||||||
|
|
||||||
/**
|
protected $filenamePrefix = 'logo';
|
||||||
* @var FilesystemInterface
|
|
||||||
*/
|
|
||||||
protected $uploadDir;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param SettingsRepositoryInterface $settings
|
|
||||||
* @param FilesystemInterface $uploadDir
|
|
||||||
*/
|
|
||||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
|
||||||
{
|
|
||||||
$this->settings = $settings;
|
|
||||||
$this->uploadDir = $uploadDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function data(ServerRequestInterface $request, Document $document)
|
protected function makeImage(UploadedFileInterface $file): Image
|
||||||
{
|
{
|
||||||
$request->getAttribute('actor')->assertAdmin();
|
$manager = new ImageManager();
|
||||||
|
|
||||||
$file = Arr::get($request->getUploadedFiles(), 'logo');
|
|
||||||
|
|
||||||
$manager = new ImageManager;
|
|
||||||
|
|
||||||
$encodedImage = $manager->make($file->getStream())->heighten(60, function ($constraint) {
|
$encodedImage = $manager->make($file->getStream())->heighten(60, function ($constraint) {
|
||||||
$constraint->upsize();
|
$constraint->upsize();
|
||||||
})->encode('png');
|
})->encode('png');
|
||||||
|
|
||||||
if (($path = $this->settings->get('logo_path')) && $this->uploadDir->has($path)) {
|
return $encodedImage;
|
||||||
$this->uploadDir->delete($path);
|
|
||||||
}
|
|
||||||
|
|
||||||
$uploadName = 'logo-'.Str::lower(Str::random(8)).'.png';
|
|
||||||
|
|
||||||
$this->uploadDir->write($uploadName, $encodedImage);
|
|
||||||
|
|
||||||
$this->settings->set('logo_path', $uploadName);
|
|
||||||
|
|
||||||
return parent::data($request, $document);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user