|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2018, Michael Weimann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
9
|
|
|
* @author Christoph Wurst <[email protected]> |
|
10
|
|
|
* @author Joas Schilling <[email protected]> |
|
11
|
|
|
* @author Michael Weimann <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @license GNU AGPL version 3 or any later version |
|
14
|
|
|
* |
|
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
18
|
|
|
* License, or (at your option) any later version. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
namespace OC\Avatar; |
|
31
|
|
|
|
|
32
|
|
|
use OC\NotSquareException; |
|
33
|
|
|
use OC\User\User; |
|
34
|
|
|
use OCP\Files\NotFoundException; |
|
35
|
|
|
use OCP\Files\NotPermittedException; |
|
36
|
|
|
use OCP\Files\SimpleFS\ISimpleFile; |
|
37
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
|
38
|
|
|
use OCP\IConfig; |
|
39
|
|
|
use OCP\IImage; |
|
40
|
|
|
use OCP\IL10N; |
|
41
|
|
|
use OCP\ILogger; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* This class represents a registered user's placeholder avatar. |
|
45
|
|
|
* |
|
46
|
|
|
* It generates an image based on the user's initials and caches it on storage |
|
47
|
|
|
* for faster retrieval, unlike the GuestAvatar. |
|
48
|
|
|
*/ |
|
49
|
|
|
class PlaceholderAvatar extends Avatar { |
|
50
|
|
|
/** @var ISimpleFolder */ |
|
51
|
|
|
private $folder; |
|
52
|
|
|
|
|
53
|
|
|
/** @var User */ |
|
54
|
|
|
private $user; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* UserAvatar constructor. |
|
58
|
|
|
* |
|
59
|
|
|
* @param IConfig $config The configuration |
|
60
|
|
|
* @param ISimpleFolder $folder The avatar files folder |
|
61
|
|
|
* @param IL10N $l The localization helper |
|
62
|
|
|
* @param User $user The user this class manages the avatar for |
|
63
|
|
|
* @param ILogger $logger The logger |
|
64
|
|
|
*/ |
|
65
|
|
|
public function __construct( |
|
66
|
|
|
ISimpleFolder $folder, |
|
67
|
|
|
$user, |
|
68
|
|
|
ILogger $logger) { |
|
69
|
|
|
parent::__construct($logger); |
|
70
|
|
|
|
|
71
|
|
|
$this->folder = $folder; |
|
72
|
|
|
$this->user = $user; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Check if an avatar exists for the user |
|
77
|
|
|
* |
|
78
|
|
|
* @return bool |
|
79
|
|
|
*/ |
|
80
|
|
|
public function exists() { |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets the users avatar. |
|
86
|
|
|
* |
|
87
|
|
|
* @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
88
|
|
|
* @throws \Exception if the provided file is not a jpg or png image |
|
89
|
|
|
* @throws \Exception if the provided image is not valid |
|
90
|
|
|
* @throws NotSquareException if the image is not square |
|
91
|
|
|
* @return void |
|
92
|
|
|
*/ |
|
93
|
|
|
public function set($data) { |
|
94
|
|
|
// unimplemented for placeholder avatars |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Removes the users avatar. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function remove(bool $silent = false) { |
|
101
|
|
|
$avatars = $this->folder->getDirectoryListing(); |
|
102
|
|
|
|
|
103
|
|
|
foreach ($avatars as $avatar) { |
|
104
|
|
|
$avatar->delete(); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the avatar for an user. |
|
110
|
|
|
* |
|
111
|
|
|
* If there is no avatar file yet, one is generated. |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $size |
|
114
|
|
|
* @return ISimpleFile |
|
115
|
|
|
* @throws NotFoundException |
|
116
|
|
|
* @throws \OCP\Files\NotPermittedException |
|
117
|
|
|
* @throws \OCP\PreConditionNotMetException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getFile($size) { |
|
120
|
|
|
$size = (int) $size; |
|
121
|
|
|
|
|
122
|
|
|
$ext = 'png'; |
|
123
|
|
|
|
|
124
|
|
|
if ($size === -1) { |
|
125
|
|
|
$path = 'avatar-placeholder.' . $ext; |
|
126
|
|
|
} else { |
|
127
|
|
|
$path = 'avatar-placeholder.' . $size . '.' . $ext; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
try { |
|
131
|
|
|
$file = $this->folder->getFile($path); |
|
132
|
|
|
} catch (NotFoundException $e) { |
|
133
|
|
|
if ($size <= 0) { |
|
134
|
|
|
throw new NotFoundException; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (!$data = $this->generateAvatarFromSvg($size)) { |
|
138
|
|
|
$data = $this->generateAvatar($this->getDisplayName(), $size); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
try { |
|
142
|
|
|
$file = $this->folder->newFile($path); |
|
143
|
|
|
$file->putContent($data); |
|
144
|
|
|
} catch (NotPermittedException $e) { |
|
145
|
|
|
$this->logger->error('Failed to save avatar placeholder for ' . $this->user->getUID()); |
|
146
|
|
|
throw new NotFoundException(); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $file; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns the user display name. |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getDisplayName(): string { |
|
159
|
|
|
return $this->user->getDisplayName(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Handles user changes. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $feature The changed feature |
|
166
|
|
|
* @param mixed $oldValue The previous value |
|
167
|
|
|
* @param mixed $newValue The new value |
|
168
|
|
|
* @throws NotPermittedException |
|
169
|
|
|
* @throws \OCP\PreConditionNotMetException |
|
170
|
|
|
*/ |
|
171
|
|
|
public function userChanged($feature, $oldValue, $newValue) { |
|
172
|
|
|
$this->remove(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Check if the avatar of a user is a custom uploaded one |
|
177
|
|
|
* |
|
178
|
|
|
* @return bool |
|
179
|
|
|
*/ |
|
180
|
|
|
public function isCustomAvatar(): bool { |
|
181
|
|
|
return false; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: