PlaceholderAvatar.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann <mail@michael-weimann.eu>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Vincent Petry <vincent@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Avatar;
  26. use OC\NotSquareException;
  27. use OC\User\User;
  28. use OCP\Files\NotFoundException;
  29. use OCP\Files\NotPermittedException;
  30. use OCP\Files\SimpleFS\ISimpleFile;
  31. use OCP\Files\SimpleFS\ISimpleFolder;
  32. use OCP\IConfig;
  33. use OCP\IImage;
  34. use OCP\IL10N;
  35. use Psr\Log\LoggerInterface;
  36. /**
  37. * This class represents a registered user's placeholder avatar.
  38. *
  39. * It generates an image based on the user's initials and caches it on storage
  40. * for faster retrieval, unlike the GuestAvatar.
  41. */
  42. class PlaceholderAvatar extends Avatar {
  43. private ISimpleFolder $folder;
  44. private User $user;
  45. /**
  46. * UserAvatar constructor.
  47. *
  48. * @param IConfig $config The configuration
  49. * @param ISimpleFolder $folder The avatar files folder
  50. * @param IL10N $l The localization helper
  51. * @param User $user The user this class manages the avatar for
  52. * @param LoggerInterface $logger The logger
  53. */
  54. public function __construct(
  55. ISimpleFolder $folder,
  56. $user,
  57. LoggerInterface $logger) {
  58. parent::__construct($logger);
  59. $this->folder = $folder;
  60. $this->user = $user;
  61. }
  62. /**
  63. * Check if an avatar exists for the user
  64. */
  65. public function exists(): bool {
  66. return true;
  67. }
  68. /**
  69. * Sets the users avatar.
  70. *
  71. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  72. * @throws \Exception if the provided file is not a jpg or png image
  73. * @throws \Exception if the provided image is not valid
  74. * @throws NotSquareException if the image is not square
  75. * @return void
  76. */
  77. public function set($data): void {
  78. // unimplemented for placeholder avatars
  79. }
  80. /**
  81. * Removes the users avatar.
  82. */
  83. public function remove(bool $silent = false): void {
  84. $avatars = $this->folder->getDirectoryListing();
  85. foreach ($avatars as $avatar) {
  86. $avatar->delete();
  87. }
  88. }
  89. /**
  90. * Returns the avatar for an user.
  91. *
  92. * If there is no avatar file yet, one is generated.
  93. *
  94. * @param int $size
  95. * @return ISimpleFile
  96. * @throws NotFoundException
  97. * @throws \OCP\Files\NotPermittedException
  98. * @throws \OCP\PreConditionNotMetException
  99. */
  100. public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
  101. $ext = 'png';
  102. if ($size === -1) {
  103. $path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $ext;
  104. } else {
  105. $path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $size . '.' . $ext;
  106. }
  107. try {
  108. $file = $this->folder->getFile($path);
  109. } catch (NotFoundException $e) {
  110. if ($size <= 0) {
  111. throw new NotFoundException;
  112. }
  113. if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
  114. $data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
  115. }
  116. try {
  117. $file = $this->folder->newFile($path);
  118. $file->putContent($data);
  119. } catch (NotPermittedException $e) {
  120. $this->logger->error('Failed to save avatar placeholder for ' . $this->user->getUID());
  121. throw new NotFoundException();
  122. }
  123. }
  124. return $file;
  125. }
  126. /**
  127. * Returns the user display name.
  128. */
  129. public function getDisplayName(): string {
  130. return $this->user->getDisplayName();
  131. }
  132. /**
  133. * Handles user changes.
  134. *
  135. * @param string $feature The changed feature
  136. * @param mixed $oldValue The previous value
  137. * @param mixed $newValue The new value
  138. * @throws NotPermittedException
  139. * @throws \OCP\PreConditionNotMetException
  140. */
  141. public function userChanged(string $feature, $oldValue, $newValue): void {
  142. $this->remove();
  143. }
  144. /**
  145. * Check if the avatar of a user is a custom uploaded one
  146. */
  147. public function isCustomAvatar(): bool {
  148. return false;
  149. }
  150. }