Passed
Push — php8 ( 3e2147...190cd5 )
by Matias
10:58 queued 08:41
created

ClusterController::findUnassigned()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 31
c 1
b 0
f 1
nc 8
nop 0
dl 0
loc 46
ccs 0
cts 34
cp 0
crap 72
rs 8.1795
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018-2020 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 OCP\IRequest;
27
use OCP\Files\File;
28
29
use OCP\AppFramework\Http;
30
use OCP\AppFramework\Http\DataResponse;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Http\DataDisplayResponse;
33
use OCP\AppFramework\Controller;
34
35
use OCA\FaceRecognition\Db\Face;
36
use OCA\FaceRecognition\Db\FaceMapper;
37
38
use OCA\FaceRecognition\Db\Image;
39
use OCA\FaceRecognition\Db\ImageMapper;
40
41
use OCA\FaceRecognition\Db\Person;
42
use OCA\FaceRecognition\Db\PersonMapper;
43
44
use OCA\FaceRecognition\Service\SettingsService;
45
use OCA\FaceRecognition\Service\UrlService;
46
47
48
class ClusterController extends Controller {
49
50
	/** @var FaceMapper */
51
	private $faceMapper;
52
53
	/** @var ImageMapper */
54
	private $imageMapper;
55
56
	/** @var PersonMapper */
57
	private $personMapper;
58
59
	/** @var SettingsService */
60
	private $settingsService;
61
62
	/** @var UrlService */
63
	private $urlService;
64
65
	/** @var string */
66
	private $userId;
67
68
	public function __construct($AppName,
69
	                            IRequest        $request,
70
	                            FaceMapper      $faceMapper,
71
	                            ImageMapper     $imageMapper,
72
	                            PersonMapper    $personmapper,
73
	                            SettingsService $settingsService,
74
	                            UrlService      $urlService,
75
	                            $UserId)
76
	{
77
		parent::__construct($AppName, $request);
78
79
		$this->faceMapper      = $faceMapper;
80
		$this->imageMapper     = $imageMapper;
81
		$this->personMapper    = $personmapper;
82
		$this->settingsService = $settingsService;
83
		$this->urlService      = $urlService;
84
		$this->userId          = $UserId;
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 */
90
	public function find(int $id) {
91
		$person = $this->personMapper->find($this->userId, $id);
92
93
		$resp = [];
94
		$faces = [];
95
		$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $this->settingsService->getCurrentFaceModel());
96
		foreach ($personFaces as $personFace) {
97
			$image = $this->imageMapper->find($this->userId, $personFace->getImage());
98
			$fileId = $image->getFile();
99
			if ($fileId === null) continue;
100
101
			$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
102
			if ($fileUrl === null) continue;
103
104
			$face = [];
105
			$face['thumbUrl'] = $this->urlService->getThumbUrl($personFace->getId(), 50);
106
			$face['fileUrl'] = $fileUrl;
107
			$faces[] = $face;
108
		}
109
		$resp['name'] = $person->getName();
110
		$resp['id'] = $person->getId();
111
		$resp['faces'] = $faces;
112
113
		return new DataResponse($resp);
114
	}
115
116
	/**
117
	 * @NoAdminRequired
118
	 */
119
	public function findByName(string $personName) {
120
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
121
122
		$resp = array();
123
		$resp['enabled'] = $userEnabled;
124
		$resp['clusters'] = array();
125
126
		if (!$userEnabled)
127
			return new DataResponse($resp);
128
129
		$modelId = $this->settingsService->getCurrentFaceModel();
130
131
		$persons = $this->personMapper->findByName($this->userId, $modelId, $personName);
132
		foreach ($persons as $person) {
133
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId);
134
135
			$faces = [];
136
			foreach ($personFaces as $personFace) {
137
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
138
				$fileId = $image->getFile();
139
				if ($fileId === null) continue;
140
141
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
142
				if ($fileUrl === null) continue;
143
144
				$face = [];
145
				$face['thumbUrl'] = $this->urlService->getThumbUrl($personFace->getId(), 50);
146
				$face['fileUrl'] = $fileUrl;
147
				$faces[] = $face;
148
			}
149
150
			$cluster = [];
151
			$cluster['name'] = $person->getName();
152
			$cluster['count'] = count($personFaces);
153
			$cluster['id'] = $person->getId();
154
			$cluster['faces'] = $faces;
155
			$resp['clusters'][] = $cluster;
156
		}
157
158
		return new DataResponse($resp);
159
	}
160
161
	/**
162
	 * @NoAdminRequired
163
	 *
164
	 */
165
	public function findUnassigned() {
166
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
167
168
		$resp = array();
169
		$resp['enabled'] = $userEnabled;
170
		$resp['clusters'] = array();
171
172
		if (!$userEnabled)
173
			return new DataResponse($resp);
174
175
		$modelId = $this->settingsService->getCurrentFaceModel();
176
177
		$persons = $this->personMapper->findUnassigned($this->userId, $modelId);
178
		foreach ($persons as $person) {
179
			$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $modelId, 40);
180
			if (count($personFaces) === 1)
181
				continue;
182
183
			$faces = [];
184
			foreach ($personFaces as $personFace) {
185
				$image = $this->imageMapper->find($this->userId, $personFace->getImage());
186
187
				$fileId = $image->getFile();
188
				if ($fileId === null) continue;
189
190
				$fileUrl = $this->urlService->getRedirectToFileUrl($fileId);
191
				if ($fileUrl === null) continue;
192
193
				$thumbUrl = $this->urlService->getThumbUrl($personFace->getId(), 50);
194
				if ($thumbUrl === null) continue;
195
196
				$face = [];
197
				$face['thumbUrl'] = $thumbUrl;
198
				$face['fileUrl'] = $fileUrl;
199
200
				$faces[] = $face;
201
			}
202
203
			$cluster = [];
204
			$cluster['count'] = count($personFaces);
205
			$cluster['id'] = $person->getId();
206
			$cluster['faces'] = $faces;
207
			$resp['clusters'][] = $cluster;
208
		}
209
210
		return new DataResponse($resp);
211
	}
212
213
	/**
214
	 * @NoAdminRequired
215
	 *
216
	 * @param int $id
217
	 * @param string $name
218
	 */
219
	public function updateName($id, $name) {
220
		$person = $this->personMapper->find ($this->userId, $id);
221
		$person->setName($name);
222
		$this->personMapper->update($person);
223
224
		$newPerson = $this->personMapper->find($this->userId, $id);
225
		return new DataResponse($newPerson);
226
	}
227
228
}
229