1
0

PlaceholderAvatar.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\IImage;
  33. use Psr\Log\LoggerInterface;
  34. /**
  35. * This class represents a registered user's placeholder avatar.
  36. *
  37. * It generates an image based on the user's initials and caches it on storage
  38. * for faster retrieval, unlike the GuestAvatar.
  39. */
  40. class PlaceholderAvatar extends Avatar {
  41. public function __construct(
  42. private ISimpleFolder $folder,
  43. private User $user,
  44. LoggerInterface $logger,
  45. ) {
  46. parent::__construct($logger);
  47. }
  48. /**
  49. * Check if an avatar exists for the user
  50. */
  51. public function exists(): bool {
  52. return true;
  53. }
  54. /**
  55. * Sets the users avatar.
  56. *
  57. * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar
  58. * @throws \Exception if the provided file is not a jpg or png image
  59. * @throws \Exception if the provided image is not valid
  60. * @throws NotSquareException if the image is not square
  61. */
  62. public function set($data): void {
  63. // unimplemented for placeholder avatars
  64. }
  65. /**
  66. * Removes the users avatar.
  67. */
  68. public function remove(bool $silent = false): void {
  69. $avatars = $this->folder->getDirectoryListing();
  70. foreach ($avatars as $avatar) {
  71. $avatar->delete();
  72. }
  73. }
  74. /**
  75. * Returns the avatar for an user.
  76. *
  77. * If there is no avatar file yet, one is generated.
  78. *
  79. * @throws NotFoundException
  80. * @throws \OCP\Files\NotPermittedException
  81. * @throws \OCP\PreConditionNotMetException
  82. */
  83. public function getFile(int $size, bool $darkTheme = false): ISimpleFile {
  84. $ext = 'png';
  85. if ($size === -1) {
  86. $path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $ext;
  87. } else {
  88. $path = 'avatar-placeholder' . ($darkTheme ? '-dark' : '') . '.' . $size . '.' . $ext;
  89. }
  90. try {
  91. $file = $this->folder->getFile($path);
  92. } catch (NotFoundException $e) {
  93. if ($size <= 0) {
  94. throw new NotFoundException;
  95. }
  96. if (!$data = $this->generateAvatarFromSvg($size, $darkTheme)) {
  97. $data = $this->generateAvatar($this->getDisplayName(), $size, $darkTheme);
  98. }
  99. try {
  100. $file = $this->folder->newFile($path);
  101. $file->putContent($data);
  102. } catch (NotPermittedException $e) {
  103. $this->logger->error('Failed to save avatar placeholder for ' . $this->user->getUID());
  104. throw new NotFoundException();
  105. }
  106. }
  107. return $file;
  108. }
  109. /**
  110. * Returns the user display name.
  111. */
  112. public function getDisplayName(): string {
  113. return $this->user->getDisplayName();
  114. }
  115. /**
  116. * Handles user changes.
  117. *
  118. * @param string $feature The changed feature
  119. * @param mixed $oldValue The previous value
  120. * @param mixed $newValue The new value
  121. * @throws NotPermittedException
  122. * @throws \OCP\PreConditionNotMetException
  123. */
  124. public function userChanged(string $feature, $oldValue, $newValue): void {
  125. $this->remove();
  126. }
  127. /**
  128. * Check if the avatar of a user is a custom uploaded one
  129. */
  130. public function isCustomAvatar(): bool {
  131. return false;
  132. }
  133. }