GuestAvatarController.php 2.6 KB

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