mirror of
https://github.com/flarum/core.git
synced 2025-10-13 07:54:25 +02:00
Simplify uploads, inject filesystem instances
This avoids injecting the Application god class and assembling default file locations in multiple places. In addition, we no longer use the `MountManager` for these uploads. It only added complexity (by moving tmp files around) and will not be available in the next major release of Flysystem. Note: Passing PSR upload streams to Intervention Image requires an explicit upgrade of the library. (Very likely, users have already updated to the newer versions, as the old constraint allowed it, but we should be explicit for correctness' sake.)
This commit is contained in:
@@ -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