Passed
Push — master ( 2af918...8e89fd )
by Matias
08:57
created

SettingsService::getDefaultUserEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
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 = 1 * 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
	/* Enabling facial recognition for users */
49
	const DEFAULT_USER_ENABLED_KEY = 'default_enabled';
50
	const DEFAULT_USER_ENABLED = 'false';
51
52
	/* Assigned memory for image processing */
53
	const ASSIGNED_MEMORY_KEY = 'assigned_memory';
54
	const MINIMUM_ASSIGNED_MEMORY = (1 * 1024 * 1024 * 1024) * 2.0 / 3.0;
55
	const DEFAULT_ASSIGNED_MEMORY = -1;
56
	const MAXIMUN_ASSIGNED_MEMORY = 8 * 1024 * 1024 * 1024;
57
58
	/** Image area that used used for analysis */
59
	const ANALYSIS_IMAGE_AREA_KEY = 'analysis_image_area';
60
	const MINIMUM_ANALYSIS_IMAGE_AREA = 640*480;
61
	const DEFAULT_ANALYSIS_IMAGE_AREA = -1; // It is dynamically configured according to hardware
62
	const MAXIMUM_ANALYSIS_IMAGE_AREA = 3840*2160;
63
64
	/** Sensitivity used to clustering */
65
	const SENSITIVITY_KEY = 'sensitivity';
66
	const MINIMUM_SENSITIVITY = '0.2';
67
	const DEFAULT_SENSITIVITY = '0.4';
68
	const MAXIMUM_SENSITIVITY = '0.6';
69
70
	/** Minimum confidence used to try to clustring faces */
71
	const MINIMUM_CONFIDENCE_KEY = 'min_confidence';
72
	const MINIMUM_MINIMUM_CONFIDENCE = '0.0';
73
	const DEFAULT_MINIMUM_CONFIDENCE = '0.99';
74
	const MAXIMUM_MINIMUM_CONFIDENCE = '1.1';
75
76
	/** Minimum face size used to try to clustring faces */
77
	const MINIMUM_FACE_SIZE_KEY = 'min_face_size';
78
	const MINIMUM_MINIMUM_FACE_SIZE = '0';
79
	const DEFAULT_MINIMUM_FACE_SIZE = '40';
80
	const MAXIMUM_MINIMUM_FACE_SIZE = '250';
81
82
	/** Minimum of faces in cluster */
83
	const MINIMUM_FACES_IN_CLUSTER_KEY = 'min_faces_in_cluster';
84
	const MINIMUM_MINIMUM_FACES_IN_CLUSTER = '1';
85
	const DEFAULT_MINIMUM_FACES_IN_CLUSTER = '5';
86
	const MAXIMUM_MINIMUM_FACES_IN_CLUSTER = '20';
87
88
	/** User setting what indicates if has the analysis enabled */
89
	const USER_ENABLED_KEY = 'enabled';
90
	// The default is defined by system 'default_enabled' key
91
92
	/** User setting that remember last images checked */
93
	const STALE_IMAGES_LAST_CHECKED_KEY = 'stale_images_last_checked';
94
	const DEFAULT_STALE_IMAGES_LAST_CHECKED = '0';
95
96
	/** Define if for some reason need remove old images */
97
	const STALE_IMAGES_REMOVAL_NEEDED_KEY = 'stale_images_removal_needed';
98
	const DEFAULT_STALE_IMAGES_REMOVAL_NEEDED = 'false';
99
100
	/** User setting that indicate when scan finished */
101
	const FULL_IMAGE_SCAN_DONE_KEY = 'full_image_scan_done';
102
	const DEFAULT_FULL_IMAGE_SCAN_DONE = 'false';
103
104
	/** User setting that indicate that need to recreate clusters */
105
	const USER_RECREATE_CLUSTERS_KEY = 'recreate_clusters';
106
	const DEFAULT_USER_RECREATE_CLUSTERS = 'false';
107
108
	/** User setting that indicate that is forced to create clusters */
109
	const FORCE_CREATE_CLUSTERS_KEY = 'force_create_clusters';
110
	const DEFAULT_FORCE_CREATE_CLUSTERS = 'false';
111
112
	/** Hidden setting that allows to analyze shared files */
113
	const HANDLE_SHARED_FILES_KEY = 'handle_shared_files';
