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