mirror of
https://github.com/flarum/framework.git
synced 2024-12-12 06:03:39 +08: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:
parent
f571a40ca8
commit
de3f9d82a0
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user