More helpful avatar upload error messages

ref #165, #118
This commit is contained in:
Toby Zerner
2015-10-22 10:39:38 +10:30
parent a471a44ca6
commit ea98e4bda9
4 changed files with 48 additions and 25 deletions

View File

@ -206,7 +206,7 @@ export default class App {
try { try {
return JSON.parse(responseText); return JSON.parse(responseText);
} catch (e) { } catch (e) {
throw new RequestError(500, responseText, options, xhr); throw new RequestError(500, responseText, options);
} }
}); });

View File

@ -11,17 +11,19 @@
namespace Flarum\Core\Command; namespace Flarum\Core\Command;
use Flarum\Core\Access\AssertPermissionTrait; use Flarum\Core\Access\AssertPermissionTrait;
use Flarum\Event\AvatarWillBeSaved;
use Flarum\Core\Repository\UserRepository; use Flarum\Core\Repository\UserRepository;
use Flarum\Core\Support\DispatchEventsTrait; use Flarum\Core\Support\DispatchEventsTrait;
use Flarum\Core\Validator\AvatarValidator;
use Flarum\Event\AvatarWillBeSaved;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
use League\Flysystem\Adapter\Local; use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem; use League\Flysystem\Filesystem;
use League\Flysystem\FilesystemInterface; use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager; use League\Flysystem\MountManager;
use Intervention\Image\ImageManager; use Symfony\Component\HttpFoundation\File\UploadedFile;
class UploadAvatarHandler class UploadAvatarHandler
{ {
@ -43,17 +45,25 @@ class UploadAvatarHandler
*/ */
protected $app; protected $app;
/**
* @var AvatarValidator
*/
protected $validator;
/** /**
* @param Dispatcher $events * @param Dispatcher $events
* @param UserRepository $users * @param UserRepository $users
* @param FilesystemInterface $uploadDir * @param FilesystemInterface $uploadDir
* @param Application $app
* @param AvatarValidator $validator
*/ */
public function __construct(Dispatcher $events, UserRepository $users, FilesystemInterface $uploadDir, Application $app) public function __construct(Dispatcher $events, UserRepository $users, FilesystemInterface $uploadDir, Application $app, AvatarValidator $validator)
{ {
$this->events = $events; $this->events = $events;
$this->users = $users; $this->users = $users;
$this->uploadDir = $uploadDir; $this->uploadDir = $uploadDir;
$this->app = $app; $this->app = $app;
$this->validator = $validator;
} }
/** /**
@ -74,6 +84,17 @@ class UploadAvatarHandler
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar'); $tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar');
$command->file->moveTo($tmpFile); $command->file->moveTo($tmpFile);
$file = new UploadedFile(
$tmpFile,
$command->file->getClientFilename(),
$command->file->getClientMediaType(),
$command->file->getSize(),
$command->file->getError(),
true
);
$this->validator->assertValid(['avatar' => $file]);
$manager = new ImageManager; $manager = new ImageManager;
$manager->make($tmpFile)->fit(100, 100)->save(); $manager->make($tmpFile)->fit(100, 100)->save();

View File

@ -51,17 +51,6 @@ abstract class AbstractValidator
$this->translator = $translator; $this->translator = $translator;
} }
/**
* Check whether a model is valid.
*
* @param array $attributes
* @return bool
*/
public function valid(array $attributes)
{
return $this->makeValidator($attributes)->passes();
}
/** /**
* Throw an exception if a model is not valid. * Throw an exception if a model is not valid.
* *
@ -72,7 +61,7 @@ abstract class AbstractValidator
$validator = $this->makeValidator($attributes); $validator = $this->makeValidator($attributes);
if ($validator->fails()) { if ($validator->fails()) {
$this->throwValidationException($validator); throw new ValidationException($validator);
} }
} }
@ -92,15 +81,6 @@ abstract class AbstractValidator
return []; return [];
} }
/**
* @param Validator $validator
* @throws ValidationException
*/
protected function throwValidationException(Validator $validator)
{
throw new ValidationException($validator);
}
/** /**
* Make a new validator instance for this model. * Make a new validator instance for this model.
* *

View File

@ -0,0 +1,22 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Core\Validator;
class AvatarValidator extends AbstractValidator
{
protected $rules = [
'avatar' => [
'required',
'image',
'max:1024'
]
];
}