From ea98e4bda97d50f3052a6e351f0b040f0b5a2104 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 22 Oct 2015 10:39:38 +1030 Subject: [PATCH] More helpful avatar upload error messages ref #165, #118 --- js/lib/App.js | 2 +- src/Core/Command/UploadAvatarHandler.php | 27 +++++++++++++++++++++--- src/Core/Validator/AbstractValidator.php | 22 +------------------ src/Core/Validator/AvatarValidator.php | 22 +++++++++++++++++++ 4 files changed, 48 insertions(+), 25 deletions(-) create mode 100644 src/Core/Validator/AvatarValidator.php diff --git a/js/lib/App.js b/js/lib/App.js index f1d05594e..d7083d9e6 100644 --- a/js/lib/App.js +++ b/js/lib/App.js @@ -206,7 +206,7 @@ export default class App { try { return JSON.parse(responseText); } catch (e) { - throw new RequestError(500, responseText, options, xhr); + throw new RequestError(500, responseText, options); } }); diff --git a/src/Core/Command/UploadAvatarHandler.php b/src/Core/Command/UploadAvatarHandler.php index 3400bd2e0..a22e74a15 100644 --- a/src/Core/Command/UploadAvatarHandler.php +++ b/src/Core/Command/UploadAvatarHandler.php @@ -11,17 +11,19 @@ namespace Flarum\Core\Command; use Flarum\Core\Access\AssertPermissionTrait; -use Flarum\Event\AvatarWillBeSaved; use Flarum\Core\Repository\UserRepository; use Flarum\Core\Support\DispatchEventsTrait; +use Flarum\Core\Validator\AvatarValidator; +use Flarum\Event\AvatarWillBeSaved; use Flarum\Foundation\Application; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Str; +use Intervention\Image\ImageManager; use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; use League\Flysystem\FilesystemInterface; use League\Flysystem\MountManager; -use Intervention\Image\ImageManager; +use Symfony\Component\HttpFoundation\File\UploadedFile; class UploadAvatarHandler { @@ -43,17 +45,25 @@ class UploadAvatarHandler */ protected $app; + /** + * @var AvatarValidator + */ + protected $validator; + /** * @param Dispatcher $events * @param UserRepository $users * @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->users = $users; $this->uploadDir = $uploadDir; $this->app = $app; + $this->validator = $validator; } /** @@ -74,6 +84,17 @@ class UploadAvatarHandler $tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar'); $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->make($tmpFile)->fit(100, 100)->save(); diff --git a/src/Core/Validator/AbstractValidator.php b/src/Core/Validator/AbstractValidator.php index 7231262bb..aa2d40abc 100644 --- a/src/Core/Validator/AbstractValidator.php +++ b/src/Core/Validator/AbstractValidator.php @@ -51,17 +51,6 @@ abstract class AbstractValidator $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. * @@ -72,7 +61,7 @@ abstract class AbstractValidator $validator = $this->makeValidator($attributes); if ($validator->fails()) { - $this->throwValidationException($validator); + throw new ValidationException($validator); } } @@ -92,15 +81,6 @@ abstract class AbstractValidator return []; } - /** - * @param Validator $validator - * @throws ValidationException - */ - protected function throwValidationException(Validator $validator) - { - throw new ValidationException($validator); - } - /** * Make a new validator instance for this model. * diff --git a/src/Core/Validator/AvatarValidator.php b/src/Core/Validator/AvatarValidator.php new file mode 100644 index 000000000..4b9957a77 --- /dev/null +++ b/src/Core/Validator/AvatarValidator.php @@ -0,0 +1,22 @@ + + * + * 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' + ] + ]; +}