From d5797dae794df86ad2e98560e3447c0cca6bf5ea Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 27 Aug 2016 23:53:02 +0930 Subject: [PATCH] Remove temporary file after avatar upload failure. closes flarum/core#999 --- src/Core/Command/UploadAvatarHandler.php | 75 +++++++++++++----------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/src/Core/Command/UploadAvatarHandler.php b/src/Core/Command/UploadAvatarHandler.php index b5efe435d..a15443d7c 100644 --- a/src/Core/Command/UploadAvatarHandler.php +++ b/src/Core/Command/UploadAvatarHandler.php @@ -10,6 +10,7 @@ namespace Flarum\Core\Command; +use Exception; use Flarum\Core\Access\AssertPermissionTrait; use Flarum\Core\Repository\UserRepository; use Flarum\Core\Support\DispatchEventsTrait; @@ -84,46 +85,52 @@ 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 - ); + try { + $file = new UploadedFile( + $tmpFile, + $command->file->getClientFilename(), + $command->file->getClientMediaType(), + $command->file->getSize(), + $command->file->getError(), + true + ); - $this->validator->assertValid(['avatar' => $file]); + $this->validator->assertValid(['avatar' => $file]); - $manager = new ImageManager; + $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); + // 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) - ); + $this->events->fire( + new AvatarWillBeSaved($user, $actor, $tmpFile) + ); - $mount = new MountManager([ - 'source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), - 'target' => $this->uploadDir, - ]); + $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); + 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; } - - $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; } }