mirror of
https://github.com/flarum/core.git
synced 2025-10-12 07:24:27 +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:
63
src/Api/Controller/DeleteFaviconController.php
Normal file
63
src/Api/Controller/DeleteFaviconController.php
Normal 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);
|
||||
}
|
||||
}
|
63
src/Api/Controller/DeleteLogoController.php
Normal file
63
src/Api/Controller/DeleteLogoController.php
Normal 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);
|
||||
}
|
||||
}
|
90
src/Api/Controller/UploadFaviconController.php
Normal file
90
src/Api/Controller/UploadFaviconController.php
Normal 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);
|
||||
}
|
||||
}
|
83
src/Api/Controller/UploadLogoController.php
Normal file
83
src/Api/Controller/UploadLogoController.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user