Passed
Push — master ( 8fc0ca...991a64 )
by Matias
08:13 queued 06:03
created

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