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 OCA\FaceRecognition\Db\Image; |
||
9 | use OCA\FaceRecognition\Db\ImageMapper; |
||
10 | |||
11 | use OCA\FaceRecognition\Db\Face; |
||
12 | use OCA\FaceRecognition\Db\FaceMapper; |
||
13 | |||
14 | use OCA\FaceRecognition\Db\Person; |
||
15 | use OCA\FaceRecognition\Db\PersonMapper; |
||
16 | |||
17 | use OCA\FaceRecognition\Service\FileService; |
||
18 | |||
19 | use OCA\FaceRecognition\Service\SettingsService; |
||
20 | |||
21 | use OCA\FaceRecognition\Service\UrlService; |
||
22 | |||
23 | class FileController extends Controller { |
||
24 | |||
25 | /** @var ImageMapper */ |
||
26 | private $imageMapper; |
||
27 | |||
28 | /** @var PersonMapper */ |
||
29 | private $personMapper; |
||
30 | |||
31 | /** @var FaceMapper */ |
||
32 | private $faceMapper; |
||
33 | |||
34 | /** @var FileService */ |
||
35 | private $fileService; |
||
36 | |||
37 | /** @var SettingsService */ |
||
38 | private $settingsService; |
||
39 | |||
40 | /** @var UrlService */ |
||
41 | private $urlService; |
||
42 | |||
43 | /** @var string */ |
||
44 | private $userId; |
||
45 | |||
46 | public function __construct($AppName, |
||
47 | IRequest $request, |
||
48 | ImageMapper $imageMapper, |
||
49 | PersonMapper $personMapper, |
||
50 | FaceMapper $faceMapper, |
||
51 | FileService $fileService, |
||
52 | SettingsService $settingsService, |
||
53 | UrlService $urlService, |
||
54 | $UserId) |
||
55 | { |
||
56 | parent::__construct($AppName, $request); |
||
57 | |||
58 | $this->imageMapper = $imageMapper; |
||
59 | $this->personMapper = $personMapper; |
||
60 | $this->faceMapper = $faceMapper; |
||
61 | $this->fileService = $fileService; |
||
62 | $this->settingsService = $settingsService; |
||
63 | $this->urlService = $urlService; |
||
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 | $modelId = $this->settingsService->getCurrentFaceModel(); |
||
87 | |||
88 | $image = $this->imageMapper->findFromFile($this->userId, $modelId, $fileId); |
||
89 | |||
90 | $resp['enabled'] = true; |
||
91 | $resp['is_allowed'] = $this->fileService->isAllowedNode($file); |
||
92 | $resp['parent_detection'] = !$this->fileService->isUnderNoDetection($file); |
||
93 | $resp['image_id'] = $image ? $image->getId() : 0; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
94 | $resp['is_processed'] = $image ? $image->getIsProcessed() : false; |
||
0 ignored issues
–
show
|
|||
95 | $resp['error'] = $image ? $image->getError() : null; |
||
0 ignored issues
–
show
|
|||
96 | $resp['persons'] = array(); |
||
97 | |||
98 | $faces = $this->faceMapper->findFromFile($this->userId, $modelId, $fileId); |
||
99 | foreach ($faces as $face) { |
||
100 | // When there are faces but still dont have person, the process is not completed yet. |
||
101 | // See issue https://github.com/matiasdelellis/facerecognition/issues/255 |
||
102 | if (!$face->getPerson()) { |
||
103 | $resp['is_processed'] = false; |
||
104 | break; |
||
105 | } |
||
106 | |||
107 | $person = $this->personMapper->find($this->userId, $face->getPerson()); |
||
108 | $personName = $person->getName(); |
||
109 | |||
110 | $facePerson = array(); |
||
111 | $facePerson['name'] = $personName; |
||
112 | $facePerson['person_id'] = $person->getId(); |
||
113 | $facePerson['person_visible'] = $person->getIsVisible(); |
||
114 | $facePerson['face_id'] = $face->getId(); |
||
115 | $facePerson['thumb_url'] = $this->urlService->getThumbUrl($face->getId(), 50); |
||
116 | $facePerson['photos_url'] = $personName ? $this->urlService->getRedirectToPersonUrl($personName) : null; |
||
117 | |||
118 | $resp['persons'][] = $facePerson; |
||
119 | } |
||
120 | |||
121 | return new JSONResponse($resp); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @NoAdminRequired |
||
126 | * |
||
127 | * Get if folder if folder is enabled |
||
128 | * |
||
129 | * @param string $fullpath of the folder |
||
130 | * @return JSONResponse |
||
131 | */ |
||
132 | public function getFolderOptions(string $fullpath) { |
||
133 | $resp = array(); |
||
134 | |||
135 | if (!$this->settingsService->getUserEnabled($this->userId)) { |
||
136 | $resp['enabled'] = false; |
||
137 | return new JSONResponse($resp); |
||
138 | } |
||
139 | |||
140 | $folder = $this->fileService->getFileByPath($fullpath); |
||
141 | |||
142 | $resp['enabled'] = 'true'; |
||
143 | $resp['is_allowed'] = $this->fileService->isAllowedNode($folder); |
||
144 | $resp['parent_detection'] = !$this->fileService->isUnderNoDetection($folder); |
||
145 | $resp['descendant_detection'] = $this->fileService->getDescendantDetection($folder); |
||
146 | |||
147 | return new JSONResponse($resp); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @NoAdminRequired |
||
152 | * |
||
153 | * Apply option to folder to enabled or disable it. |
||
154 | * |
||
155 | * @param string $fullpath of the folder. |
||
156 | * @param bool $detection |
||
157 | * @return JSONResponse |
||
158 | */ |
||
159 | public function setFolderOptions(string $fullpath, bool $detection) { |
||
160 | $folder = $this->fileService->getFileByPath($fullpath); |
||
161 | $this->fileService->setDescendantDetection($folder, $detection); |
||
162 | |||
163 | return $this->getFolderOptions($fullpath); |
||
164 | } |
||
165 | |||
166 | } |
||
167 |