GuestAvatarController.php 2.8 KB

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