You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PlaceholderAvatar.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /** @var ISimpleFolder */
  44. private $folder;
  45. /** @var User */
  46. private $user;
  47. /**
  48. * UserAvatar constructor.
  49. *
  50. * @param IConfig $config The configuration
  51. * @param ISimpleFolder $folder The avatar files folder
  52. * @param IL10N $l The localization helper
  53. * @param User $user The user this class manages the avatar for
  54. * @param LoggerInterface $logger The logger
  55. */
  56. public function __construct(
  57. ISimpleFolder $folder,
  58. $user,
  59. LoggerInterface $logger) {
  60. parent::__construct($logger);
  61. $this->folder = $folder;
  62. $this->user = $user;
  63. }
  64. /**
  65. * Check if an avatar exists for the user
  66. *
  67. * @return bool
  68. */
  69. public function exists() {
  70. return true;
  71. }
  72. /**
  73. * Sets the users avatar.
  74. *
  75. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  76. * @throws \Exception if the provided file is not a jpg or png image
  77. * @throws \Exception if the provided image is not valid
  78. * @throws NotSquareException if the image is not square
  79. * @return void
  80. */
  81. public function set($data) {
  82. // unimplemented for placeholder avatars
  83. }
  84. /**
  85. * Removes the users avatar.
  86. */
  87. public function remove(bool $silent = false) {
  88. $avatars = $this->folder->getDirectoryListing();
  89. foreach ($avatars as $avatar) {
  90. $avatar->delete();
  91. }
  92. }
  93. /**
  94. * Returns the avatar for an user.
  95. *
  96. * If there is no avatar file yet, one is generated.
  97. *
  98. * @param int $size
  99. * @return ISimpleFile
  100. * @throws NotFoundException
  101. * @throws \OCP\Files\NotPermittedException
  102. * @throws \OCP\PreConditionNotMetException
  103. */
  104. public function getFile($size) {
  105. $size = (int) $size;
  106. $ext = 'png';
  107. if ($size === -1) {
  108. $path = 'avatar-placeholder.' . $ext;
  109. } else {
  110. $path = 'avatar-placeholder.' . $size . '.' . $ext;
  111. }
  112. try {
  113. $file = $this->folder->getFile($path);
  114. } catch (NotFoundException $e) {
  115. if ($size <= 0) {
  116. throw new NotFoundException;
  117. }
  118. if (!$data = $this->generateAvatarFromSvg($size)) {
  119. $data = $this->generateAvatar($this->getDisplayName(), $size);
  120. }
  121. try {
  122. $file = $this->folder->newFile($path);
  123. $file->putContent($data);
  124. } catch (NotPermittedException $e) {
  125. $this->logger->error('Failed to save avatar placeholder for ' . $this->user->getUID());
  126. throw new NotFoundException();
  127. }
  128. }
  129. return $file;
  130. }
  131. /**
  132. * Returns the user display name.
  133. *
  134. * @return string
  135. */
  136. public function getDisplayName(): string {
  137. return $this->user->getDisplayName();
  138. }
  139. /**
  140. * Handles user changes.
  141. *
  142. * @param string $feature The changed feature
  143. * @param mixed $oldValue The previous value
  144. * @param mixed $newValue The new value
  145. * @throws NotPermittedException
  146. * @throws \OCP\PreConditionNotMetException
  147. */
  148. public function userChanged($feature, $oldValue, $newValue) {
  149. $this->remove();
  150. }
  151. /**
  152. * Check if the avatar of a user is a custom uploaded one
  153. *
  154. * @return bool
  155. */
  156. public function isCustomAvatar(): bool {
  157. return false;
  158. }
  159. }