1
0
mirror of https://github.com/flarum/core.git synced 2025-08-18 22:31:32 +02:00

Add ability to upload a logo + favicon, and add custom header HTML

Closes #268. Not going to bother with a preview SVG or anything fancy for now – we can think about that as part of #746. Right now it's just good to finally get this functionality in!

Also need to think about apple-touch-icon, msTile stuff, and social sharing image. Not sure if this is all too much for core, but it's definitely too much for the current Appearance page layout. Again, something to think about as part of #746.

Code is a bit rough around the edges, but figured there's not much point in using the command bus properly since #870.
This commit is contained in:
Toby Zerner
2016-06-04 18:05:46 +09:30
parent 01a6dccb83
commit feffe53a86
17 changed files with 811 additions and 92 deletions

View File

@@ -361,6 +361,34 @@ class ApiServiceProvider extends AbstractServiceProvider
$route->toController(Controller\SetPermissionController::class)
);
// Upload a logo
$routes->post(
'/logo',
'logo',
$route->toController(Controller\UploadLogoController::class)
);
// Remove the logo
$routes->delete(
'/logo',
'logo.delete',
$route->toController(Controller\DeleteLogoController::class)
);
// Upload a favicon
$routes->post(
'/favicon',
'favicon',
$route->toController(Controller\UploadFaviconController::class)
);
// Remove the favicon
$routes->delete(
'/favicon',
'favicon.delete',
$route->toController(Controller\DeleteFaviconController::class)
);
$this->app->make('events')->fire(
new ConfigureApiRoutes($routes, $route)
);

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\EmptyResponse;
class DeleteFaviconController extends AbstractDeleteController
{
use AssertPermissionTrait;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var Application
*/
protected $app;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Application $app)
{
$this->settings = $settings;
$this->app = $app;
}
/**
* {@inheritdoc}
*/
protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$path = $this->settings->get('favicon_path');
$this->settings->set('favicon_path', null);
$uploadDir = new Filesystem(new Local($this->app->publicPath().'/assets'));
if ($uploadDir->has($path)) {
$uploadDir->delete($path);
}
return new EmptyResponse(204);
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response\EmptyResponse;
class DeleteLogoController extends AbstractDeleteController
{
use AssertPermissionTrait;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var Application
*/
protected $app;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Application $app)
{
$this->settings = $settings;
$this->app = $app;
}
/**
* {@inheritdoc}
*/
protected function delete(ServerRequestInterface $request)
{
$this->assertAdmin($request->getAttribute('actor'));
$path = $this->settings->get('logo_path');
$this->settings->set('logo_path', null);
$uploadDir = new Filesystem(new Local($this->app->publicPath().'/assets'));
if ($uploadDir->has($path)) {
$uploadDir->delete($path);
}
return new EmptyResponse(204);
}
}

View File

@@ -0,0 +1,90 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\MountManager;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
class UploadFaviconController extends ShowForumController
{
use AssertPermissionTrait;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var Application
*/
protected $app;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Application $app)
{
$this->settings = $settings;
$this->app = $app;
}
/**
* {@inheritdoc}
*/
public function data(ServerRequestInterface $request, Document $document)
{
$this->assertAdmin($request->getAttribute('actor'));
$file = array_get($request->getUploadedFiles(), 'favicon');
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'favicon');
$file->moveTo($tmpFile);
$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
if ($extension !== 'ico') {
$manager = new ImageManager;
$encodedImage = $manager->make($tmpFile)->resize(64, 64, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->encode('png');
file_put_contents($tmpFile, $encodedImage);
$extension = 'png';
}
$mount = new MountManager([
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
'target' => new Filesystem(new Local($this->app->publicPath().'/assets')),
]);
if (($path = $this->settings->get('favicon_path')) && $mount->has($file = "target://$path")) {
$mount->delete($file);
}
$uploadName = 'favicon-'.Str::lower(Str::quickRandom(8)).'.'.$extension;
$mount->move('source://'.pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
$this->settings->set('favicon_path', $uploadName);
return parent::data($request, $document);
}
}

