GuestAvatarController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  35. * GuestAvatarController constructor.
  36. */
  37. public function __construct(
  38. string $appName,
  39. IRequest $request,
  40. private IAvatarManager $avatarManager,
  41. private LoggerInterface $logger,
  42. ) {
  43. parent::__construct($appName, $request);
  44. }
  45. /**
  46. * Returns a guest avatar image response.
  47. *
  48. * @PublicPage
  49. * @NoCSRFRequired
  50. *
  51. * @param string $guestName The guest name, e.g. "Albert"
  52. * @param string $size The desired avatar size, e.g. 64 for 64x64px
  53. * @return FileDisplayResponse|Http\Response
  54. */
  55. public function getAvatar(string $guestName, string $size, ?bool $darkTheme = false) {
  56. $size = (int) $size;
  57. $darkTheme = $darkTheme ?? false;
  58. if ($size <= 64) {
  59. if ($size !== 64) {
  60. $this->logger->debug('Avatar requested in deprecated size ' . $size);
  61. }
  62. $size = 64;
  63. } else {
  64. if ($size !== 512) {
  65. $this->logger->debug('Avatar requested in deprecated size ' . $size);
  66. }
  67. $size = 512;
  68. }
  69. try {
  70. $avatar = $this->avatarManager->getGuestAvatar($guestName);
  71. $avatarFile = $avatar->getFile($size, $darkTheme);
  72. $resp = new FileDisplayResponse(
  73. $avatarFile,
  74. $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
  75. ['Content-Type' => $avatarFile->getMimeType()]
  76. );
  77. } catch (\Exception $e) {
  78. $this->logger->error('error while creating guest avatar', [
  79. 'err' => $e,
  80. ]);
  81. $resp = new Http\Response();
  82. $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
  83. return $resp;
  84. }
  85. // Cache for 30 minutes
  86. $resp->cacheFor(1800, false, true);
  87. return $resp;
  88. }
  89. /**
  90. * @PublicPage
  91. * @NoCSRFRequired
  92. */
  93. public function getAvatarDark(string $guestName, string $size) {
  94. return $this->getAvatar($guestName, $size, true);
  95. }
  96. }