Completed
Push — exception_in_tasks ( 7463cd...3899e4 )
by Branko
01:58
created

FaceController::getFaceThumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 14
cp 0
rs 9.392
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A FaceController::random() 0 4 1
A FaceController::findFile() 0 6 1
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
class FaceController extends Controller {
16
17
	private $rootFolder;
18
	private $faceMapper;
19
	private $userId;
20
21 View Code Duplication
	public function __construct($AppName, IRequest $request, IRootFolder $rootFolder, FaceMapper $facemapper, $UserId) {
22
		parent::__construct($AppName, $request);
23
		$this->rootFolder = $rootFolder;
24
		$this->faceMapper = $facemapper;
25
		$this->userId = $UserId;
26
	}
27
28
	/**
29
	 * @NoAdminRequired
30
	 */
31
	 public function index() {
32
		$faces = $this->faceMapper->findAll($this->userId);
33
		return new DataResponse($faces);
34
	}
35
36
	/**
37
	 * @NoAdminRequired
38
	 *
39
	 * @param int $id
40
	 */
41
	public function find ($id) {
42
		$face = $this->faceMapper->find($this->userId, $id);
43
		return new DataResponse($face);
44
	}
45
46
	/**
47
	 * @NoAdminRequired
48
	 * @NoCSRFRequired
49
	 */
50
	public function getThumb ($id) {
51
		\OC_Util::tearDownFS();
52
		\OC_Util::setupFS($this->userId);
53
54
		$face = $this->faceMapper->find($id, $this->userId);
55
		$fileId = $face->getFile();
56
57
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
58
		$nodes = $userFolder->getById($fileId);
59
		$file = $nodes[0];
60
61
		$ownerView = new \OC\Files\View('/'. $this->userId . '/files');
62
		$path = $userFolder->getRelativePath($file->getPath());
63
64
		$img = new \OC_Image();
65
		$fileName = $ownerView->getLocalFile($path);
66
		$img->loadFromFile($fileName);
67
68
		$x = $face->getLeft ();
69
		$y = $face->getTop ();
70
		$w = $face->getRight () - $x;
71
		$h = $face->getBottom () - $y;
72
73
		$padding = $h*0.25;
74
		$x -= $padding;
75
		$y -= $padding;
76
		$w += $padding*2;
77
		$h += $padding*2;
78
79
		$img->crop($x, $y, $w, $h);
80
		$img->scaleDownToFit(64, 64);
81
82
		$resp = new DataDisplayResponse($img->data(), Http::STATUS_OK, ['Content-Type' => $img->mimeType()]);
83
		$resp->setETag((string)crc32($img->data()));
84
		$resp->cacheFor(7 * 24 * 60 * 60);
85
		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
86
87
		return $resp;
88
	}
89
90
	/**
91
	 * @NoAdminRequired
92
	 *
93
	 */
94
	public function random () {
95
		$faces = $this->faceMapper->findRandom($this->userId);
96
		return new DataResponse($faces);
97
	}
98
99
	/**
100
	 * @NoAdminRequired
101
	 *
102
	 * @param string $fullpath
103
	 */
104
	public function findFile ($fullpath) {
105
		$userFolder = $this->rootFolder->getUserFolder($this->userId);
106
		$fileId = $userFolder->get($fullpath)->getId();
107
		$faces = $this->faceMapper->findFile($this->userId, $fileId);
108
		return new DataResponse($faces);
109
	}
110
111
	/**
112
	 * @NoAdminRequired
113
	 *
114
	 * @param int $id
115
	 * @param string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
116
	 */
117 View Code Duplication
	public function updateName ($id, $newName) {
118
		$face = $this->faceMapper->find($id, $this->userId);
119
		$face->setName($newName);
120
		$face->setDistance(0.0);
121
		$newFace = $this->faceMapper->update($face);
122
		return new DataResponse($newFace);
123
	}
124
125
	/**
126
	 * @NoAdminRequired
127
	 *
128
	 * @param int $id
129
	 */
130 View Code Duplication
	public function invalidate($id) {
131
		$face = $this->faceMapper->find($id, $this->userId);
132
		$note->setDistance(1.0);
0 ignored issues
show
Bug introduced by
The variable $note does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
133
		$newFace = $this->faceMapper->update($face);
134
		return new DataResponse($newFace);
135
	}
136
137
}
138