114
	const DEFAULT_HANDLE_SHARED_FILES = 'false';
115
116
	/** Hidden setting that allows to analyze external files */
117
	const HANDLE_EXTERNAL_FILES_KEY = 'handle_external_files';
118
	const DEFAULT_HANDLE_EXTERNAL_FILES = 'false';
119
120
	/** Hidden setting that allows to analyze group files */
121
	const HANDLE_GROUP_FILES_KEY = 'handle_group_files';
122
	const DEFAULT_HANDLE_GROUP_FILES = 'false';
123
124
	/** Hidden setting that indicate minimum large of image to analyze */
125
	const MINIMUM_IMAGE_SIZE_KEY = 'min_image_size';
126
	const DEFAULT_MINIMUM_IMAGE_SIZE = '512';
127
128
	/** Hidden setting that indicate maximum area of image to analyze */
129
	const MAXIMUM_IMAGE_AREA_KEY = 'max_image_area';
130
	const DEFAULT_MAXIMUM_IMAGE_AREA = '-1';
131
132
	/** Hidden setting that allows obfuscate that faces for security */
133
	const OBFUSCATE_FACE_THUMBS_KEY = 'obfuscate_faces';
134
	const DEFAULT_OBFUSCATE_FACE_THUMBS = 'false';
135
136
	/** System setting to enable mimetypes */
137
138
	const SYSTEM_ENABLED_MIMETYPES = 'enabledFaceRecognitionMimetype';
139
	private $allowedMimetypes = ['image/jpeg', 'image/png'];
140
	private $cachedAllowedMimetypes = false;
141
142
	/** System setting to use custom folder for models */
143
	const SYSTEM_MODEL_PATH = 'facerecognition.model_path';
144
145
	/** System setting to configure external model */
146
	const SYSTEM_EXTERNAL_MODEL_URL = 'facerecognition.external_model_url';
147
	const SYSTEM_EXTERNAL_MODEL_API_KEY = 'facerecognition.external_model_api_key';
148
149
	/**
150
	 * SettingsService
151
	 */
152
153
	/** @var IConfig Config */
154
	private $config;
155
156
	/**  @var string|null */
157
	private $userId;
158
159
	/**
160
	 * @param IConfig $config
161
	 * @param string $userId
162
	 */
163 1
	public function __construct(IConfig $config,
164
	                            $userId)
165
	{
166 1
		$this->config = $config;
167 1
		$this->userId = $userId;
168
	}
169
170
	/*
171
	 * User settings.
172
	 */
173
	/**
174
	 * @param null|string $userId
175
	 */
176 10
	public function getUserEnabled (?string $userId = null): bool {
177 10
		$enabled = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY,
178 10
		                                       $this->getDefaultUserEnabled());
179 10
		return ($enabled === 'true');
180
	}
181
182
	public function setUserEnabled (bool $enabled, $userId = null): void {
183
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_ENABLED_KEY, $enabled ? "true" : "false");
184
	}
185
186
	/**
187
	 * @param null|string $userId
188
	 */
189 10
	public function getUserFullScanDone (?string $userId = null): bool {
190 10
		$fullScanDone = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, self::DEFAULT_FULL_IMAGE_SCAN_DONE);
191 10
		return ($fullScanDone === 'true');
192
	}
193
194
	/**
195
	 * @param null|string $userId
196
	 */
197 28
	public function setUserFullScanDone (bool $fullScanDone, ?string $userId = null): void {
198 28
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FULL_IMAGE_SCAN_DONE_KEY, $fullScanDone ? "true" : "false");
199
	}
200
201 3
	public function getNeedRemoveStaleImages ($userId = null): bool {
202 3
		$needRemoval = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, self::DEFAULT_STALE_IMAGES_REMOVAL_NEEDED);
203 3
		return ($needRemoval === 'true');
204
	}
205
206
	/**
207
	 * @param null|string $userId
208
	 */
209 2
	public function setNeedRemoveStaleImages (bool $needRemoval, ?string $userId = null): void {
210 2
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_REMOVAL_NEEDED_KEY, $needRemoval ? "true" : "false");
211
	}
212
213
	/**
214
	 * @param null|string $userId
215
	 */
216 2
	public function getLastStaleImageChecked (?string $userId = null): int {
217 2
		return intval($this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_LAST_CHECKED_KEY, self::DEFAULT_STALE_IMAGES_LAST_CHECKED));
