| 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 |  |  | 	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 |  |  | 	 * @NoCSRFRequired | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	public function getThumb ($id, $size) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		$face = $this->faceNewMapper->find($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		$image = $this->imageMapper->find($this->userId, $face->getImage()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		$fileId = $image->getFile(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		$userFolder = $this->rootFolder->getUserFolder($this->userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		$nodes = $userFolder->getById($fileId); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 		$file = $nodes[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 		$ownerView = new \OC\Files\View('/'. $this->userId . '/files'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 		$path = $userFolder->getRelativePath($file->getPath()); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 		$img = new \OC_Image(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		$fileName = $ownerView->getLocalFile($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 		$img->loadFromFile($fileName); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 		$x = $face->getLeft (); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 		$y = $face->getTop (); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 		$w = $face->getRight () - $x; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		$h = $face->getBottom () - $y; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  | 		$padding = $h*0.25; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | 		$x -= $padding; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  | 		$y -= $padding; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | 		$w += $padding*2; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 		$h += $padding*2; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		$img->crop($x, $y, $w, $h); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 		$img->scaleDownToFit($size, $size); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		$resp->setETag((string)crc32($img->data())); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		$resp->cacheFor(7 * 24 * 60 * 60); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		return $resp; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	 * @NoAdminRequired | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 	 * | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 	 * @param int $id | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	public function invalidate($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		$face = $this->faceMapper->find($id, $this->userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$note->setDistance(1.0); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$newFace = $this->faceMapper->update($face); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  | 		return new DataResponse($newFace); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |