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.

GuestAvatarController.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2019, Michael Weimann <mail@michael-weimann.eu>
  4. *
  5. * @author Michael Weimann <mail@michael-weimann.eu>
  6. * @author Kate Döen <kate.doeen@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  28. use OCP\AppFramework\Http\FileDisplayResponse;
  29. use OCP\AppFramework\Http\Response;
  30. use OCP\IAvatarManager;
  31. use OCP\IRequest;
  32. use Psr\Log\LoggerInterface;
  33. /**
  34. * This controller handles guest avatar requests.
  35. */
  36. class GuestAvatarController extends Controller {
  37. /**
  38. * GuestAvatarController constructor.
  39. */
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. private IAvatarManager $avatarManager,
  44. private LoggerInterface $logger,
  45. ) {
  46. parent::__construct($appName, $request);
  47. }
  48. /**
  49. * Returns a guest avatar image response
  50. *
  51. * @PublicPage
  52. * @NoCSRFRequired
  53. *
  54. * @param string $guestName The guest name, e.g. "Albert"
  55. * @param string $size The desired avatar size, e.g. 64 for 64x64px
  56. * @param bool|null $darkTheme Return dark avatar
  57. * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
  58. *
  59. * 200: Custom avatar returned
  60. * 201: Avatar returned
  61. */
  62. #[FrontpageRoute(verb: 'GET', url: '/avatar/guest/{guestName}/{size}')]
  63. public function getAvatar(string $guestName, string $size, ?bool $darkTheme = false) {
  64. $size = (int) $size;
  65. $darkTheme = $darkTheme ?? false;
  66. if ($size <= 64) {
  67. if ($size !== 64) {
  68. $this->logger->debug('Avatar requested in deprecated size ' . $size);
  69. }
  70. $size = 64;
  71. } else {
  72. if ($size !== 512) {
  73. $this->logger->debug('Avatar requested in deprecated size ' . $size);
  74. }
  75. $size = 512;
  76. }
  77. try {
  78. $avatar = $this->avatarManager->getGuestAvatar($guestName);
  79. $avatarFile = $avatar->getFile($size, $darkTheme);
  80. $resp = new FileDisplayResponse(
  81. $avatarFile,
  82. $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
  83. ['Content-Type' => $avatarFile->getMimeType()]
  84. );
  85. } catch (\Exception $e) {
  86. $this->logger->error('error while creating guest avatar', [
  87. 'err' => $e,
  88. ]);
  89. $resp = new Http\Response();
  90. $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
  91. return $resp;
  92. }
  93. // Cache for 30 minutes
  94. $resp->cacheFor(1800, false, true);
  95. return $resp;
  96. }
  97. /**
  98. * Returns a dark guest avatar image response
  99. *
  100. * @PublicPage
  101. * @NoCSRFRequired
  102. *
  103. * @param string $guestName The guest name, e.g. "Albert"
  104. * @param string $size The desired avatar size, e.g. 64 for 64x64px
  105. * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
  106. *
  107. * 200: Custom avatar returned
  108. * 201: Avatar returned
  109. */
  110. #[FrontpageRoute(verb: 'GET', url: '/avatar/guest/{guestName}/{size}/dark')]
  111. public function getAvatarDark(string $guestName, string $size) {
  112. return $this->getAvatar($guestName, $size, true);
  113. }
  114. }