mirror of
https://github.com/flarum/core.git
synced 2025-08-06 00:17:31 +02:00
Merge pull request #2117 from flarum/fl/2055-streamline-uploads
Simplify uploads, avoid Application contract
This commit is contained in:
@@ -9,12 +9,10 @@
|
||||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Laminas\Diactoros\Response\EmptyResponse;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\FilesystemInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class DeleteFaviconController extends AbstractDeleteController
|
||||
@@ -27,17 +25,18 @@ class DeleteFaviconController extends AbstractDeleteController
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
* @var FilesystemInterface
|
||||
*/
|
||||
protected $app;
|
||||
protected $uploadDir;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param FilesystemInterface $uploadDir
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, Application $app)
|
||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->app = $app;
|
||||
$this->uploadDir = $uploadDir;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,10 +50,8 @@ class DeleteFaviconController extends AbstractDeleteController
|
||||
|
||||
$this->settings->set('favicon_path', null);
|
||||
|
||||
$uploadDir = new Filesystem(new Local($this->app->publicPath().'/assets'));
|
||||
|
||||
if ($uploadDir->has($path)) {
|
||||
$uploadDir->delete($path);
|
||||
if ($this->uploadDir->has($path)) {
|
||||
$this->uploadDir->delete($path);
|
||||
}
|
||||
|
||||
return new EmptyResponse(204);
|
||||
|
@@ -9,12 +9,10 @@
|
||||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Laminas\Diactoros\Response\EmptyResponse;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\FilesystemInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class DeleteLogoController extends AbstractDeleteController
|
||||
@@ -27,17 +25,18 @@ class DeleteLogoController extends AbstractDeleteController
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
* @var FilesystemInterface
|
||||
*/
|
||||
protected $app;
|
||||
protected $uploadDir;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param FilesystemInterface $uploadDir
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, Application $app)
|
||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->app = $app;
|
||||
$this->uploadDir = $uploadDir;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,10 +50,8 @@ class DeleteLogoController extends AbstractDeleteController
|
||||
|
||||
$this->settings->set('logo_path', null);
|
||||
|
||||
$uploadDir = new Filesystem(new Local($this->app->publicPath().'/assets'));
|
||||
|
||||
if ($uploadDir->has($path)) {
|
||||
$uploadDir->delete($path);
|
||||
if ($this->uploadDir->has($path)) {
|
||||
$this->uploadDir->delete($path);
|
||||
}
|
||||
|
||||
return new EmptyResponse(204);
|
||||
|
@@ -9,15 +9,12 @@
|
||||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManager;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\MountManager;
|
||||
use League\Flysystem\FilesystemInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
@@ -31,17 +28,18 @@ class UploadFaviconController extends ShowForumController
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
* @var FilesystemInterface
|
||||
*/
|
||||
protected $app;
|
||||
protected $uploadDir;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param FilesystemInterface $uploadDir
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, Application $app)
|
||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->app = $app;
|
||||
$this->uploadDir = $uploadDir;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,36 +50,28 @@ class UploadFaviconController extends ShowForumController
|
||||
$this->assertAdmin($request->getAttribute('actor'));
|
||||
|
||||
$file = Arr::get($request->getUploadedFiles(), 'favicon');
|
||||
|
||||
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'favicon');
|
||||
$file->moveTo($tmpFile);
|
||||
|
||||
$extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
|
||||
|
||||
if ($extension !== 'ico') {
|
||||
if ($extension === 'ico') {
|
||||
$image = $file->getStream();
|
||||
} else {
|
||||
$manager = new ImageManager;
|
||||
|
||||
$encodedImage = $manager->make($tmpFile)->resize(64, 64, function ($constraint) {
|
||||
$image = $manager->make($file->getStream())->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);
|
||||
if (($path = $this->settings->get('favicon_path')) && $this->uploadDir->has($path)) {
|
||||
$this->uploadDir->delete($path);
|
||||
}
|
||||
|
||||
$uploadName = 'favicon-'.Str::lower(Str::random(8)).'.'.$extension;
|
||||
|
||||
$mount->move('source://'.pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
|
||||
$this->uploadDir->write($uploadName, $image);
|
||||
|
||||
$this->settings->set('favicon_path', $uploadName);
|
||||
|
||||
|
@@ -9,15 +9,12 @@
|
||||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\ImageManager;
|
||||
use League\Flysystem\Adapter\Local;
|
||||
use League\Flysystem\Filesystem;
|
||||
use League\Flysystem\MountManager;
|
||||
use League\Flysystem\FilesystemInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
@@ -31,17 +28,18 @@ class UploadLogoController extends ShowForumController
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
* @var FilesystemInterface
|
||||
*/
|
||||
protected $app;
|
||||
protected $uploadDir;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param FilesystemInterface $uploadDir
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings, Application $app)
|
||||
public function __construct(SettingsRepositoryInterface $settings, FilesystemInterface $uploadDir)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
$this->app = $app;
|
||||
$this->uploadDir = $uploadDir;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,28 +51,19 @@ class UploadLogoController extends ShowForumController
|
||||
|
||||
$file = Arr::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) {
|
||||
$encodedImage = $manager->make($file->getStream())->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);
|
||||
if (($path = $this->settings->get('logo_path')) && $this->uploadDir->has($path)) {
|
||||
$this->uploadDir->delete($path);
|
||||
}
|
||||
|
||||
$uploadName = 'logo-'.Str::lower(Str::random(8)).'.png';
|
||||
|
||||
$mount->move('source://'.pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
|
||||
$this->uploadDir->write($uploadName, $encodedImage);
|
||||
|
||||
$this->settings->set('logo_path', $uploadName);
|
||||
|
||||
|
Reference in New Issue
Block a user