mirror of
https://github.com/flarum/framework.git
synced 2025-05-29 19:49:36 +08:00
Remove temporary file after avatar upload failure. closes flarum/core#999
This commit is contained in:
@ -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\Repository\UserRepository;
|
use Flarum\Core\Repository\UserRepository;
|
||||||
use Flarum\Core\Support\DispatchEventsTrait;
|
use Flarum\Core\Support\DispatchEventsTrait;
|
||||||
@ -84,46 +85,52 @@ 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(
|
try {
|
||||||
$tmpFile,
|
$file = new UploadedFile(
|
||||||
$command->file->getClientFilename(),
|
$tmpFile,
|
||||||
$command->file->getClientMediaType(),
|
$command->file->getClientFilename(),
|
||||||
$command->file->getSize(),
|
$command->file->getClientMediaType(),
|
||||||
$command->file->getError(),
|
$command->file->getSize(),
|
||||||
true
|
$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)
|
// 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);
|
$encodedImage = $manager->make($tmpFile)->fit(100, 100)->encode('jpg', 100);
|
||||||
file_put_contents($tmpFile, $encodedImage);
|
file_put_contents($tmpFile, $encodedImage);
|
||||||
|
|
||||||
$this->events->fire(
|
$this->events->fire(
|
||||||
new AvatarWillBeSaved($user, $actor, $tmpFile)
|
new AvatarWillBeSaved($user, $actor, $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))),
|
||||||
'target' => $this->uploadDir,
|
'target' => $this->uploadDir,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if ($user->avatar_path && $mount->has($file = "target://$user->avatar_path")) {
|
if ($user->avatar_path && $mount->has($file = "target://$user->avatar_path")) {
|
||||||
$mount->delete($file);
|
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user