1
0

PlaceholderAvatar.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Avatar;
  8. use OC\NotSquareException;
  9. use OC\User\User;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\NotPermittedException;
  12. use OCP\Files\SimpleFS\ISimpleFile;
  13. use OCP\Files\SimpleFS\ISimpleFolder;
  14. use OCP\IImage;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17. * This class represents a registered user's placeholder avatar.
  18. *
  19. * It generates an image based on the user's initials and caches it on storage
  20. * for faster retrieval, unlike the GuestAvatar.
  21. */
  22. class PlaceholderAvatar extends Avatar {
  23. public function __construct(
  24. private ISimpleFolder $folder,
  25. private User $user,
  26. LoggerInterface $logger,
  27. ) {
  28. parent::__construct($logger);
  29. }
  30. /**
  31. * Check if an avatar exists for the user
  32. */
  33. public function exists(): bool {
  34. return true;
  35. }
  36. /**
  37. * Sets the users avatar.
  38. *
  39. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  40. * @throws \Exception if the provided file is not a jpg or png image
  41. * @throws \Exception if the provided image is not valid
  42. * @throws NotSquareException if the image is not square
  43. */
  44. public function set($data): void {
  45. // unimplemented for placeholder avatars
  46. }
  47. /**
  48. * Removes the users avatar.
  49. */
  50. public function remove(bool $silent = false): void {
  51. $avatars = $this->folder->getDirectoryListing();
  52. foreach ($avatars as $avatar) {
  53. $avatar->delete();
  54. }
  55. }
  56. /**
  57. * Returns the avatar for an user.
  58. *
  59. * If there is no avatar file yet, one is generated.
  60. *
  61. * @throws NotFoundException
  62. * @throws \OCP\Files\NotPermittedException
  63. * @throws \OCP\PreConditionNotMetException
  64. */
  65. public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
  66. $ext = 'png';
  67. if ($size === -1) {
  68. $path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $ext;
  69. } else {
  70. $path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $size . '.' . $ext;
  71. }
  72. try {
  73. $file = $this->folder->getFile($path);
  74. } catch (NotFoundException $e) {
  75. if ($size <= 0) {
  76. throw new NotFoundException;
  77. }
  78. if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
  79. $data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
  80. }
  81. try {
  82. $file = $this->folder->newFile($path);
  83. $file->putContent($data);
  84. } catch (NotPermittedException $e) {
  85. $this->logger->error('Failed to save avatar placeholder for ' . $this->user->getUID());
  86. throw new NotFoundException();
  87. }
  88. }
  89. return $file;
  90. }
  91. /**
  92. * Returns the user display name.
  93. */
  94. public function getDisplayName(): string {
  95. return $this->user->getDisplayName();
  96. }
  97. /**
  98. * Handles user changes.
  99. *
  100. * @param string $feature The changed feature
  101. * @param mixed $oldValue The previous value
  102. * @param mixed $newValue The new value
  103. * @throws NotPermittedException
  104. * @throws \OCP\PreConditionNotMetException
  105. */
  106. public function userChanged(string $feature, $oldValue, $newValue): void {
  107. $this->remove();
  108. }
  109. /**
  110. * Check if the avatar of a user is a custom uploaded one
  111. */
  112. public function isCustomAvatar(): bool {
  113. return false;
  114. }
  115. }