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
|
|
|
|
38
|
|
|
class SettingController extends Controller { |
39
|
|
|
|
40
|
|
|
/** @var IConfig */ |
41
|
|
|
private $config; |
42
|
|
|
|
43
|
|
|
/** @var IUserManager */ |
44
|
|
|
private $userManager; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
private $userId; |
48
|
|
|
|
49
|
|
|
const STATE_OK = 0; |
50
|
|
|
const STATE_FALSE = 1; |
51
|
|
|
const STATE_SUCCESS = 2; |
52
|
|
|
const STATE_ERROR = 3; |
53
|
|
|
|
54
|
|
|
public function __construct ($appName, |
55
|
|
|
IRequest $request, |
56
|
|
|
IConfig $config, |
57
|
|
|
IUserManager $userManager, |
58
|
|
|
$userId) |
59
|
|
|
{ |
60
|
|
|
parent::__construct($appName, $request); |
61
|
|
|
$this->appName = $appName; |
62
|
|
|
$this->config = $config; |
63
|
|
|
$this->userManager = $userManager; |
64
|
|
|
$this->userId = $userId; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @NoAdminRequired |
69
|
|
|
* @param $type |
70
|
|
|
* @param $value |
71
|
|
|
* @return JSONResponse |
72
|
|
|
*/ |
73
|
|
|
public function setUserValue($type, $value) { |
74
|
|
|
// Apply the change of settings |
75
|
|
|
$this->config->setUserValue($this->userId, $this->appName, $type, $value); |
76
|
|
|
|
77
|
|
|
// Handles special cases when have to do something else according to the change |
78
|
|
|
switch ($type) { |
79
|
|
|
case 'enabled': |
80
|
|
|
if ($value === 'true') { |
81
|
|
|
$this->config->setUserValue($this->userId, $this->appName, AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false'); |
82
|
|
|
} |
83
|
|
|
break; |
84
|
|
|
default: |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Response |
89
|
|
|
$result = [ |
90
|
|
|
'status' => self::STATE_SUCCESS, |
91
|
|
|
'value' => $value |
92
|
|
|
]; |
93
|
|
|
return new JSONResponse($result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @NoAdminRequired |
98
|
|
|
* @param $type |
99
|
|
|
* @return JSONResponse |
100
|
|
|
*/ |
101
|
|
|
public function getUserValue($type) { |
102
|
|
|
$value = $this->config->getUserValue($this->userId, $this->appName, $type); |
103
|
|
|
if ($value !== '') { |
104
|
|
|
$result = [ |
105
|
|
|
'status' => self::STATE_OK, |
106
|
|
|
'value' => $value |
107
|
|
|
]; |
108
|
|
|
} else { |
109
|
|
|
$result = [ |
110
|
|
|
'status' => self::STATE_FALSE, |
111
|
|
|
'value' =>'nodata' |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
return new JSONResponse($result); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $type |
119
|
|
|
* @param $value |
120
|
|
|
* @return JSONResponse |
121
|
|
|
*/ |
122
|
|
|
public function setAppValue($type, $value) { |
123
|
|
|
$status = self::STATE_SUCCESS; |
124
|
|
|
switch ($type) { |
125
|
|
|
case 'sensitivity': |
126
|
|
|
$this->config->setAppValue('facerecognition', $type, $value); |
127
|
|
|
$this->userManager->callForSeenUsers(function(IUser $user) { |
128
|
|
|
$this->config->setUserValue($user->getUID(), 'facerecognition', 'recreate-clusters', 'true'); |
129
|
|
|
}); |
130
|
|
|
break; |
131
|
|
|
case 'memory-limits': |
132
|
|
|
if (is_numeric ($value)) { |
133
|
|
|
// Apply prundent limits. |
134
|
|
|
if ($value < 1 * 1024 * 1024) { |
135
|
|
|
$value = 1 * 1024 * 1024; |
136
|
|
|
$status = self::STATE_ERROR; |
137
|
|
|
} |
138
|
|
|
else if ($value > 4 * 1024 * 1024) { |
139
|
|
|
$value = 4 * 1024 * 1024; |
140
|
|
|
$status = self::STATE_ERROR; |
141
|
|
|
} |
142
|
|
|
// Valid according to RAM of php.ini setting. |
143
|
|
|
$memory = MemoryLimits::getAvailableMemory(); |
144
|
|
|
if ($value > $memory) { |
145
|
|
|
$value = $memory; |
146
|
|
|
$status = self::STATE_ERROR; |
147
|
|
|
} |
148
|
|
|
// If any validation error saves the value |
149
|
|
|
if ($status !== self::STATE_ERROR) |
150
|
|
|
$this->config->setAppValue('facerecognition', $type, $value); |
151
|
|
|
} else { |
152
|
|
|
$status = self::STATE_ERROR; |
153
|
|
|
$value = '-1'; |
154
|
|
|
} |
155
|
|
|
break; |
156
|
|
|
default: |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Response |
161
|
|
|
$result = [ |
162
|
|
|
'status' => $status, |
163
|
|
|
'value' => $value |
164
|
|
|
]; |
165
|
|
|
return new JSONResponse($result); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $type |
170
|
|
|
* @return JSONResponse |
171
|
|
|
*/ |
172
|
|
|
public function getAppValue($type) { |
173
|
|
|
$value = 'nodata'; |
174
|
|
|
$status = self::STATE_OK; |
175
|
|
|
switch ($type) { |
176
|
|
|
case 'sensitivity': |
177
|
|
|
$value = $this->config->getAppValue('facerecognition', $type, '0.5'); |
178
|
|
|
break; |
179
|
|
|
case 'memory-limits': |
180
|
|
|
$value = $this->config->getAppValue('facerecognition', $type, '-1'); |
181
|
|
|
// If it was not configured, returns the default |
182
|
|
|
// values used by the background task as a reference. |
183
|
|
|
if ($value === '-1') { |
184
|
|
|
$memory = MemoryLimits::getAvailableMemory(); |
185
|
|
|
if ($memory > 4 * 1024 * 1024) |
186
|
|
|
$memory = 4 * 1024 * 1024; |
187
|
|
|
$value = $memory; |
188
|
|
|
$status = self::STATE_FALSE; |
189
|
|
|
} |
190
|
|
|
break; |
191
|
|
|
default: |
192
|
|
|
break; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Response |
196
|
|
|
$result = [ |
197
|
|
|
'status' => $status, |
198
|
|
|
'value' => $value |
199
|
|
|
]; |
200
|
|
|
|
201
|
|
|
return new JSONResponse($result); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|