218
	}
219
220
	/**
221
	 * @param null|string $userId
222
	 */
223 2
	public function setLastStaleImageChecked (int $lastCheck, ?string $userId = null): void {
224 2
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::STALE_IMAGES_LAST_CHECKED_KEY, strval($lastCheck));
225
	}
226
227
	/**
228
	 * @param null|string $userId
229
	 */
230
	public function getNeedRecreateClusters (?string $userId = null): bool {
231
		$needRecreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, self::DEFAULT_USER_RECREATE_CLUSTERS);
232
		return ($needRecreate === 'true');
233
	}
234
235
	/**
236
	 * @param null|string $userId
237
	 */
238 1
	public function setNeedRecreateClusters (bool $needRecreate, ?string $userId = null): void {
239 1
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::USER_RECREATE_CLUSTERS_KEY, $needRecreate ? "true" : "false");
240
	}
241
242
	// Private function used only on tests
243
	/**
244
	 * @param null|string $userId
245
	 */
246 1
	public function _getForceCreateClusters (?string $userId = null): bool {
247 1
		$forceCreate = $this->config->getUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, self::DEFAULT_FORCE_CREATE_CLUSTERS);
248 1
		return ($forceCreate === 'true');
249
	}
250
251
	// Private function used only on tests
252
	/**
253
	 * @param null|string $userId
254
	 */
255 1
	public function _setForceCreateClusters (bool $forceCreate, ?string $userId = null): void {
256 1
		$this->config->setUserValue($userId ?? $this->userId, Application::APP_NAME, self::FORCE_CREATE_CLUSTERS_KEY, $forceCreate ? "true" : "false");
257
	}
258
259
	/*
260
	 * Admin and process settings.
261
	 */
262 12
	public function getCurrentFaceModel(): int {
263 12
		return intval($this->config->getAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, strval(self::FALLBACK_CURRENT_MODEL)));
264
	}
265
266
	public function setCurrentFaceModel(int $model): void {
267
		$this->config->setAppValue(Application::APP_NAME, self::CURRENT_MODEL_KEY, strval($model));
268
	}
269
270 4
	public function getAnalysisImageArea(): int {
271 4
		return intval($this->config->getAppValue(Application::APP_NAME, self::ANALYSIS_IMAGE_AREA_KEY, strval(self::DEFAULT_ANALYSIS_IMAGE_AREA)));
272
	}
273
274
	public function setAssignedMemory(int $assignedMemory): void {
275
		$this->config->setAppValue(Application::APP_NAME, self::ASSIGNED_MEMORY_KEY, strval($assignedMemory));
276
	}
277
278
	public function setAnalysisImageArea(int $imageArea): void {
279
		$this->config->setAppValue(Application::APP_NAME, self::ANALYSIS_IMAGE_AREA_KEY, strval($imageArea));
280
	}
281
282 1
	public function getSensitivity(): float {
283 1
		return floatval($this->config->getAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, self::DEFAULT_SENSITIVITY));
284
	}
285
286
	public function setSensitivity($sensitivity): void {
287
		$this->config->setAppValue(Application::APP_NAME, self::SENSITIVITY_KEY, $sensitivity);
288
	}
289
290 1
	public function getMinimumConfidence(): float {
291 1
		return floatval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, self::DEFAULT_MINIMUM_CONFIDENCE));
292
	}
293
294
	public function setMinimumConfidence($confidence): void {
295
		$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_CONFIDENCE_KEY, $confidence);
296
	}
297
298
	public function getMinimumFacesInCluster(): int {
299
		return intval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_FACES_IN_CLUSTER_KEY, self::DEFAULT_MINIMUM_FACES_IN_CLUSTER));
300
	}
301
302
	public function setMinimumFacesInCluster($no_faces): void {
303
		$this->config->setAppValue(Application::APP_NAME, self::MINIMUM_FACES_IN_CLUSTER_KEY, $no_faces);
304
	}
305
306
	/**
307
	 * The next settings are advanced preferences that are not available in gui.
308
	 * See: https://github.com/matiasdelellis/facerecognition/wiki/Settings#hidden-settings
309
	 */
