1
0

GuestAvatarController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 64|512 $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, int $size, ?bool $darkTheme = false) {
  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. * @param string $guestName The guest name, e.g. "Albert"
  83. * @param 64|512 $size The desired avatar size, e.g. 64 for 64x64px
  84. * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}>
  85. *
  86. * 200: Custom avatar returned
  87. * 201: Avatar returned
  88. */
  89. #[PublicPage]
  90. #[NoCSRFRequired]
  91. #[FrontpageRoute(verb: 'GET', url: '/avatar/guest/{guestName}/{size}/dark')]
  92. public function getAvatarDark(string $guestName, int $size) {
  93. return $this->getAvatar($guestName, $size, true);
  94. }
  95. }