GuestAvatarController.php 2.7 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\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. // min/max size
  73. if ($size > 2048) {
  74. $size = 2048;
  75. } elseif ($size <= 0) {
  76. $size = 64;
  77. }
  78. try {
  79. $avatar = $this->avatarManager->getGuestAvatar($guestName);
  80. $avatarFile = $avatar->getFile($size);
  81. $resp = new FileDisplayResponse(
  82. $avatarFile,
  83. $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
  84. ['Content-Type' => $avatarFile->getMimeType()]
  85. );
  86. } catch (\Exception $e) {
  87. $this->logger->error('error while creating guest avatar', [
  88. 'err' => $e,
  89. ]);
  90. $resp = new Http\Response();
  91. $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
  92. return $resp;
  93. }
  94. // Cache for 30 minutes
  95. $resp->cacheFor(1800);
  96. return $resp;
  97. }
  98. }