Passed
Push — fix-empty-enabled ( e15728...1aae3b )
by Matias
04:12
created

FileController::getThumbUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
namespace OCA\FaceRecognition\Controller;
3
4
use OCP\IRequest;
5
use OCP\AppFramework\Http\JSONResponse;
6
use OCP\AppFramework\Controller;
7
8
use OCP\IURLGenerator;
9
10
use OCA\FaceRecognition\Db\Image;
11
use OCA\FaceRecognition\Db\ImageMapper;
12
13
use OCA\FaceRecognition\Db\Face;
14
use OCA\FaceRecognition\Db\FaceMapper;
15
16
use OCA\FaceRecognition\Db\Person;
17
use OCA\FaceRecognition\Db\PersonMapper;
18
19
use OCA\FaceRecognition\Service\FileService;
20
21
use OCA\FaceRecognition\Service\SettingsService;
22
23
class FileController extends Controller {
24
25
	/** @var IURLGenerator */
26
	private $urlGenerator;
27
28
	/** @var ImageMapper */
29
	private $imageMapper;
30
31
	/** @var PersonMapper */
32
	private $personMapper;
33
34
	/** @var FaceMapper */
35
	private $faceMapper;
36
37
	/** @var FileService */
38
	private $fileService;
39
40
	/** @var SettingsService */
41
	private $settingsService;
42
43
	/** @var string */
44
	private $userId;
45
46
	public function __construct($AppName,
47
	                            IURLGenerator   $urlGenerator,
48
	                            IRequest        $request,
49
	                            ImageMapper     $imageMapper,
50
	                            PersonMapper    $personMapper,
51
	                            FaceMapper      $faceMapper,
52
	                            FileService     $fileService,
53
	                            SettingsService $settingsService,
54
	                            $UserId)
55
	{
56
		parent::__construct($AppName, $request);
57
58
		$this->urlGenerator    = $urlGenerator;
59
		$this->imageMapper     = $imageMapper;
60
		$this->personMapper    = $personMapper;
61
		$this->faceMapper      = $faceMapper;
62
		$this->fileService     = $fileService;
63
		$this->settingsService = $settingsService;
64
		$this->userId          = $UserId;
65
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 *
70
	 * Get persons on file.
71
	 *
72
	 * @param string $fullpath of the file to get persons
73
	 * @return JSONResponse
74
	 */
75
	public function getPersonsFromPath(string $fullpath) {
76
77
		$resp = array();
78
		if (!$this->settingsService->getUserEnabled($this->userId)) {
79
			$resp['enabled'] = false;
80
			return new JSONResponse($resp);
81
		}
82
83
		$file = $this->fileService->getFileByPath($fullpath);
84
85
		$fileId = $file->getId();
86
		$image = $this->imageMapper->findFromFile($this->userId, $fileId);
87
88
		$resp['enabled'] = true;
89
		$resp['is_allowed'] = $this->fileService->isAllowedNode($file);
90
		$resp['parent_detection'] = !$this->fileService->isUnderNoDetection($file);
91
		$resp['image_id'] = $image ? $image->getId() : 0;
92
		$resp['is_processed'] = $image ? $image->getIsProcessed() : 0;
93
		$resp['error'] = $image ? $image->getError() : null;
94
		$resp['persons'] = array();
95
96
		$persons = $this->personMapper->findFromFile($this->userId, $fileId);
97
		foreach ($persons as $person) {
98
			$face = $this->faceMapper->getPersonOnFile($this->userId, $person->getId(), $fileId, $this->settingsService->getCurrentFaceModel());
99
			if (!count($face))
100
				continue;
101
102
			$facePerson = array();
103
			$facePerson['name'] = $person->getName();
104
			$facePerson['person_id'] = $person->getId();
105
			$facePerson['thumb_url'] = $this->getThumbUrl($face[0]->getId());
106
			$facePerson['face_left'] = $face[0]->getLeft();
107
			$facePerson['face_right'] = $face[0]->getRight();
108
			$facePerson['face_top'] = $face[0]->getTop();
109
			$facePerson['face_bottom'] = $face[0]->getBottom();
110
111
			$resp['persons'][] = $facePerson;
112
		}
113
114
		return new JSONResponse($resp);
115
	}
116
117
	/**
118
	 * @NoAdminRequired
119
	 *
120
	 * Get if folder if folder is enabled
121
	 *
122
	 * @param string $fullpath of the folder
123
	 * @return JSONResponse
124
	 */
125
	public function getFolderOptions(string $fullpath) {
126
		$resp = array();
127
128
		if (!$this->settingsService->getUserEnabled($this->userId)) {
129
			$resp['enabled'] = false;
130
			return new JSONResponse($resp);
131
		}
132
133
		$folder = $this->fileService->getFileByPath($fullpath);
134
135
		$resp['enabled'] = 'true';
136
		$resp['is_allowed'] = $this->fileService->isAllowedNode($folder);
137
		$resp['parent_detection'] = !$this->fileService->isUnderNoDetection($folder);
138
		$resp['descendant_detection'] = $this->fileService->getDescendantDetection($folder);
139
140
		return new JSONResponse($resp);
141
	}
142
143
	/**
144
	 * @NoAdminRequired
145
	 *
146
	 * Apply option to folder to enabled or disable it.
147
	 *
148
	 * @param string $fullpath of the folder.
149
	 * @param bool $detection
150
	 * @return JSONResponse
151
	 */
152
	public function setFolderOptions(string $fullpath, bool $detection) {
153
		$folder = $this->fileService->getFileByPath($fullpath);
154
		$this->fileService->setDescendantDetection($folder, $detection);
155
156
		return $this->getFolderOptions($fullpath);
157
	}
158
159
	/**
160
	 * Url to thumb face
161
	 *
162
	 * @param string $faceId face id to show
163
	 */
164
	private function getThumbUrl($faceId) {
165
		$params = [];
166
		$params['id'] = $faceId;
167
		$params['size'] = 32;
168
		return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params);
169
	}
170
171
}
172