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:
Toby Zerner 2015-06-19 16:26:16 +09:30
parent f571a40ca8
commit de3f9d82a0
2 changed files with 10 additions and 7 deletions

View File

@ -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.');

View File

@ -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);