1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2019, Matias De lellis <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Matias De lellis <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\FaceRecognition\Controller; |
25
|
|
|
|
26
|
|
|
use OCA\FaceRecognition\BackgroundJob\Tasks\AddMissingImagesTask; |
27
|
|
|
use OCA\FaceRecognition\FaceManagementService; |
28
|
|
|
|
29
|
|
|
use OCA\FaceRecognition\Helper\MemoryLimits; |
30
|
|
|
|
31
|
|
|
use OCP\AppFramework\Controller; |
32
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
33
|
|
|
use OCP\IRequest; |
34
|
|
|
use OCP\IConfig; |
35
|
|
|
use OCP\IUserManager; |
36
|
|
|
use OCP\IUser; |
37
|
|
|
use OCP\IL10N; |
38
|
|
|
|
39
|
|
|
class SettingController extends Controller { |
40
|
|
|
|
41
|
|
|
/** @var IConfig */ |
42
|
|
|
private $config; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** @var \OCP\IL10N */ |
46
|
|
|
protected $l10n; |
47
|
|
|
|
48
|
|
|
/** @var IUserManager */ |
49
|
|
|
private $userManager; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
private $userId; |
53
|
|
|
|
54
|
|
|
const STATE_OK = 0; |
55
|
|
|
const STATE_FALSE = 1; |
56
|
|
|
const STATE_SUCCESS = 2; |
57
|
|
|
const STATE_ERROR = 3; |
58
|
|
|
|
59
|
|
|
public function __construct ($appName, |
60
|
|
|
IRequest $request, |
61
|
|
|
IConfig $config, |
62
|
|
|
IL10N $l10n, |
63
|
|
|
IUserManager $userManager, |
64
|
|
|
$userId) |
65
|
|
|
{ |
66
|
|
|
parent::__construct($appName, $request); |
67
|
|
|
$this->appName = $appName; |
68
|
|
|
$this->config = $config; |
69
|
|
|
$this->l10n = $l10n; |
70
|
|
|
$this->userManager = $userManager; |
71
|
|
|
$this->userId = $userId; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @NoAdminRequired |
76
|
|
|
* @param $type |
77
|
|
|
* @param $value |
78
|
|
|
* @return JSONResponse |
79
|
|
|
*/ |
80
|
|
|
public function setUserValue($type, $value) { |
81
|
|
|
// Apply the change of settings |
82
|
|
|
$this->config->setUserValue($this->userId, $this->appName, $type, $value); |
83
|
|
|
|
84
|
|
|
// Handles special cases when have to do something else according to the change |
85
|
|
|
switch ($type) { |
86
|
|
|
case 'enabled': |
87
|
|
|
if ($value === 'true') { |
88
|
|
|
$this->config->setUserValue($this->userId, $this->appName, AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false'); |
89
|
|
|
} |
90
|
|
|
break; |
91
|
|
|
default: |
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Response |
96
|
|
|
$result = [ |
97
|
|
|
'status' => self::STATE_SUCCESS, |
98
|
|
|
'value' => $value |
99
|
|
|
]; |
100
|
|
|
return new JSONResponse($result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @NoAdminRequired |
105
|
|
|
* @param $type |
106
|
|
|
* @return JSONResponse |
107
|
|
|
*/ |
108
|
|
|
public function getUserValue($type) { |
109
|
|
|
$value = $this->config->getUserValue($this->userId, $this->appName, $type); |
110
|
|
|
if ($value !== '') { |
111
|
|
|
$result = [ |
112
|
|
|
'status' => self::STATE_OK, |
113
|
|
|
'value' => $value |
114
|
|
|
]; |
115
|
|
|
} else { |
116
|
|
|
$result = [ |
117
|
|
|
'status' => self::STATE_FALSE, |
118
|
|
|
'value' =>'nodata' |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
return new JSONResponse($result); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param $type |
126
|
|
|
* @param $value |
127
|
|
|
* @return JSONResponse |
128
|
|
|
*/ |
129
|
|
|
public function setAppValue($type, $value) { |
130
|
|
|
$status = self::STATE_SUCCESS; |
131
|
|
|
$message = ""; |
132
|
|
|
switch ($type) { |
133
|
|
|
case 'sensitivity': |
134
|
|
|
$this->config->setAppValue('facerecognition', $type, $value); |
135
|
|
|
$this->userManager->callForSeenUsers(function(IUser $user) { |
136
|
|
|
$this->config->setUserValue($user->getUID(), 'facerecognition', 'recreate-clusters', 'true'); |
137
|
|
|
}); |
138
|
|
|
break; |
139
|
|
|
case 'memory-limits': |
140
|
|
|
if (is_numeric ($value)) { |
141
|
|
|
// Apply prundent limits. |
142
|
|
|
if ($value < 1 * 1024 * 1024 * 1024) { |
143
|
|
|
$value = 1 * 1024 * 1024 * 1024; |
144
|
|
|
$message = $this->l10n->t("The recommended analysis needs at least 1 GB of RAM."); |
145
|
|
|
$status = self::STATE_ERROR; |
146
|
|
|
} else if ($value > 4 * 1024 * 1024 * 1024) { |
147
|
|
|
$value = 4 * 1024 * 1024 * 1024; |
148
|
|
|
$message = $this->l10n->t("It is not recommended to use more than 4GB of RAM."); |
149
|
|
|
$status = self::STATE_ERROR; |
150
|
|
|
} |
151
|
|
|
// Valid according to RAM of php.ini setting. |
152
|
|
|
$memory = MemoryLimits::getAvailableMemory(); |
153
|
|
|
if ($value > $memory) { |
154
|
|
|
$value = $memory; |
155
|
|
|
$message = $this->l10n->t("According to your system you can not use more than %s GB of RAM", ($value / 1024 / 1024 / 1024)); |
156
|
|
|
$status = self::STATE_ERROR; |
157
|
|
|
} |
158
|
|
|
// If any validation error saves the value |
159
|
|
|
if ($status !== self::STATE_ERROR) { |
160
|
|
|
$message = $this->l10n->t("The changes were saved. It will be taken into account in the next analysis."); |
161
|
|
|
$this->config->setAppValue('facerecognition', $type, $value); |
162
|
|
|
} |
163
|
|
|
} else { |
164
|
|
|
$status = self::STATE_ERROR; |
165
|
|
|
$message = $this->l10n->t("The format seems to be incorrect."); |
166
|
|
|
$value = '-1'; |
167
|
|
|
} |
168
|
|
|
break; |
169
|
|
|
case 'show-not-grouped': |
170
|
|
|
$this->config->setAppValue('facerecognition', $type, $value); |
171
|
|
|
break; |
172
|
|
|
default: |
173
|
|
|
break; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Response |
177
|
|
|
$result = [ |
178
|
|
|
'status' => $status, |
179
|
|
|
'message' => $message, |
180
|
|
|
'value' => $value |
181
|
|
|
]; |
182
|
|
|
return new JSONResponse($result); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $type |
187
|
|
|
* @return JSONResponse |
188
|
|
|
*/ |
189
|
|
|
public function getAppValue($type) { |
190
|
|
|
$value = 'nodata'; |
191
|
|
|
$status = self::STATE_OK; |
192
|
|
|
switch ($type) { |
193
|
|
|
case 'sensitivity': |
194
|
|
|
$value = $this->config->getAppValue('facerecognition', $type, '0.5'); |
195
|
|
|
break; |
196
|
|
|
case 'memory-limits': |
197
|
|
|
$value = $this->config->getAppValue('facerecognition', $type, '-1'); |
198
|
|
|
// If it was not configured, returns the default |
199
|
|
|
// values used by the background task as a reference. |
200
|
|
|
if ($value === '-1') { |
201
|
|
|
$memory = MemoryLimits::getAvailableMemory(); |
202
|
|
|
if ($memory > 4 * 1024 * 1024 * 1024) |
203
|
|
|
$memory = 4 * 1024 * 1024 * 1024; |
204
|
|
|
$value = $memory; |
205
|
|
|
$status = self::STATE_FALSE; |
206
|
|
|
} |
207
|
|
|
break; |
208
|
|
|
case 'show-not-grouped': |
209
|
|
|
$value = $this->config->getAppValue('facerecognition', $type, 'false'); |
210
|
|
|
break; |
211
|
|
|
default: |
212
|
|
|
break; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Response |
216
|
|
|
$result = [ |
217
|
|
|
'status' => $status, |
218
|
|
|
'value' => $value |
219
|
|
|
]; |
220
|
|
|
|
221
|
|
|
return new JSONResponse($result); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|