Inheritance: extends AbstractValidator
Exemplo n.º 1
0
 /**
  * @param UploadAvatar $command
  * @return \Flarum\Core\User
  * @throws \Flarum\Core\Exception\PermissionDeniedException
  */
 public function handle(UploadAvatar $command)
 {
     $actor = $command->actor;
     $user = $this->users->findOrFail($command->userId);
     if ($actor->id !== $user->id) {
         $this->assertCan($actor, 'edit', $user);
     }
     $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'avatar');
     $command->file->moveTo($tmpFile);
     try {
         $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();
         // Explicitly tell Intervention to encode the image as JSON (instead of having to guess from the extension)
         $encodedImage = $manager->make($tmpFile)->fit(100, 100)->encode('jpg', 100);
         file_put_contents($tmpFile, $encodedImage);
         $this->events->fire(new AvatarWillBeSaved($user, $actor, $tmpFile));
         $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]);
         if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) {
             $mount->delete($file);
         }
         $uploadName = Str::lower(Str::quickRandom()) . '.jpg';
         $user->changeAvatarPath($uploadName);
         $mount->move('source://' . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}");
         $user->save();
         $this->dispatchEventsFor($user, $actor);
         return $user;
     } catch (Exception $e) {
         @unlink($tmpFile);
         throw $e;
     }
 }
Exemplo n.º 2
0
 /**
  * @param UploadAvatar $command
  * @return \Flarum\Core\User
  * @throws \Flarum\Core\Exception\PermissionDeniedException
  */
 public function handle(UploadAvatar $command)
 {
     $actor = $command->actor;
     $user = $this->users->findOrFail($command->userId);
     if ($actor->id !== $user->id) {
         $this->assertCan($actor, 'edit', $user);
     }
     $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();
     $this->events->fire(new AvatarWillBeSaved($user, $actor, $tmpFile));
     $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]);
     if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) {
         $mount->delete($file);
     }
     $uploadName = Str::lower(Str::quickRandom()) . '.jpg';
     $user->changeAvatarPath($uploadName);
     $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}");
     $user->save();
     $this->dispatchEventsFor($user, $actor);
     return $user;
 }