Passed
Push — dependabot/npm_and_yarn/minimi... ( f33d6d )
by
unknown
11:45
created

SettingsService::getMinimumImageSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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