mirror of
https://github.com/flarum/framework.git
synced 2024-11-26 02:10:09 +08:00
DRY up image uploading code (#2477)
This commit is contained in:
parent
55070ede84
commit
f0484cab74
|
@ -9,69 +9,36 @@
|
|||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\Image;
|
||||
use Intervention\Image\ImageManager;
|
||||
use League\Flysystem\FilesystemInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
class UploadFaviconController extends ShowForumController
|
||||
class UploadFaviconController extends UploadImageController
|
||||
{
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
protected $filePathSettingKey = 'favicon_path';
|
||||
|
||||
/**
|
||||
* @var FilesystemInterface
|
||||
*/
|
||||
protected $uploadDir;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param FilesystemInterface $uploadDir
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->uploadDir = $uploadDir;
|
||||
}
|
||||
protected $filenamePrefix = 'favicon';
|
||||
|
||||
/**
|
||||
* {@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');
|
||||
$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
||||
|
||||
if ($extension === 'ico') {
|
||||
$image = $file->getStream();
|
||||
if ($this->fileExtension === 'ico') {
|
||||
$encodedImage = $file->getStream();
|
||||
} 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->upsize();
|
||||
})->encode('png');
|
||||
|
||||
$extension = 'png';
|
||||
$this->fileExtension = 'png';
|
||||
}
|
||||
|
||||
if (($path = $this->settings->get('favicon_path')) && $this->uploadDir->has($path)) {
|
||||
$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);
|
||||
return $encodedImage;
|
||||
}
|
||||
}
|
||||
|
|
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;
|
||||
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\Image;
|
||||
use Intervention\Image\ImageManager;
|
||||
use League\Flysystem\FilesystemInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
|
||||
class UploadLogoController extends ShowForumController
|
||||
class UploadLogoController extends UploadImageController
|
||||
{
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
protected $filePathSettingKey = 'logo_path';
|
||||
|
||||
/**
|
||||
* @var FilesystemInterface
|
||||
*/
|
||||
protected $uploadDir;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param FilesystemInterface $uploadDir
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->uploadDir = $uploadDir;
|
||||
}
|
||||
protected $filenamePrefix = 'logo';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function data(ServerRequestInterface $request, Document $document)
|
||||
protected function makeImage(UploadedFileInterface $file): Image
|
||||
{
|
||||
$request->getAttribute('actor')->assertAdmin();
|
||||
|
||||
$file = Arr::get($request->getUploadedFiles(), 'logo');
|
||||
|
||||
$manager = new ImageManager;
|
||||
$manager = new ImageManager();
|
||||
|
||||
$encodedImage = $manager->make($file->getStream())->heighten(60, function ($constraint) {
|
||||
$constraint->upsize();
|
||||
})->encode('png');
|
||||
|
||||
if (($path = $this->settings->get('logo_path')) && $this->uploadDir->has($path)) {
|
||||
$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);
|
||||
return $encodedImage;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user