GuestAvatarController.php 3.6 KB

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