mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 08:46:25 +08:00
Validate avatar URL
Still needs refactor
This commit is contained in:
parent
ce02387ee4
commit
a61929730e
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
namespace Flarum\Core\Command;
|
namespace Flarum\Core\Command;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Flarum\Core\Access\AssertPermissionTrait;
|
use Flarum\Core\Access\AssertPermissionTrait;
|
||||||
use Flarum\Core\User;
|
use Flarum\Core\User;
|
||||||
use Flarum\Core\AuthToken;
|
use Flarum\Core\AuthToken;
|
||||||
@ -20,7 +21,10 @@ use Flarum\Foundation\Application;
|
|||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Flarum\Core\Exception\PermissionDeniedException;
|
use Flarum\Core\Exception\PermissionDeniedException;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
use Illuminate\Contracts\Validation\Factory;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationException;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Validation\Validator;
|
||||||
use Intervention\Image\ImageManager;
|
use Intervention\Image\ImageManager;
|
||||||
use League\Flysystem\Adapter\Local;
|
use League\Flysystem\Adapter\Local;
|
||||||
use League\Flysystem\Filesystem;
|
use League\Flysystem\Filesystem;
|
||||||
@ -52,20 +56,27 @@ class RegisterUserHandler
|
|||||||
*/
|
*/
|
||||||
protected $uploadDir;
|
protected $uploadDir;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Factory
|
||||||
|
*/
|
||||||
|
private $validatorFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Dispatcher $events
|
* @param Dispatcher $events
|
||||||
* @param SettingsRepositoryInterface $settings
|
* @param SettingsRepositoryInterface $settings
|
||||||
* @param UserValidator $validator
|
* @param UserValidator $validator
|
||||||
* @param Application $app
|
* @param Application $app
|
||||||
* @param FilesystemInterface $uploadDir
|
* @param FilesystemInterface $uploadDir
|
||||||
|
* @param Factory $validatorFactory
|
||||||
*/
|
*/
|
||||||
public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings, UserValidator $validator, Application $app, FilesystemInterface $uploadDir)
|
public function __construct(Dispatcher $events, SettingsRepositoryInterface $settings, UserValidator $validator, Application $app, FilesystemInterface $uploadDir, Factory $validatorFactory)
|
||||||
{
|
{
|
||||||
$this->events = $events;
|
$this->events = $events;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->validator = $validator;
|
$this->validator = $validator;
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
$this->uploadDir = $uploadDir;
|
$this->uploadDir = $uploadDir;
|
||||||
|
$this->validatorFactory = $validatorFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -119,7 +130,19 @@ class RegisterUserHandler
|
|||||||
|
|
||||||
$this->validator->assertValid(array_merge($user->getAttributes(), compact('password')));
|
$this->validator->assertValid(array_merge($user->getAttributes(), compact('password')));
|
||||||
|
|
||||||
$this->saveAvatarFromUrl($user, array_get($data, 'attributes.avatarUrl'));
|
if ($avatarUrl = array_get($data, 'attributes.avatarUrl')) {
|
||||||
|
$validation = $this->validatorFactory->make(compact('avatarUrl'), ['avatarUrl' => 'url']);
|
||||||
|
|
||||||
|
if ($validation->fails()) {
|
||||||
|
throw new ValidationException($validation);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->saveAvatarFromUrl($user, $avatarUrl);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
@ -132,12 +155,12 @@ class RegisterUserHandler
|
|||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function saveAvatarFromUrl(User $user, $avatarUrl)
|
private function saveAvatarFromUrl(User $user, $url)
|
||||||
{
|
{
|
||||||
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar');
|
$tmpFile = tempnam($this->app->storagePath().'/tmp', 'avatar');
|
||||||
|
|
||||||
$manager = new ImageManager;
|
$manager = new ImageManager;
|
||||||
$manager->make($avatarUrl)->fit(100, 100)->save($tmpFile);
|
$manager->make($url)->fit(100, 100)->save($tmpFile);
|
||||||
|
|
||||||
$mount = new MountManager([
|
$mount = new MountManager([
|
||||||
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
|
'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user