|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2019-2020 Matias De lellis <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Matias De lellis <[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
|
|
|
|
|
24
|
|
|
namespace OCA\FaceRecognition\Controller; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\AppFramework\Controller; |
|
27
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
28
|
|
|
use OCP\IRequest; |
|
29
|
|
|
use OCP\IUserManager; |
|
30
|
|
|
use OCP\IUser; |
|
31
|
|
|
use OCP\IL10N; |
|
32
|
|
|
|
|
33
|
|
|
use OCP\Util as OCP_Util; |
|
34
|
|
|
|
|
35
|
|
|
use OCA\FaceRecognition\Helper\MemoryLimits; |
|
36
|
|
|
|
|
37
|
|
|
use OCA\FaceRecognition\Model\IModel; |
|
38
|
|
|
use OCA\FaceRecognition\Model\ModelManager; |
|
39
|
|
|
|
|
40
|
|
|
use OCA\FaceRecognition\Service\SettingsService; |
|
41
|
|
|
|
|
42
|
|
|
class SettingsController extends Controller { |
|
43
|
|
|
|
|
44
|
|
|
/** @var ModelManager */ |
|
45
|
|
|
private $modelManager; |
|
46
|
|
|
|
|
47
|
|
|
/** @var SettingsService */ |
|
48
|
|
|
private $settingsService; |
|
49
|
|
|
|
|
50
|
|
|
/** @var \OCP\IL10N */ |
|
51
|
|
|
protected $l10n; |
|
52
|
|
|
|
|
53
|
|
|
/** @var IUserManager */ |
|
54
|
|
|
private $userManager; |
|
55
|
|
|
|
|
56
|
|
|
/** @var string */ |
|
57
|
|
|
private $userId; |
|
58
|
|
|
|
|
59
|
|
|
const STATE_OK = 0; |
|
60
|
|
|
const STATE_FALSE = 1; |
|
61
|
|
|
const STATE_SUCCESS = 2; |
|
62
|
|
|
const STATE_ERROR = 3; |
|
63
|
|
|
|
|
64
|
|
|
public function __construct ($appName, |
|
65
|
|
|
IRequest $request, |
|
66
|
|
|
ModelManager $modelManager, |
|
67
|
|
|
SettingsService $settingsService, |
|
68
|
|
|
IL10N $l10n, |
|
69
|
|
|
IUserManager $userManager, |
|
70
|
|
|
$userId) |
|
71
|
|
|
{ |
|
72
|
|
|
parent::__construct($appName, $request); |
|
73
|
|
|
|
|
74
|
|
|
$this->appName = $appName; |
|
75
|
|
|
$this->modelManager = $modelManager; |
|
76
|
|
|
$this->settingsService = $settingsService; |
|
77
|
|
|
$this->l10n = $l10n; |
|
78
|
|
|
$this->userManager = $userManager; |
|
79
|
|
|
$this->userId = $userId; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @NoAdminRequired |
|
84
|
|
|
* @param $type |
|
85
|
|
|
* @param $value |
|
86
|
|
|
* @return JSONResponse |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setUserValue($type, $value) { |
|
89
|
|
|
$status = self::STATE_SUCCESS; |
|
90
|
|
|
|
|
91
|
|
|
switch ($type) { |
|
92
|
|
|
case SettingsService::USER_ENABLED_KEY: |
|
93
|
|
|
$enabled = ($value === 'true'); |
|
94
|
|
|
$this->settingsService->setUserEnabled($enabled); |
|
95
|
|
|
if ($enabled) { |
|
96
|
|
|
$this->settingsService->setUserFullScanDone(false); |
|
97
|
|
|
} |
|
98
|
|
|
break; |
|
99
|
|
|
default: |
|
100
|
|
|
$status = self::STATE_ERROR; |
|
101
|
|
|
break; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Response |
|
105
|
|
|
$result = [ |
|
106
|
|
|
'status' => $status, |
|
107
|
|
|
'value' => $value |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
return new JSONResponse($result); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @NoAdminRequired |
|
115
|
|
|
* @param $type |
|
116
|
|
|
* @return JSONResponse |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getUserValue($type) { |
|
119
|
|
|
$status = self::STATE_OK; |
|
120
|
|
|
$value ='nodata'; |
|
121
|
|
|
|
|
122
|
|
|
switch ($type) { |
|
123
|
|
|
case SettingsService::USER_ENABLED_KEY: |
|
124
|
|
|
$value = $this->settingsService->getUserEnabled(); |
|
125
|
|
|
break; |
|
126
|
|
|
default: |
|
127
|
|
|
$status = self::STATE_FALSE; |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$result = [ |
|
132
|
|
|
'status' => $status, |
|
133
|
|
|
'value' => $value |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
return new JSONResponse($result); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param $type |
|
141
|
|
|
* @param $value |
|
142
|
|
|
* @return JSONResponse |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setAppValue($type, $value) { |
|
145
|
|
|
$status = self::STATE_SUCCESS; |
|
146
|
|
|
$message = ""; |
|
147
|
|
|
|
|
148
|
|
|
switch ($type) { |
|
149
|
|
|
case SettingsService::ANALYSIS_IMAGE_AREA_KEY: |
|
150
|
|
|
if (!is_numeric ($value)) { |
|
151
|
|
|
$status = self::STATE_ERROR; |
|
152
|
|
|
$message = $this->l10n->t("The format seems to be incorrect."); |
|
153
|
|
|
$value = '-1'; |
|
154
|
|
|
break; |
|
155
|
|
|
} |
|
156
|
|
|
$model = $this->modelManager->getCurrentModel(); |
|
157
|
|
|
if (is_null($model)) { |
|
158
|
|
|
$status = self::STATE_ERROR; |
|
159
|
|
|
$message = $this->l10n->t("Seems you haven't set up any analysis model yet"); |
|
160
|
|
|
$value = '-1'; |
|
161
|
|
|
break; |
|
162
|
|
|
} |
|
163
|
|
|
// Apply prudent limits. |
|
164
|
|
|
if ($value > 0 && $value < SettingsService::MINIMUM_ANALYSIS_IMAGE_AREA) { |
|
165
|
|
|
$value = SettingsService::MINIMUM_ANALYSIS_IMAGE_AREA; |
|
166
|
|
|
$message = $this->l10n->t("The minimum recommended area is %s", $this->getFourByThreeRelation($value)); |
|
167
|
|
|
$status = self::STATE_ERROR; |
|
168
|
|
|
} else if ($value > SettingsService::MAXIMUM_ANALYSIS_IMAGE_AREA) { |
|
169
|
|
|
$value = SettingsService::MAXIMUM_ANALYSIS_IMAGE_AREA; |
|
170
|
|
|
$message = $this->l10n->t("The maximum recommended area is %s", $this->getFourByThreeRelation($value)); |
|
171
|
|
|
$status = self::STATE_ERROR; |
|
172
|
|
|
} |
|
173
|
|
|
$maxImageArea = $model->getMaximumArea(); |
|
174
|
|
|
if ($value > $maxImageArea) { |
|
175
|
|
|
$value = $maxImageArea; |
|
176
|
|
|
$message = $this->l10n->t("The model does not recommend an area greater than %s", $this->getFourByThreeRelation($value)); |
|
177
|
|
|
$status = self::STATE_ERROR; |
|
178
|
|
|
} |
|
179
|
|
|
// If any validation error saves the value |
|
180
|
|
|
if ($status !== self::STATE_ERROR) { |
|
181
|
|
|
$message = $this->l10n->t("The changes were saved. It will be taken into account in the next analysis."); |
|
182
|
|
|
$this->settingsService->setAnalysisImageArea($value); |
|
183
|
|
|
} |
|
184
|
|
|
break; |
|
185
|
|
|
case SettingsService::SENSITIVITY_KEY: |
|
186
|
|
|
$this->settingsService->setSensitivity($value); |
|
187
|
|
|
$this->userManager->callForSeenUsers(function(IUser $user) { |
|
188
|
|
|
$this->settingsService->setNeedRecreateClusters(true, $user->getUID()); |
|
189
|
|
|
}); |
|
190
|
|
|
break; |
|
191
|
|
|
case SettingsService::MINIMUM_CONFIDENCE_KEY: |
|
192
|
|
|
$this->settingsService->setMinimumConfidence($value); |
|
193
|
|
|
$this->userManager->callForSeenUsers(function(IUser $user) { |
|
194
|
|
|
$this->settingsService->setNeedRecreateClusters(true, $user->getUID()); |
|
195
|
|
|
}); |
|
196
|
|
|
break; |
|
197
|
|
|
case SettingsService::MINIMUM_FACE_SIZE_KEY: |
|
198
|
|
|
$this->settingsService->setMinimumFaceSize($value); |
|
199
|
|
|
$this->userManager->callForSeenUsers(function(IUser $user) { |
|
200
|
|
|
$this->settingsService->setNeedRecreateClusters(true, $user->getUID()); |
|
201
|
|
|
}); |
|
202
|
|
|
break; |
|
203
|
|
|
case SettingsService::SHOW_NOT_GROUPED_KEY: |
|
204
|
|
|
$this->settingsService->setShowNotGrouped($value === 'true' ? true : false); |
|
205
|
|
|
break; |
|
206
|
|
|
case SettingsService::OBFUSCATE_FACE_THUMBS_KEY: |
|
207
|
|
|
$this->settingsService->setObfuscateFaces(!$this->settingsService->getObfuscateFaces()); |
|
208
|
|
|
break; |
|
209
|
|
|
default: |
|
210
|
|
|
$status = self::STATE_ERROR; |
|
211
|
|
|
break; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// Response |
|
215
|
|
|
$result = [ |
|
216
|
|
|
'status' => $status, |
|
217
|
|
|
'message' => $message, |
|
218
|
|
|
'value' => $value |
|
219
|
|
|
]; |
|
220
|
|
|
|
|
221
|
|
|
return new JSONResponse($result); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param $type |
|
226
|
|
|
* @return JSONResponse |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getAppValue($type) { |
|
229
|
|
|
$status = self::STATE_OK; |
|
230
|
|
|
$value = 'nodata'; |
|
231
|
|
|
|
|
232
|
|
|
switch ($type) { |
|
233
|
|
|
case SettingsService::SENSITIVITY_KEY: |
|
234
|
|
|
$value = $this->settingsService->getSensitivity(); |
|
235
|
|
|
break; |
|
236
|
|
|
case SettingsService::MINIMUM_CONFIDENCE_KEY: |
|
237
|
|
|
$value = $this->settingsService->getMinimumConfidence(); |
|
238
|
|
|
break; |
|
239
|
|
|
case SettingsService::MINIMUM_FACE_SIZE_KEY: |
|
240
|
|
|
$value = $this->settingsService->getMinimumFaceSize(); |
|
241
|
|
|
break; |
|
242
|
|
|
case SettingsService::ANALYSIS_IMAGE_AREA_KEY: |
|
243
|
|
|
$value = $this->settingsService->getAnalysisImageArea(); |
|
244
|
|
|
break; |
|
245
|
|
|
case SettingsService::SHOW_NOT_GROUPED_KEY: |
|
246
|
|
|
$value = $this->settingsService->getShowNotGrouped(); |
|
247
|
|
|
break; |
|
248
|
|
|
default: |
|
249
|
|
|
break; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
// Response |
|
253
|
|
|
$result = [ |
|
254
|
|
|
'status' => $status, |
|
255
|
|
|
'value' => $value |
|
256
|
|
|
]; |
|
257
|
|
|
|
|
258
|
|
|
return new JSONResponse($result); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Get an approximate image size with 4x3 ratio |
|
263
|
|
|
* @param int $area area in pixels^2 |
|
264
|
|
|
* @return string |
|
265
|
|
|
*/ |
|
266
|
|
|
private function getFourByThreeRelation(int $area): string { |
|
267
|
|
|
$width = intval(sqrt($area * 4 / 3)); |
|
268
|
|
|
$height = intval($width * 3 / 4); |
|
269
|
|
|
return $width . 'x' . $height; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
} |
|
273
|
|
|
|