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