GuestAvatarController.php 3.0 KB

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