310 10
	public function getDefaultUserEnabled (): bool {
311 10
		$enabled = $this->config->getAppValue(Application::APP_NAME, self::DEFAULT_USER_ENABLED_KEY, self::DEFAULT_USER_ENABLED);
312 10
		return ($enabled === 'true');
313
	}
314
315
	public function getHandleSharedFiles(): bool {
316
		$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_SHARED_FILES_KEY, self::DEFAULT_HANDLE_SHARED_FILES);
317
		return ($handle === 'true');
318
	}
319
320
	public function getHandleExternalFiles(): bool {
321
		$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_EXTERNAL_FILES_KEY, self::DEFAULT_HANDLE_EXTERNAL_FILES);
322
		return ($handle === 'true');
323
	}
324
325
	public function getHandleGroupFiles(): bool {
326
		$handle = $this->config->getAppValue(Application::APP_NAME, self::HANDLE_GROUP_FILES_KEY, self::DEFAULT_HANDLE_GROUP_FILES);
327
		return ($handle === 'true');
328
	}
329
330 4
	public function getMinimumImageSize(): int {
331 4
		return intval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_IMAGE_SIZE_KEY, self::DEFAULT_MINIMUM_IMAGE_SIZE));
332
	}
333
334 1
	public function getMinimumFaceSize(): int {
335 1
		$minFaceSize = intval($this->config->getAppValue(Application::APP_NAME, self::MINIMUM_FACE_SIZE_KEY, self::DEFAULT_MINIMUM_FACE_SIZE));
336 1
		$minFaceSize = max(self::MINIMUM_MINIMUM_FACE_SIZE, $minFaceSize);
337 1
		$minFaceSize = min($minFaceSize, self::MAXIMUM_MINIMUM_FACE_SIZE);
338 1
		return intval($minFaceSize);
339
	}
340
341 4
	public function getMaximumImageArea(): int {
342 4
		return intval($this->config->getAppValue(Application::APP_NAME, self::MAXIMUM_IMAGE_AREA_KEY, self::DEFAULT_MAXIMUM_IMAGE_AREA));
343
	}
344
345
	public function getAssignedMemory(): int {
346
		return intval($this->config->getAppValue(Application::APP_NAME, self::ASSIGNED_MEMORY_KEY, strval(self::DEFAULT_ASSIGNED_MEMORY)));
347
	}
348
349
	public function getObfuscateFaces(): bool {
350
		$obfuscate = $this->config->getAppValue(Application::APP_NAME, self::OBFUSCATE_FACE_THUMBS_KEY, self::DEFAULT_OBFUSCATE_FACE_THUMBS);
351
		return ($obfuscate === 'true');
352
	}
353
354
	public function setObfuscateFaces(bool $obfuscate): void {
355
		$this->config->setAppValue(Application::APP_NAME, self::OBFUSCATE_FACE_THUMBS_KEY, $obfuscate ? 'true' : 'false');
356
	}
357
358
	/**
359
	 * System settings that must be configured according to the server configuration.
360
	 */
361 8
	public function isAllowedMimetype(string $mimetype): bool {
362 8
		if (!$this->cachedAllowedMimetypes) {
363 1
			$systemMimetypes = $this->config->getSystemValue(self::SYSTEM_ENABLED_MIMETYPES, $this->allowedMimetypes);
364 1
			$this->allowedMimetypes = array_merge($this->allowedMimetypes, $systemMimetypes);
365 1
			$this->allowedMimetypes = array_unique($this->allowedMimetypes);
366
367 1
			$this->cachedAllowedMimetypes = true;
368
		}
369
370 8
		return in_array($mimetype, $this->allowedMimetypes);
371
	}
372
373
	/**
374
	 * System settings that allow use an custom folder to install the models.
375
	 */
376 1
	public function getSystemModelPath(): ?string {
377 1
		return $this->config->getSystemValue(self::SYSTEM_MODEL_PATH, null);
378
	}
379
380
	/**
381
	 * External model url
382
	 */
383
	public function getExternalModelUrl(): ?string {
384
		return $this->config->getSystemValue(self::SYSTEM_EXTERNAL_MODEL_URL, null);
385
	}
386
387
	/**
388
	 * External model Api Key
389
	 */
390
	public function getExternalModelApiKey(): ?string {
391
		return $this->config->getSystemValue(self::SYSTEM_EXTERNAL_MODEL_API_KEY, null);
392
	}
393
394
}
395