| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace OCA\FaceRecognition\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use OCP\IConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use OCA\FaceRecognition\Db\ImageMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use OCA\FaceRecognition\Db\Face; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use OCA\FaceRecognition\Db\FaceMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use OCA\FaceRecognition\Db\Person; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use OCA\FaceRecognition\Db\PersonMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use OCA\FaceRecognition\Service\FileService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use OCA\FaceRecognition\Migration\AddDefaultFaceModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class FileController extends Controller { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private $imageMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	private $personMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	private $faceMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	private $fileService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private $userId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	public function __construct($AppName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	                            IRequest     $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	                            IConfig      $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	                            ImageMapper  $imageMapper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	                            PersonMapper $personMapper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	                            FaceMapper   $faceMapper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	                            FileService  $fileService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	                            $UserId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		parent::__construct($AppName, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$this->config       = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$this->imageMapper  = $imageMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->personMapper = $personMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->faceMapper   = $faceMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$this->fileService  = $fileService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$this->userId       = $UserId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	public function getPersonsFromPath(string $fullpath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$userEnabled = $this->config->getUserValue($this->userId, 'facerecognition', 'enabled', 'false'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		$resp = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		if ($userEnabled !== 'true') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			$resp['enabled'] = 'false'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			return new DataResponse($resp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$file = $this->fileService->getFileByPath($fullpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$fileId = $file->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		$image = $this->imageMapper->findFromFile($this->userId, $fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		$resp['enabled'] = 'true'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$resp['is_allowed'] = $this->fileService->isAllowedNode($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$resp['parent_detection'] = !$this->fileService->isUnderNoDetection($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$resp['image_id'] = $image ? $image->getId() : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$resp['is_processed'] = $image ? $image->getIsProcessed() : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$resp['error'] = $image ? $image->getError() : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$resp['persons'] = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$persons = $this->personMapper->findFromFile($this->userId, $fileId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		foreach ($persons as $person) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			$face = $this->faceMapper->getPersonOnFile($this->userId, $person->getId(), $fileId, $model); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			if (!count($face)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 				continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$facePerson = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			$facePerson['name'] = $person->getName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			$facePerson['person_id'] = $person->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			$facePerson['face'] = $face[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$resp['persons'][] = $facePerson; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		return new DataResponse($resp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	public function getFolderOptions(string $fullpath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		$userEnabled = $this->config->getUserValue($this->userId, 'facerecognition', 'enabled', 'false'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$resp = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		if ($userEnabled !== 'true') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$resp['enabled'] = 'false'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			return new DataResponse($resp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$folder = $this->fileService->getFileByPath($fullpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		$resp['enabled'] = 'true'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$resp['is_allowed'] = $this->fileService->isAllowedNode($folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$resp['parent_detection'] = !$this->fileService->isUnderNoDetection($folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		$resp['child_detection'] = $this->fileService->allowsChildDetection($folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return new DataResponse($resp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 	public function setFolderOptions(string $fullpath, bool $detection) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  | 		$folder = $this->fileService->getFileByPath($fullpath); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  | 		$done = $this->fileService->setAllowChildDetection($folder, $detection); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 		$resp = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 		$resp['done'] = $done ? 'true' : 'false'; | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  | 		$resp['child_detection'] = $detection ? 'true' : 'false'; | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  | 		return $this->getFolderOptions($fullpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  |