1
0

GuestAvatarController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Controller;
  7. use OCP\AppFramework\Controller;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  10. use OCP\AppFramework\Http\FileDisplayResponse;
  11. use OCP\AppFramework\Http\Response;
  12. use OCP\IAvatarManager;
  13. use OCP\IRequest;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * This controller handles guest avatar requests.
  17. */
  18. class GuestAvatarController extends Controller {
  19. /**
  20. * GuestAvatarController constructor.
  21. */
  22. public function __construct(
  23. string $appName,
  24. IRequest $request,
  25. private IAvatarManager $avatarManager,
  26. private LoggerInterface $logger,
  27. ) {
  28. parent::__construct($appName, $request);
  29. }
  30. /**
  31. * Returns a guest avatar image response
  32. *
  33. * @PublicPage
  34. * @NoCSRFRequired
  35. *
  36. * @param string $guestName The guest name, e.g. "Albert"
  37. * @param string $size The desired avatar size, e.g. 64 for 64x64px
  38. * @param bool|null $darkTheme Return dark avatar
  39. * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
  40. *
  41. * 200: Custom avatar returned
  42. * 201: Avatar returned
  43. */
  44. #[FrontpageRoute(verb: 'GET', url: '/avatar/guest/{guestName}/{size}')]
  45. public function getAvatar(string $guestName, string $size, ?bool $darkTheme = false) {
  46. $size = (int) $size;
  47. $darkTheme = $darkTheme ?? false;
  48. if ($size <= 64) {
  49. if ($size !== 64) {
  50. $this->logger->debug('Avatar requested in deprecated size ' . $size);
  51. }
  52. $size = 64;
  53. } else {
  54. if ($size !== 512) {
  55. $this->logger->debug('Avatar requested in deprecated size ' . $size);
  56. }
  57. $size = 512;
  58. }
  59. try {
  60. $avatar = $this->avatarManager->getGuestAvatar($guestName);
  61. $avatarFile = $avatar->getFile($size, $darkTheme);
  62. $resp = new FileDisplayResponse(
  63. $avatarFile,
  64. $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
  65. ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()]
  66. );
  67. } catch (\Exception $e) {
  68. $this->logger->error('error while creating guest avatar', [
  69. 'err' => $e,
  70. ]);
  71. $resp = new Http\Response();
  72. $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
  73. return $resp;
  74. }
  75. // Cache for 30 minutes
  76. $resp->cacheFor(1800, false, true);
  77. return $resp;
  78. }
  79. /**
  80. * Returns a dark guest avatar image response
  81. *
  82. * @PublicPage
  83. * @NoCSRFRequired
  84. *
  85. * @param string $guestName The guest name, e.g. "Albert"
  86. * @param string $size The desired avatar size, e.g. 64 for 64x64px
  87. * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
  88. *
  89. * 200: Custom avatar returned
  90. * 201: Avatar returned
  91. */
  92. #[FrontpageRoute(verb: 'GET', url: '/avatar/guest/{guestName}/{size}/dark')]
  93. public function getAvatarDark(string $guestName, string $size) {
  94. return $this->getAvatar($guestName, $size, true);
  95. }
  96. }