mirror of
https://github.com/flarum/core.git
synced 2025-10-26 21:21:28 +01: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,7 +9,6 @@
|
||||
|
||||
namespace Flarum\User\Command;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Foundation\DispatchEventsTrait;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Flarum\User\AvatarUploader;
|
||||
@@ -29,11 +28,6 @@ class UploadAvatarHandler
|
||||
*/
|
||||
protected $users;
|
||||
|
||||
/**
|
||||
* @var Application
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* @var AvatarUploader
|
||||
*/
|
||||
@@ -47,15 +41,13 @@ class UploadAvatarHandler
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
* @param UserRepository $users
|
||||
* @param Application $app
|
||||
* @param AvatarUploader $uploader
|
||||
* @param AvatarValidator $validator
|
||||
*/
|
||||
public function __construct(Dispatcher $events, UserRepository $users, Application $app, AvatarUploader $uploader, AvatarValidator $validator)
|
||||
public function __construct(Dispatcher $events, UserRepository $users, AvatarUploader $uploader, AvatarValidator $validator)
|
||||
{
|
||||
$this->events = $events;
|
||||
$this->users = $users;
|
||||
$this->app = $app;
|
||||
$this->uploader = $uploader;
|
||||
$this->validator = $validator;
|
||||
}
|
||||
@@ -76,26 +68,17 @@ class UploadAvatarHandler
|
||||
$this->assertCan($actor, 'edit', $user);
|
||||
}
|
||||
|
||||
$file = $command->file;
|
||||
$this->validator->assertValid(['avatar' => $command->file]);
|
||||
|
||||
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar');
|
||||
$file->moveTo($tmpFile);
|
||||
$image = (new ImageManager)->make($command->file->getStream());
|
||||
|
||||
try {
|
||||
$this->validator->assertValid(['avatar' => $file]);
|
||||
$this->events->dispatch(
|
||||
new AvatarSaving($user, $actor, $image)
|
||||
);
|
||||
|
||||
$image = (new ImageManager)->make($tmpFile);
|
||||
$this->uploader->upload($user, $image);
|
||||
|
||||
$this->events->dispatch(
|
||||
new AvatarSaving($user, $actor, $image)
|
||||
);
|
||||
|
||||
$this->uploader->upload($user, $image);
|
||||
|
||||
$user->save();
|
||||
} finally {
|
||||
@unlink($tmpFile);
|
||||
}
|
||||
$user->save();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user