Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY up image uploading code #2477

Merged
merged 4 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 13 additions & 46 deletions src/Api/Controller/UploadFaviconController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

$file = Arr::get($request->getUploadedFiles(), 'favicon');
$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
$this->fileExtension = 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';
}

if (($path = $this->settings->get('favicon_path')) && $this->uploadDir->has($path)) {
$this->uploadDir->delete($path);
$this->fileExtension = 'png';
}

$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 changes: 87 additions & 0 deletions src/Api/Controller/UploadImageController.php
Original file line number Diff line number Diff line change
@@ -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;
}
50 changes: 8 additions & 42 deletions src/Api/Controller/UploadLogoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}