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::SHOW_NOT_GROUPED_KEY: |
198
|
|
|
$this->settingsService->setShowNotGrouped($value === 'true' ? true : false); |
199
|
|
|
break; |
200
|
|
|
case SettingsService::OBFUSCATE_FACE_THUMBS_KEY: |
201
|
|
|
$this->settingsService->setObfuscateFaces(!$this->settingsService->getObfuscateFaces()); |
202
|
|
|
break; |
203
|
|
|
default: |
204
|
|
|
$status = self::STATE_ERROR; |
205
|
|
|
break; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Response |
209
|
|
|
$result = [ |
210
|
|
|
'status' => $status, |
211
|
|
|
'message' => $message, |
212
|
|
|
'value' => $value |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
return new JSONResponse($result); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param $type |
220
|
|
|
* @return JSONResponse |
221
|
|
|
*/ |
222
|
|
|
public function getAppValue($type) { |
223
|
|
|
$status = self::STATE_OK; |
224
|
|
|
$value = 'nodata'; |
225
|
|
|
|
226
|
|
|
switch ($type) { |
227
|
|
|
case SettingsService::SENSITIVITY_KEY: |
228
|
|
|
$value = $this->settingsService->getSensitivity(); |
229
|
|
|
break; |
230
|
|
|
case SettingsService::MINIMUM_CONFIDENCE_KEY: |
231
|
|
|
$value = $this->settingsService->getMinimumConfidence(); |
232
|
|
|
break; |
233
|
|
|
case SettingsService::ANALYSIS_IMAGE_AREA_KEY: |
234
|
|
|
$value = $this->settingsService->getAnalysisImageArea(); |
235
|
|
|
break; |
236
|
|
|
case SettingsService::SHOW_NOT_GROUPED_KEY: |
237
|
|
|
$value = $this->settingsService->getShowNotGrouped(); |
238
|
|
|
break; |
239
|
|
|
default: |
240
|
|
|
break; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// Response |
244
|
|
|
$result = [ |
245
|
|
|
'status' => $status, |
246
|
|
|
'value' => $value |
247
|
|
|
]; |
248
|
|
|
|
249
|
|
|
return new JSONResponse($result); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get an approximate image size with 4x3 ratio |
254
|
|
|
* @param int $area area in pixels^2 |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
private function getFourByThreeRelation(int $area): string { |
258
|
|
|
$width = intval(sqrt($area * 4 / 3)); |
259
|
|
|
$height = intval($width * 3 / 4); |
260
|
|
|
return $width . 'x' . $height . ' (4x3)'; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
} |
264
|
|
|
|