Passed
Push — dependabot/npm_and_yarn/ellipt... ( eb0fee )
by
unknown
24:49
created

SettingsService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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