mirror of
https://github.com/flarum/core.git
synced 2025-08-04 07:27:39 +02:00
Update avatar uploading code for psr-7
Not sure if a tmp file is the best way, but it works
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
<?php namespace Flarum\Core\Commands;
|
<?php namespace Flarum\Core\Commands;
|
||||||
|
|
||||||
|
use Psr\Http\Message\UploadedFileInterface;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
class UploadAvatarCommand
|
class UploadAvatarCommand
|
||||||
@@ -7,13 +8,13 @@ class UploadAvatarCommand
|
|||||||
public $userId;
|
public $userId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Symfony\Component\HttpFoundation\File\UploadedFile
|
* @var \Psr\Http\Message\UploadedFileInterface
|
||||||
*/
|
*/
|
||||||
public $file;
|
public $file;
|
||||||
|
|
||||||
public $actor;
|
public $actor;
|
||||||
|
|
||||||
public function __construct($userId, $file, $actor)
|
public function __construct($userId, UploadedFileInterface $file, $actor)
|
||||||
{
|
{
|
||||||
if (empty($userId) || !intval($userId)) {
|
if (empty($userId) || !intval($userId)) {
|
||||||
throw new RuntimeException('No valid user ID specified.');
|
throw new RuntimeException('No valid user ID specified.');
|
||||||
|
@@ -40,14 +40,16 @@ class UploadAvatarCommandHandler
|
|||||||
// throw an exception otherwise.
|
// throw an exception otherwise.
|
||||||
$user->assertCan($command->actor, 'edit');
|
$user->assertCan($command->actor, 'edit');
|
||||||
|
|
||||||
$manager = new ImageManager(array('driver' => 'imagick'));
|
$tmpFile = tempnam(sys_get_temp_dir(), 'avatar');
|
||||||
$manager->make($command->file->getRealPath())->fit(100, 100)->save();
|
$command->file->moveTo($tmpFile);
|
||||||
|
|
||||||
$filename = $command->file->getFilename();
|
|
||||||
$uploadName = Str::lower(Str::quickRandom()) . '.jpg';
|
$uploadName = Str::lower(Str::quickRandom()) . '.jpg';
|
||||||
|
|
||||||
|
$manager = new ImageManager(array('driver' => 'imagick'));
|
||||||
|
$manager->make($tmpFile)->fit(100, 100)->save();
|
||||||
|
|
||||||
$mount = new MountManager([
|
$mount = new MountManager([
|
||||||
'source' => new Filesystem(new Local($command->file->getPath())),
|
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
|
||||||
'target' => $this->uploadDir,
|
'target' => $this->uploadDir,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -59,7 +61,7 @@ class UploadAvatarCommandHandler
|
|||||||
|
|
||||||
event(new AvatarWillBeUploaded($user, $command));
|
event(new AvatarWillBeUploaded($user, $command));
|
||||||
|
|
||||||
$mount->move("source://$filename", "target://$uploadName");
|
$mount->move("source://".pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
|
||||||
|
|
||||||
$user->save();
|
$user->save();
|
||||||
$this->dispatchEventsFor($user);
|
$this->dispatchEventsFor($user);
|
||||||
|
Reference in New Issue
Block a user