summaryrefslogtreecommitdiffstats
path: root/core/Controller
diff options
context:
space:
mode:
authorMichael Weimann <mail@michael-weimann.eu>2019-01-20 11:13:41 +0100
committerMorris Jobke <hey@morrisjobke.de>2019-02-07 14:23:16 +0100
commitbf1253cb49a4931244a6bbde4dfa44bf084f4377 (patch)
tree6faacd5110c029778ed56554e27a5d73c4e888db /core/Controller
parentb69b17f29fd518495a6495e0f90f60c70ef0fc73 (diff)
downloadnextcloud-server-bf1253cb49a4931244a6bbde4dfa44bf084f4377.tar.gz
nextcloud-server-bf1253cb49a4931244a6bbde4dfa44bf084f4377.zip
Implement guest avatar endpoint
Signed-off-by: Michael Weimann <mail@michael-weimann.eu>
Diffstat (limited to 'core/Controller')
-rw-r--r--core/Controller/AvatarController.php1
-rw-r--r--core/Controller/GuestAvatarController.php107
2 files changed, 107 insertions, 1 deletions
diff --git a/core/Controller/AvatarController.php b/core/Controller/AvatarController.php
index 03efe4d1e52..9ee344f7ed8 100644
--- a/core/Controller/AvatarController.php
+++ b/core/Controller/AvatarController.php
@@ -42,7 +42,6 @@ use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
-use OCP\AppFramework\Http\DataResponse;
/**
* Class AvatarController
diff --git a/core/Controller/GuestAvatarController.php b/core/Controller/GuestAvatarController.php
new file mode 100644
index 00000000000..76e140fee30
--- /dev/null
+++ b/core/Controller/GuestAvatarController.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * @copyright Copyright (c) 2019, Michael Weimann <mail@michael-weimann.eu>
+ *
+ * @author Michael Weimann <mail@michael-weimann.eu>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ */
+
+namespace OC\Core\Controller;
+
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\FileDisplayResponse;
+use OCP\IAvatarManager;
+use OCP\ILogger;
+use OCP\IRequest;
+
+/**
+ * This controller handles guest avatar requests.
+ */
+class GuestAvatarController extends Controller {
+
+ /**
+ * @var ILogger
+ */
+ private $logger;
+
+ /**
+ * @var IAvatarManager
+ */
+ private $avatarManager;
+
+ /**
+ * GuestAvatarController constructor.
+ *
+ * @param $appName
+ * @param IRequest $request
+ * @param IAvatarManager $avatarManager
+ * @param ILogger $logger
+ */
+ public function __construct(
+ $appName,
+ IRequest $request,
+ IAvatarManager $avatarManager,
+ ILogger $logger
+ ) {
+ parent::__construct($appName, $request);
+ $this->avatarManager = $avatarManager;
+ $this->logger = $logger;
+ }
+
+ /**
+ * Returns a guest avatar image response.
+ *
+ * @PublicPage
+ * @NoCSRFRequired
+ *
+ * @param string $guestName The guest name, e.g. "Albert"
+ * @param string $size The desired avatar size, e.g. 64 for 64x64px
+ * @return FileDisplayResponse|Http\Response
+ */
+ public function getAvatar($guestName, $size) {
+ $size = (int) $size;
+
+ // min/max size
+ if ($size > 2048) {
+ $size = 2048;
+ } elseif ($size <= 0) {
+ $size = 64;
+ }
+
+ try {
+ $avatar = $this->avatarManager->getGuestAvatar($guestName);
+ $avatarFile = $avatar->getFile($size);
+
+ $resp = new FileDisplayResponse(
+ $avatarFile,
+ $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
+ ['Content-Type' => $avatarFile->getMimeType()]
+ );
+ } catch (\Exception $e) {
+ $this->logger->error('error while creating guest avatar', [
+ 'err' => $e,
+ ]);
+ $resp = new Http\Response();
+ $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
+ return $resp;
+ }
+
+ // Cache for 30 minutes
+ $resp->cacheFor(1800);
+ return $resp;
+ }
+}