3 namespace BookStack\Uploads;
5 use BookStack\Exceptions\HttpFetchException;
6 use BookStack\Http\HttpRequestService;
7 use BookStack\Users\Models\User;
9 use GuzzleHttp\Psr7\Request;
10 use Illuminate\Support\Facades\Log;
11 use Illuminate\Support\Str;
12 use Psr\Http\Client\ClientExceptionInterface;
16 public function __construct(
17 protected ImageService $imageService,
18 protected HttpRequestService $http
23 * Fetch and assign an avatar image to the given user.
25 public function fetchAndAssignToUser(User $user): void
27 if (!$this->avatarFetchEnabled()) {
32 $this->destroyAllForUser($user);
33 $avatar = $this->saveAvatarImage($user);
34 $user->avatar()->associate($avatar);
36 } catch (Exception $e) {
37 Log::error('Failed to save user avatar image', ['exception' => $e]);
42 * Assign a new avatar image to the given user using the given image data.
44 public function assignToUserFromExistingData(User $user, string $imageData, string $extension): void
47 $this->destroyAllForUser($user);
48 $avatar = $this->createAvatarImageFromData($user, $imageData, $extension);
49 $user->avatar()->associate($avatar);
51 } catch (Exception $e) {
52 Log::error('Failed to save user avatar image', ['exception' => $e]);
57 * Destroy all user avatars uploaded to the given user.
59 public function destroyAllForUser(User $user): void
61 $profileImages = Image::query()->where('type', '=', 'user')
62 ->where('uploaded_to', '=', $user->id)
65 foreach ($profileImages as $image) {
66 $this->imageService->destroy($image);
71 * Save an avatar image from an external service.
73 * @throws HttpFetchException
75 protected function saveAvatarImage(User $user, int $size = 500): Image
77 $avatarUrl = $this->getAvatarUrl();
78 $email = strtolower(trim($user->email));
81 '${hash}' => md5($email),
83 '${email}' => urlencode($email),
86 $userAvatarUrl = strtr($avatarUrl, $replacements);
87 $imageData = $this->getAvatarImageData($userAvatarUrl);
89 return $this->createAvatarImageFromData($user, $imageData, 'png');
93 * Creates a new image instance and saves it in the system as a new user avatar image.
95 protected function createAvatarImageFromData(User $user, string $imageData, string $extension): Image
97 $imageName = Str::random(10) . '-avatar.' . $extension;
99 $image = $this->imageService->saveNew($imageName, $imageData, 'user', $user->id);
100 $image->created_by = $user->id;
101 $image->updated_by = $user->id;
108 * Gets an image from url and returns it as a string of image data.
110 * @throws HttpFetchException
112 protected function getAvatarImageData(string $url): string
115 $client = $this->http->buildClient(5);
116 $response = $client->sendRequest(new Request('GET', $url));
117 if ($response->getStatusCode() !== 200) {
118 throw new HttpFetchException(trans('errors.cannot_get_image_from_url', ['url' => $url]));
121 return (string) $response->getBody();
122 } catch (ClientExceptionInterface $exception) {
123 throw new HttpFetchException(trans('errors.cannot_get_image_from_url', ['url' => $url]), $exception->getCode(), $exception);
128 * Check if fetching external avatars is enabled.
130 protected function avatarFetchEnabled(): bool
132 $fetchUrl = $this->getAvatarUrl();
134 return str_starts_with($fetchUrl, 'http');
138 * Get the URL to fetch avatars from.
140 protected function getAvatarUrl(): string
142 $configOption = config('services.avatar_url');
143 if ($configOption === false) {
147 $url = trim($configOption);
149 if (empty($url) && !config('services.disable_services')) {
150 $url = 'https://www.gravatar.com/avatar/${hash}?s=${size}&d=identicon';