- /**
- * Destroys an image at the given path.
- * Searches for image thumbnails in addition to main provided path..
- * @param string $path
- * @return bool
- */
- protected function destroyImagesFromPath(string $path)
- {
- $storage = $this->getStorage();
-
- $imageFolder = dirname($path);
- $imageFileName = basename($path);
- $allImages = collect($storage->allFiles($imageFolder));
-
- // Delete image files
- $imagesToDelete = $allImages->filter(function ($imagePath) use ($imageFileName) {
- $expectedIndex = strlen($imagePath) - strlen($imageFileName);
- return strpos($imagePath, $imageFileName) === $expectedIndex;
- });
- $storage->delete($imagesToDelete->all());
-
- // Cleanup of empty folders
- $foldersInvolved = array_merge([$imageFolder], $storage->directories($imageFolder));
- foreach ($foldersInvolved as $directory) {
- if ($this->isFolderEmpty($directory)) {
- $storage->deleteDirectory($directory);
- }
- }
-
- return true;
- }
-
- /**
- * Save a gravatar image and set a the profile image for a user.
- * @param \BookStack\Auth\User $user
- * @param null|string $gravatarUrl
- * @param int $size
- * @return mixed
- * @throws Exception
- */
- public function saveUserGravatar(User $user, $gravatarUrl, $size = 500)
- {
- if (!is_string($gravatarUrl) || empty($gravatarUrl)) {
- $gravatarUrl = 'https://www.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon';
- }
- $email = strtolower(trim($user->email));
- $gravatarUrl = str_replace('%{hash}', md5($email), $gravatarUrl);
- $gravatarUrl = str_replace('%{size}', $size, $gravatarUrl);
- $gravatarUrl = str_replace('%{email}', urlencode($email), $gravatarUrl);
- $imageName = str_replace(' ', '-', $user->name . '-gravatar.png');
- $image = $this->saveNewFromUrl($gravatarUrl, 'user', $imageName);
- $image->created_by = $user->id;
- $image->updated_by = $user->id;
- $image->save();
- return $image;
- }
-
-