View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\MountManager;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
class UploadLogoController extends ShowForumController
{
use AssertPermissionTrait;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var Application
*/
protected $app;
/**
* @param SettingsRepositoryInterface $settings
*/
public function __construct(SettingsRepositoryInterface $settings, Application $app)
{
$this->settings = $settings;
$this->app = $app;
}
/**
* {@inheritdoc}
*/
public function data(ServerRequestInterface $request, Document $document)
{
$this->assertAdmin($request->getAttribute('actor'));
$file = array_get($request->getUploadedFiles(), 'logo');
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'logo');
$file->moveTo($tmpFile);
$manager = new ImageManager;
$encodedImage = $manager->make($tmpFile)->heighten(60, function ($constraint) {
$constraint->upsize();
})->encode('png');
file_put_contents($tmpFile, $encodedImage);
$mount = new MountManager([
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
'target' => new Filesystem(new Local($this->app->publicPath().'/assets')),
]);
if (($path = $this->settings->get('logo_path')) && $mount->has($file = "target://$path")) {
$mount->delete($file);
}
$uploadName = 'logo-'.Str::lower(Str::quickRandom(8)).'.png';
$mount->move('source://'.pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
$this->settings->set('logo_path', $uploadName);
return parent::data($request, $document);
}
}

View File

@@ -11,6 +11,7 @@
namespace Flarum\Api\Serializer;
use Flarum\Core\Access\Gate;
use Flarum\Forum\UrlGenerator;
use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface;
@@ -21,11 +22,6 @@ class ForumSerializer extends AbstractSerializer
*/
protected $type = 'forums';
/**
* @var Gate
*/
protected $gate;
/**
* @var Application
*/
@@ -37,15 +33,20 @@ class ForumSerializer extends AbstractSerializer
protected $settings;
/**
* @param Gate $gate
* @var UrlGenerator
*/
protected $url;
/**
* @param Application $app
* @param SettingsRepositoryInterface $settings
* @param UrlGenerator $url
*/
public function __construct(Gate $gate, Application $app, SettingsRepositoryInterface $settings)
public function __construct(Application $app, SettingsRepositoryInterface $settings, UrlGenerator $url)
{
$this->gate = $gate;
$this->app = $app;
$this->settings = $settings;
$this->url = $url;
}
/**
@@ -61,25 +62,27 @@ class ForumSerializer extends AbstractSerializer
*/
protected function getDefaultAttributes($model)
{
$gate = $this->gate->forUser($this->actor);
$attributes = [
'title' => $this->settings->get('forum_title'),
'description' => $this->settings->get('forum_description'),
'baseUrl' => $url = $this->app->url(),
'basePath' => parse_url($url, PHP_URL_PATH) ?: '',
'debug' => $this->app->inDebugMode(),
'apiUrl' => $this->app->url('api'),
'welcomeTitle' => $this->settings->get('welcome_title'),
'welcomeMessage' => $this->settings->get('welcome_message'),
'themePrimaryColor' => $this->settings->get('theme_primary_color'),
'allowSignUp' => (bool) $this->settings->get('allow_sign_up'),
'defaultRoute' => $this->settings->get('default_route'),
'canViewDiscussions' => $gate->allows('viewDiscussions'),
'canStartDiscussion' => $gate->allows('startDiscussion')
'title' => $this->settings->get('forum_title'),
'description' => $this->settings->get('forum_description'),
'baseUrl' => $url = $this->app->url(),
'basePath' => parse_url($url, PHP_URL_PATH) ?: '',
'debug' => $this->app->inDebugMode(),
'apiUrl' => $this->app->url('api'),
'welcomeTitle' => $this->settings->get('welcome_title'),
'welcomeMessage' => $this->settings->get('welcome_message'),
'themePrimaryColor' => $this->settings->get('theme_primary_color'),
'themeSecondaryColor' => $this->settings->get('theme_secondary_color'),
'logoUrl' => $this->getLogoUrl(),
'faviconUrl' => $this->getFaviconUrl(),
'headerHtml' => $this->settings->get('custom_header'),
'allowSignUp' => (bool) $this->settings->get('allow_sign_up'),
'defaultRoute' => $this->settings->get('default_route'),
'canViewDiscussions' => $this->actor->can('viewDiscussions'),
'canStartDiscussion' => $this->actor->can('startDiscussion')
];
if ($gate->allows('administrate')) {
if ($this->actor->can('administrate')) {
$attributes['adminUrl'] = $this->app->url('admin');
$attributes['version'] = $this->app->version();
}
@@ -94,4 +97,24 @@ class ForumSerializer extends AbstractSerializer
{
return $this->hasMany($model, 'Flarum\Api\Serializer\GroupSerializer');
}
/**
* @return null|string
*/
protected function getLogoUrl()
{
$logoPath = $this->settings->get('logo_path');
return $logoPath ? $this->url->toPath('assets/'.$logoPath) : null;
}
/**
* @return null|string
*/
protected function getFaviconUrl()
{
$faviconPath = $this->settings->get('favicon_path');
return $faviconPath ? $this->url->toPath('assets/'.$faviconPath) : null;
}
}