Passed
Push — master ( 71d1fd...4c8c00 )
by Joas
14:27 queued 12s
created

GuestAvatarController::getAvatar()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 16
nop 2
dl 0
loc 36
rs 8.9137
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019, Michael Weimann <[email protected]>
4
 *
5
 * @author Michael Weimann <[email protected]>
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
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http;
27
use OCP\AppFramework\Http\FileDisplayResponse;
28
use OCP\IAvatarManager;
29
use OCP\ILogger;
30
use OCP\IRequest;
31
32
/**
33
 * This controller handles guest avatar requests.
34
 */
35
class GuestAvatarController extends Controller {
36
37
	/**
38
	 * @var ILogger
39
	 */
40
	private $logger;
41
42
	/**
43
	 * @var IAvatarManager
44
	 */
45
	private $avatarManager;
46
47
	/**
48
	 * GuestAvatarController constructor.
49
	 *
50
	 * @param $appName
51
	 * @param IRequest $request
52
	 * @param IAvatarManager $avatarManager
53
	 * @param ILogger $logger
54
	 */
55
	public function __construct(
56
		$appName,
57
		IRequest $request,
58
		IAvatarManager $avatarManager,
59
		ILogger $logger
60
	) {
61
		parent::__construct($appName, $request);
62
		$this->avatarManager = $avatarManager;
63
		$this->logger = $logger;
64
	}
65
66
	/**
67
	 * Returns a guest avatar image response.
68
	 *
69
	 * @PublicPage
70
	 * @NoCSRFRequired
71
	 *
72
	 * @param string $guestName The guest name, e.g. "Albert"
73
	 * @param string $size The desired avatar size, e.g. 64 for 64x64px
74
	 * @return FileDisplayResponse|Http\Response
75
	 */
76
	public function getAvatar($guestName, $size) {
77
		$size = (int) $size;
78
79
		if ($size <= 64) {
80
			if ($size !== 64) {
81
				$this->logger->debug('Avatar requested in deprecated size ' . $size);
82
			}
83
			$size = 64;
84
		} else {
85
			if ($size !== 512) {
86
				$this->logger->debug('Avatar requested in deprecated size ' . $size);
87
			}
88
			$size = 512;
89
		}
90
91
		try {
92
			$avatar = $this->avatarManager->getGuestAvatar($guestName);
93
			$avatarFile = $avatar->getFile($size);
94
95
			$resp = new FileDisplayResponse(
96
				$avatarFile,
97
				$avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,
98
				['Content-Type' => $avatarFile->getMimeType()]
99
			);
100
		} catch (\Exception $e) {
101
			$this->logger->error('error while creating guest avatar', [
102
				'err' => $e,
103
			]);
104
			$resp = new Http\Response();
105
			$resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
106
			return $resp;
107
		}
108
109
		// Cache for 30 minutes
110
		$resp->cacheFor(1800);
111
		return $resp;
112
	}
113
}
114