|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\FaceRecognition\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use OCP\IRequest; |
|
5
|
|
|
use OCP\Files\IRootFolder; |
|
6
|
|
|
use OCP\AppFramework\Http; |
|
7
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
8
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
9
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
10
|
|
|
use OCP\AppFramework\Controller; |
|
11
|
|
|
|
|
12
|
|
|
use OCA\FaceRecognition\Db\Face; |
|
13
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
|
14
|
|
|
|
|
15
|
|
|
use OCA\FaceRecognition\Db\FaceNew; |
|
16
|
|
|
use OCA\FaceRecognition\Db\FaceNewMapper; |
|
17
|
|
|
|
|
18
|
|
|
use OCA\FaceRecognition\Db\Image; |
|
19
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
|
20
|
|
|
|
|
21
|
|
|
class FaceController extends Controller { |
|
22
|
|
|
|
|
23
|
|
|
private $rootFolder; |
|
24
|
|
|
private $faceMapper; |
|
25
|
|
|
private $faceNewMapper; |
|
26
|
|
|
private $imageMapper; |
|
27
|
|
|
private $userId; |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
public function __construct($AppName, IRequest $request, IRootFolder $rootFolder, FaceMapper $facemapper, FaceNewMapper $facenewmapper, ImageMapper $imagemapper, $UserId) { |
|
|
|
|
|
|
30
|
|
|
parent::__construct($AppName, $request); |
|
31
|
|
|
$this->rootFolder = $rootFolder; |
|
32
|
|
|
$this->faceMapper = $facemapper; |
|
33
|
|
|
$this->faceNewMapper = $facenewmapper; |
|
34
|
|
|
$this->imageMapper = $imagemapper; |
|
35
|
|
|
$this->userId = $UserId; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @NoAdminRequired |
|
40
|
|
|
*/ |
|
41
|
|
|
public function index() { |
|
42
|
|
|
$faces = $this->faceMapper->findAll($this->userId); |
|
43
|
|
|
return new DataResponse($faces); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @NoAdminRequired |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $id |
|
50
|
|
|
*/ |
|
51
|
|
|
public function find ($id) { |
|
52
|
|
|
$face = $this->faceMapper->find($this->userId, $id); |
|
53
|
|
|
return new DataResponse($face); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @NoAdminRequired |
|
58
|
|
|
* @NoCSRFRequired |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getThumb ($id) { |
|
61
|
|
|
\OC_Util::tearDownFS(); |
|
62
|
|
|
\OC_Util::setupFS($this->userId); |
|
63
|
|
|
|
|
64
|
|
|
$face = $this->faceMapper->find($id, $this->userId); |
|
65
|
|
|
|
|
66
|
|
|
$fileId = $face->getFile(); |
|
67
|
|
|
|
|
68
|
|
|
return $this->getFaceThumb ($fileId, $face); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @NoAdminRequired |
|
73
|
|
|
* @NoCSRFRequired |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getThumbV2 ($id) { |
|
76
|
|
|
\OC_Util::tearDownFS(); |
|
77
|
|
|
\OC_Util::setupFS($this->userId); |
|
78
|
|
|
|
|
79
|
|
|
$face = $this->faceNewMapper->find($id); |
|
80
|
|
|
$image = $this->imageMapper->find($this->userId, $face->getImage()); |
|
81
|
|
|
$fileId = $image->getFile(); |
|
82
|
|
|
return $this->getFaceThumb ($fileId, $face); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function getFaceThumb ($fileId, $face) { |
|
86
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
87
|
|
|
$nodes = $userFolder->getById($fileId); |
|
88
|
|
|
$file = $nodes[0]; |
|
89
|
|
|
|
|
90
|
|
|
$ownerView = new \OC\Files\View('/'. $this->userId . '/files'); |
|
91
|
|
|
$path = $userFolder->getRelativePath($file->getPath()); |
|
92
|
|
|
|
|
93
|
|
|
$img = new \OC_Image(); |
|
94
|
|
|
$fileName = $ownerView->getLocalFile($path); |
|
95
|
|
|
$img->loadFromFile($fileName); |
|
96
|
|
|
|
|
97
|
|
|
$x = $face->getLeft (); |
|
98
|
|
|
$y = $face->getTop (); |
|
99
|
|
|
$w = $face->getRight () - $x; |
|
100
|
|
|
$h = $face->getBottom () - $y; |
|
101
|
|
|
|
|
102
|
|
|
$padding = $h*0.25; |
|
103
|
|
|
$x -= $padding; |
|
104
|
|
|
$y -= $padding; |
|
105
|
|
|
$w += $padding*2; |
|
106
|
|
|
$h += $padding*2; |
|
107
|
|
|
|
|
108
|
|
|
$img->crop($x, $y, $w, $h); |
|
109
|
|
|
$img->scaleDownToFit(32, 32); |
|
110
|
|
|
|
|
111
|
|
|
$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]); |
|
112
|
|
|
$resp->setETag((string)crc32($img->data())); |
|
113
|
|
|
$resp->cacheFor(7 * 24 * 60 * 60); |
|
114
|
|
|
$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
115
|
|
|
|
|
116
|
|
|
return $resp; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @NoAdminRequired |
|
121
|
|
|
* |
|
122
|
|
|
*/ |
|
123
|
|
|
public function random () { |
|
124
|
|
|
$faces = $this->faceMapper->findRandom($this->userId); |
|
125
|
|
|
return new DataResponse($faces); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @NoAdminRequired |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $fullpath |
|
132
|
|
|
*/ |
|
133
|
|
|
public function findFile ($fullpath) { |
|
134
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
135
|
|
|
$fileId = $userFolder->get($fullpath)->getId(); |
|
136
|
|
|
$faces = $this->faceMapper->findFile($this->userId, $fileId); |
|
137
|
|
|
return new DataResponse($faces); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @NoAdminRequired |
|
142
|
|
|
* |
|
143
|
|
|
* @param int $id |
|
144
|
|
|
* @param string $name |
|
|
|
|
|
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
public function updateName ($id, $newName) { |
|
|
|
|
|
|
147
|
|
|
$face = $this->faceMapper->find($id, $this->userId); |
|
148
|
|
|
$face->setName($newName); |
|
149
|
|
|
$face->setDistance(0.0); |
|
150
|
|
|
$newFace = $this->faceMapper->update($face); |
|
151
|
|
|
return new DataResponse($newFace); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @NoAdminRequired |
|
156
|
|
|
* |
|
157
|
|
|
* @param int $id |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function invalidate($id) { |
|
|
|
|
|
|
160
|
|
|
$face = $this->faceMapper->find($id, $this->userId); |
|
161
|
|
|
$note->setDistance(1.0); |
|
|
|
|
|
|
162
|
|
|
$newFace = $this->faceMapper->update($face); |
|
163
|
|
|
return new DataResponse($newFace); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.