1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2020 Matias De lellis <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @author Matias De lellis <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\FaceRecognition\Service; |
26
|
|
|
|
27
|
|
|
use OCA\FaceRecognition\AppInfo\Application; |
28
|
|
|
|
29
|
|
|
use OCA\FaceRecognition\Model\ModelManager; |
30
|
|
|
|
31
|
|
|
use OCP\IConfig; |
32
|
|
|
|
33
|
|
|
class SettingsService { |
34
|
|
|
|
35
|
|
|
/* |
36
|
|
|
* System |
37
|
|
|
*/ |
38
|
|
|
const MINIMUM_SYSTEM_MEMORY_REQUIREMENTS = 2 * 1024 * 1024 * 1024; |
39
|
|
|
|
40
|
|
|
/* |
41
|
|
|
* Settings keys and default values. |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
/** Current Model used to analyze */ |
45
|
|
|
const CURRENT_MODEL_KEY = 'model'; |
46
|
|
|
const FALLBACK_CURRENT_MODEL = -1; |
47
|
|
|
|
48
|
|
|
/** Image area that used used for analysis */ |
49
|
|
|
const ANALYSIS_IMAGE_AREA_KEY = 'analysis_image_area'; |
50
|
|
|
const MINIMUM_ANALYSIS_IMAGE_AREA = 640*480; |
51
|
|
|
const DEFAULT_ANALYSIS_IMAGE_AREA = -1; // It is dynamically configured according to hardware |
52
|
|
|
const MAXIMUM_ANALYSIS_IMAGE_AREA = 3840*2160; |
53
|
|
|
|
54
|
|
|
/** Sensitivity used to clustering */ |
55
|
|
|
const SENSITIVITY_KEY = 'sensitivity'; |
56
|
|
|
const MINIMUM_SENSITIVITY = '0.2'; |
57
|
|
|
const DEFAULT_SENSITIVITY = '0.4'; |
58
|
|
|
const MAXIMUM_SENSITIVITY = '0.6'; |
59
|
|
|
|
60
|
|
|
/** Minimum confidence used to try to clustring faces */ |
61
|
|
|
const MINIMUM_CONFIDENCE_KEY = 'min_confidence'; |
62
|
|
|
const MINIMUM_MINIMUM_CONFIDENCE = '0.0'; |
63
|
|
|
const DEFAULT_MINIMUM_CONFIDENCE = '0.99'; |
64
|
|
|
const MAXIMUM_MINIMUM_CONFIDENCE = '1.1'; |
65
|
|
|
|
66
|
|
|
/** Show single persons on clustes view */ |
67
|
|
|
const SHOW_NOT_GROUPED_KEY = 'show_not_grouped'; |
68
|
|
|
const DEFAULT_SHOW_NOT_GROUPED = 'false'; |
69
|
|
|
|
70
|
|
|
/** User setting what indicates if has the analysis enabled */ |
71
|
|
|
const USER_ENABLED_KEY = 'enabled'; |
72
|
|
|
const DEFAULT_USER_ENABLED = 'false'; |
73
|
|
|
|
74
|
|
|
/** User setting that remember last images checked */ |
75
|
|
|
const STALE_IMAGES_LAST_CHECKED_KEY = 'stale_images_last_checked'; |
76
|
|
|
const DEFAULT_STALE_IMAGES_LAST_CHECKED = '0'; |
77
|
|
|
|
78
|
|
|
/** Define if for some reason need remove old images */ |
79
|
|
|
const STALE_IMAGES_REMOVAL_NEEDED_KEY = 'stale_images_removal_needed'; |
80
|
|
|
const DEFAULT_STALE_IMAGES_REMOVAL_NEEDED = 'false'; |
81
|
|
|
|
82
|
|
|
/** User setting that indicate when scan finished */ |
83
|
|
|
const FULL_IMAGE_SCAN_DONE_KEY = 'full_image_scan_done'; |
84
|
|
|
const DEFAULT_FULL_IMAGE_SCAN_DONE = 'false'; |
85
|
|
|
|
86
|
|
|
/** User setting that indicate that need to recreate clusters */ |
87
|
|
|
const USER_RECREATE_CLUSTERS_KEY = 'recreate_clusters'; |
88
|
|
|
const DEFAULT_USER_RECREATE_CLUSTERS = 'false'; |
89
|
|
|
|
90
|
|
|
/** User setting that indicate that is forced to create clusters */ |
91
|
|
|
const FORCE_CREATE_CLUSTERS_KEY = 'force_create_clusters'; |
92
|
|
|
const DEFAULT_FORCE_CREATE_CLUSTERS = 'false'; |
93
|
|
|
|
94
|
|
|
/** Hidden setting that allows to analyze shared files */ |
95
|
|
|
const HANDLE_SHARED_FILES_KEY = 'handle_shared_files'; |
96
|
|
|
const DEFAULT_HANDLE_SHARED_FILES = 'false'; |
97
|
|
|
|
98
|
|
|
/** Hidden setting that allows to analyze external files */ |
99
|
|
|
const HANDLE_EXTERNAL_FILES_KEY = 'handle_external_files'; |
100
|
|
|
const DEFAULT_HANDLE_EXTERNAL_FILES = 'false'; |
101
|
|
|
|
102
|
|
|
/** Hidden setting that indicate minimum large of image to analyze */ |
103
|
|
|
const MINIMUM_IMAGE_SIZE_KEY = 'min_image_size'; |
104
|
|
|
const DEFAULT_MINIMUM_IMAGE_SIZE = '512'; |
105
|
|
|
|
106
|
|
|
/** Hidden setting that indicate maximum area of image to analyze */ |
107
|
|
|
const MAXIMUM_IMAGE_AREA_KEY = 'max_image_area'; |
108
|
|
|
const DEFAULT_MAXIMUM_IMAGE_AREA = '-1'; |
109
|
|
|
|
110
|
|
|
/** Hidden setting that allows obfuscate that faces for security */ |
111
|
|
|
const OBFUSCATE_FACE_THUMBS_KEY = 'obfuscate_faces'; |
112
|
|
|
const DEFAULT_OBFUSCATE_FACE_THUMBS = 'false'; |
113
|
|
|
|
114
|
|
|
/** System setting to enable mimetypes */ |
115
|
|
|
const SYSTEM_ENABLED_MIMETYPES = 'enabledFaceRecognitionMimetype'; |
116
|
|
|
private $allowedMimetypes = ['image/jpeg', 'image/png']; |
117
|
|
|
private $cachedAllowedMimetypes = false; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* SettingsService |
121
|
|
|
*/ |
122
|
|
|
|
123
|
|
|
/** @var IConfig Config */ |
124
|
|
|
private $config; |
125
|
|
|
|
126
|
|
|
/** @var string|null */ |
127
|
|
|
private $userId; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param IConfig $config |
131
|
|
|
* @param string $userId |
132
|
|
|
*/ |
133
|
1 |
|
public function __construct(IConfig $config, |
134
|
|
|
$userId) |
135
|
|
|
{ |
136
|
1 |
|
$this->config = $config; |
137
|
1 |
|
$this->userId = $userId; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/* |
141
|
|
|
* User settings. |
142
|
|
|
*/ |
143
|
10 |
|
public function getUserEnabled ($userId = null): bool { |
144
|
10 |
|
$enabled = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, self::DEFAULT_USER_ENABLED); |
145
|
10 |
|
return ($enabled === 'true'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setUserEnabled (bool $enabled, $userId = null) { |
149
|
|
|
$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, $enabled ? "true" : "false"); |
150
|
|
|
} |
151
|
|
|
|
152
|
10 |
|
public function getUserFullScanDone ($userId = null): bool { |
153
|
10 |
|
$fullScanDone = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, self::DEFAULT_FULL_IMAGE_SCAN_DONE); |
154
|
10 |
|
return ($fullScanDone === 'true'); |
155
|
|
|
} |
156
|
|
|
|
157
|
28 |
|
public function setUserFullScanDone (bool $fullScanDone, $userId = null) { |
158
|
28 |
|
$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, $fullScanDone ? "true" : "false"); |
159
|
28 |
|
} |
160
|
|
|
|
161
|
3 |
|
public function getNeedRemoveStaleImages ($userId = null): bool { |
162
|
3 |
|
$needRemoval = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, self::DEFAULT_STALE_IMAGES_REMOVAL_NEEDED); |
163
|
3 |
|
return ($needRemoval === 'true'); |
164
|
|
|
} |
165
|
|
|
|
166
|
2 |
|
public function setNeedRemoveStaleImages (bool $needRemoval, $userId = null) { |
167
|
2 |
|
$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, $needRemoval ? "true" : "false"); |
168
|
2 |
|
} |
169
|
|
|
|
170
|
2 |
|
public function getLastStaleImageChecked ($userId = null): int { |
171
|
2 |
|
return intval($this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_LAST_CHECKED_KEY, self::DEFAULT_STALE_IMAGES_LAST_CHECKED)); |
172
|
|
|
} |
173
|
|
|
|
174
|
2 |
|
public function setLastStaleImageChecked (int $lastCheck, $userId = null) { |
175
|
2 |
|
$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_LAST_CHECKED_KEY, $lastCheck); |
176
|
2 |
|
} |
177
|
|
|
|
178
|
|
|
public function getNeedRecreateClusters ($userId = null): bool { |
179
|
|
|
$needRecreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, self::DEFAULT_USER_RECREATE_CLUSTERS); |
180
|
|
|
return ($needRecreate === 'true'); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
public function setNeedRecreateClusters (bool $needRecreate, $userId = null) { |
184
|
1 |
|
$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, $needRecreate ? "true" : "false"); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
// Private function used only on tests |
188
|
1 |
|
public function _getForceCreateClusters ($userId = null): bool { |
189
|
1 |
|
$forceCreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, self::DEFAULT_FORCE_CREATE_CLUSTERS); |
190
|
1 |
|
return ($forceCreate === 'true'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// Private function used only on tests |
194
|
1 |
|
public function _setForceCreateClusters (bool $forceCreate, $userId = null) { |
195
|
1 |
|
$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, $forceCreate ? "true" : "false"); |
196
|
1 |
|
} |
197
|
|
|
|
198
|
|
|
/* |
199
|
|
|
* Admin and process settings. |
200
|
|
|
*/ |
201
|
12 |
|
public function getCurrentFaceModel(): int { |
202
|
12 |
|
return intval($this->config->getAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, self::FALLBACK_CURRENT_MODEL)); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function setCurrentFaceModel(int $model) { |
206
|
|
|
$this->config->setAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, strval($model)); |
207
|
|
|
} |
208
|
|
|
|
209
|
4 |
|
public function getAnalysisImageArea(): int { |
210
|
4 |
|
return intval($this->config->getAppValue(Application::APP_NAME, self::ANALYSIS_IMAGE_AREA_KEY, self::DEFAULT_ANALYSIS_IMAGE_AREA)); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function setAnalysisImageArea(int $imageArea) { |
214
|
|
|
$this->config->setAppValue(Application::APP_NAME, self::ANALYSIS_IMAGE_AREA_KEY, strval($imageArea)); |
215
|
|
|
} |
216
|
|
|
|
217
|
1 |
|
public function getSensitivity(): float { |
218
|
1 |
|
return floatval($this->config->getAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, self::DEFAULT_SENSITIVITY)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function setSensitivity($sensitivity) { |
222
|
|
|
$this->config->setAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, $sensitivity); |
223
|
|
|
} |
224
|
|
|
|
225
|
1 |
|
public function getMinimumConfidence(): float { |
226
|
1 |
|
return floatval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, self::DEFAULT_MINIMUM_CONFIDENCE)); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function setMinimumConfidence($confidence) { |
230
|
|
|
$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, $confidence); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function getShowNotGrouped(): bool { |
234
|
|
|
$show = $this->config->getAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, self::DEFAULT_SHOW_NOT_GROUPED); |
235
|
|
|
return ($show === 'true'); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
public function setShowNotGrouped(bool $show) { |
239
|
|
|
$this->config->setAppValue(Application::APP_NAME, self::SHOW_NOT_GROUPED_KEY, $show ? "true" : "false"); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* The next settings are advanced preferences that are not available in gui. |
244
|
|
|
* See: https://github.com/matiasdelellis/facerecognition/wiki/Settings#hidden-settings |
245
|
|
|
*/ |
246
|
|
|
public function getHandleSharedFiles(): bool { |
247
|
|
|
$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_SHARED_FILES_KEY, self::DEFAULT_HANDLE_SHARED_FILES); |
248
|
|
|
return ($handle === 'true'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function getHandleExternalFiles(): bool { |
252
|
|
|
$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_EXTERNAL_FILES_KEY, self::DEFAULT_HANDLE_EXTERNAL_FILES); |
253
|
|
|
return ($handle === 'true'); |
254
|
|
|
} |
255
|
|
|
|
256
|
4 |
|
public function getMinimumImageSize(): int { |
257
|
4 |
|
return intval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_IMAGE_SIZE_KEY, self::DEFAULT_MINIMUM_IMAGE_SIZE)); |
258
|
|
|
} |
259
|
|
|
|
260
|
4 |
|
public function getMaximumImageArea(): int { |
261
|
4 |
|
return intval($this->config->getAppValue(Application::APP_NAME, self::MAXIMUM_IMAGE_AREA_KEY, self::DEFAULT_MAXIMUM_IMAGE_AREA)); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function getObfuscateFaces(): bool { |
265
|
|
|
$obfuscate = $this->config->getAppValue(Application::APP_NAME, self::OBFUSCATE_FACE_THUMBS_KEY, self::DEFAULT_OBFUSCATE_FACE_THUMBS); |
266
|
|
|
return ($obfuscate === 'true'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function setObfuscateFaces(bool $obfuscate) { |
270
|
|
|
$this->config->setAppValue(Application::APP_NAME, self::OBFUSCATE_FACE_THUMBS_KEY, $obfuscate ? 'true' : 'false'); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* System settings that must be configured according to the server configuration. |
275
|
|
|
*/ |
276
|
8 |
|
public function isAllowedMimetype(string $mimetype): bool { |
277
|
8 |
|
if (!$this->cachedAllowedMimetypes) { |
278
|
1 |
|
$systemMimetypes = $this->config->getSystemValue(self::SYSTEM_ENABLED_MIMETYPES, $this->allowedMimetypes); |
279
|
1 |
|
$this->allowedMimetypes = array_merge($this->allowedMimetypes, $systemMimetypes); |
280
|
1 |
|
$this->allowedMimetypes = array_unique($this->allowedMimetypes); |
281
|
|
|
|
282
|
1 |
|
$this->cachedAllowedMimetypes = true; |
283
|
|
|
} |
284
|
|
|
|
285
|
8 |
|
return in_array($mimetype, $this->allowedMimetypes); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|