Completed
Push — master ( 003f81...fa01c9 )
by Matias
32s queued 15s
created

OcsApiController::getPersonsV1()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 21
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 32
ccs 0
cts 22
cp 0
crap 30
rs 9.2728
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 Ming Tsang <[email protected]>
4
 *
5
 * @author Ming Tsang <[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\OCSController;
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
class OcsApiController extends OCSController {
48
49
	/** @var FaceMapper */
50
	private $faceMapper;
51
52
	/** @var ImageMapper */
53
	private $imageMapper;
54
55
	/** @var PersonMapper */
56
	private $personMapper;
57
58
	/** @var SettingsService */
59
	private $settingsService;
60
61
	/** @var UrlService */
62
	private $urlService;
63
64
	/** @var string */
65
	private $userId;
66
67
	public function __construct(
68
		$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
	 * API V1
89
	 */
90
91
	/**
92
	 * Get all named persons
93
	 *
94
	 * - Endpoint: /persons
95
	 * - Method: GET
96
	 * - Response: Array of persons
97
	 * 		- Person:
98
	 * 			- name: Name of the person
99
	 * 			- thumbFaceId: Face representing this person
100
	 * 			- count: Number of images associated to this person
101
	 *
102
	 * @NoAdminRequired
103
	 *
104
	 * @return DataResponse
105
	 */
106
	public function getPersonsV1(): DataResponse {
107
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
108
109
		$resp = array();
110
111
		if (!$userEnabled)
112
			return new DataResponse($resp);
113
114
		$modelId = $this->settingsService->getCurrentFaceModel();
115
116
		$personsNames = $this->personMapper->findDistinctNames($this->userId, $modelId);
117
		foreach ($personsNames as $personNamed) {
118
			$facesCount = 0;
119
			$thumbFaceId = null;
120
			$persons = $this->personMapper->findByName($this->userId, $modelId, $personNamed->getName());
121
			foreach ($persons as $person) {
122
				$personFaces = $this->faceMapper->findFromCluster($this->userId, $person->getId(), $modelId);
123
				if (is_null($thumbFaceId)) {
124
					$thumbFaceId = $personFaces[0]->getId();
125
				}
126
				$facesCount += count($personFaces);
127
			}
128
129
			$respPerson = [];
130
			$respPerson['name'] = $personNamed->getName();
131
			$respPerson['thumbFaceId'] = $thumbFaceId;
132
			$respPerson['count'] = $facesCount;
133
134
			$resp[] = $respPerson;
135
		}
136
137
		return new DataResponse($resp);
138
	}
139
140
	/**
141
	 * Get all faces associated to a person
142
	 *
143
	 * - Endpoint: /person/<name>/faces
144
	 * - Method: GET
145
	 * - URL Arguments: name - (string) name of the person
146
	 * - Response: Array of faces
147
	 * 		- Face:
148
	 * 			- id: Face ID
149
	 * 			- fileId: The file where this face was found
150
	 *
151
	 * @NoAdminRequired
152
	 *
153
	 * @return DataResponse
154
	 */
155
	public function getFacesByPerson(string $name): DataResponse {
156
		$userEnabled = $this->settingsService->getUserEnabled($this->userId);
157
158
		$resp = array();
159
160
		if (!$userEnabled)
161
			return new DataResponse($resp);
162
163
		$modelId = $this->settingsService->getCurrentFaceModel();
164
165
		$clusters = $this->personMapper->findByName($this->userId, $modelId, $name);
166
		foreach ($clusters as $cluster) {
167
			$faces = $this->faceMapper->findFromCluster($this->userId, $cluster->getId(), $modelId);
168
			foreach ($faces as $face) {
169
				$image = $this->imageMapper->find($this->userId, $face->getImage());
170
171
				$respFace = [];
172
				$respFace['id'] = $face->getId();
173
				$respFace['fileId'] = $image->getFile();
174
175
				$resp[] = $respFace;
176
			}
177
		}
178
179
		return new DataResponse($resp);
180
	}
181
182
}
183