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;
|
||||
|
||||
use Psr\Http\Message\UploadedFileInterface;
|
||||
use RuntimeException;
|
||||
|
||||
class UploadAvatarCommand
|
||||
|
@ -7,13 +8,13 @@ class UploadAvatarCommand
|
|||
public $userId;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\File\UploadedFile
|
||||
* @var \Psr\Http\Message\UploadedFileInterface
|
||||
*/
|
||||
public $file;
|
||||
|
||||
public $actor;
|
||||
|
||||
public function __construct($userId, $file, $actor)
|
||||
public function __construct($userId, UploadedFileInterface $file, $actor)
|
||||
{
|
||||
if (empty($userId) || !intval($userId)) {
|
||||
throw new RuntimeException('No valid user ID specified.');
|
||||
|
|
|
@ -40,14 +40,16 @@ class UploadAvatarCommandHandler
|
|||
// throw an exception otherwise.
|
||||
$user->assertCan($command->actor, 'edit');
|
||||
|
||||
$manager = new ImageManager(array('driver' => 'imagick'));
|
||||
$manager->make($command->file->getRealPath())->fit(100, 100)->save();
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'avatar');
|
||||
$command->file->moveTo($tmpFile);
|
||||
|
||||
$filename = $command->file->getFilename();
|
||||
$uploadName = Str::lower(Str::quickRandom()) . '.jpg';
|
||||
|
||||
$manager = new ImageManager(array('driver' => 'imagick'));
|
||||
$manager->make($tmpFile)->fit(100, 100)->save();
|
||||
|
||||
$mount = new MountManager([
|
||||
'source' => new Filesystem(new Local($command->file->getPath())),
|
||||
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
|
||||
'target' => $this->uploadDir,
|
||||
]);
|
||||
|
||||
|
@ -59,7 +61,7 @@ class UploadAvatarCommandHandler
|
|||
|
||||
event(new AvatarWillBeUploaded($user, $command));
|
||||
|
||||
$mount->move("source://$filename", "target://$uploadName");
|
||||
$mount->move("source://".pathinfo($tmpFile, PATHINFO_BASENAME), "target://$uploadName");
|
||||
|
||||
$user->save();
|
||||
$this->dispatchEventsFor($user);
|
||||
|
|
Loading…
Reference in New Issue
Block a user