Passed
Push — user-enable-settings ( 42437f...3f14b4 )
by Matias
03:52
created

ImageMapper::deleteUserImages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 5
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\Db;
25
26
use OCP\IDBConnection;
27
use OCP\IUser;
28
29
use OCP\AppFramework\Db\QBMapper;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
33
class ImageMapper extends QBMapper {
34
35 8
	public function __construct(IDBConnection $db) {
36 8
		parent::__construct($db, 'face_recognition_images', '\OCA\FaceRecognition\Db\Image');
37 8
	}
38
39
	public function find(string $userId, int $imageId): Image {
40
		$qb = $this->db->getQueryBuilder();
41
		$qb->select('id', 'file')
42
			->from('face_recognition_images', 'i')
43
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
44
			->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($imageId)));
45
		$image = $this->findEntity($qb);
46
		return $image;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $image returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\FaceRecognition\Db\Image.
Loading history...
47
	}
48
49
	public function findFromFile(string $userId, int $fileId) {
50
		$qb = $this->db->getQueryBuilder();
51
		$qb->select('id', 'is_processed', 'error')
52
		   ->from('face_recognition_images', 'i')
53
		   ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
54
		   ->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($fileId)));
55
56
		try {
57
			return $this->findEntity($qb);
58
		} catch (DoesNotExistException $e) {
59
			return null;
60
		}
61
	}
62
63 2
	public function imageExists(Image $image) {
64 2
		$qb = $this->db->getQueryBuilder();
65
		$query = $qb
66 2
			->select(['id'])
67 2
			->from('face_recognition_images')
68 2
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
69 2
			->andWhere($qb->expr()->eq('file', $qb->createParameter('file')))
70 2
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
71 2
			->setParameter('user', $image->getUser())
72 2
			->setParameter('file', $image->getFile())
73 2
			->setParameter('model', $image->getModel());
74 2
		$resultStatement = $query->execute();
75 2
		$row = $resultStatement->fetch();
76 2
		$resultStatement->closeCursor();
77 2
		return $row ? (int)$row['id'] : null;
78
	}
79
80
	public function countImages(int $model): int {
81
		$qb = $this->db->getQueryBuilder();
82
		$query = $qb
83
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
84
			->from('face_recognition_images')
85
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
86
			->setParameter('model', $model);
87
		$resultStatement = $query->execute();
88
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
89
		$resultStatement->closeCursor();
90
91
		return (int)$data[0];
92
	}
93
94
	public function countProcessedImages(int $model): int {
95
		$qb = $this->db->getQueryBuilder();
96
		$query = $qb
97
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
98
			->from('face_recognition_images')
99
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
100
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
101
			->setParameter('model', $model)
102
			->setParameter('is_processed', True);
103
		$resultStatement = $query->execute();
104
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
105
		$resultStatement->closeCursor();
106
107
		return (int)$data[0];
108
	}
109
110
	public function avgProcessingDuration(int $model): int {
111
		$qb = $this->db->getQueryBuilder();
112
		$query = $qb
113
			->select($qb->createFunction('AVG(' . $qb->getColumnName('processing_duration') . ')'))
114
			->from('face_recognition_images')
115
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
116
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
117
			->setParameter('model', $model)
118
			->setParameter('is_processed', True);
119
		$resultStatement = $query->execute();
120
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
121
		$resultStatement->closeCursor();
122
123
		return (int)$data[0];
124
	}
125
126 1
	public function countUserImages(string $userId, $model): int {
127 1
		$qb = $this->db->getQueryBuilder();
128
		$query = $qb
129 1
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
130 1
			->from('face_recognition_images')
131 1
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
132 1
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
133 1
			->setParameter('user', $userId)
134 1
			->setParameter('model', $model);
135 1
		$resultStatement = $query->execute();
136 1
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
137 1
		$resultStatement->closeCursor();
138
139 1
		return (int)$data[0];
140
	}
141
142
	public function countUserProcessedImages(string $userId, $model): int {
143
		$qb = $this->db->getQueryBuilder();
144
		$query = $qb
145
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
146
			->from('face_recognition_images')
147
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
148
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
149
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
150
			->setParameter('user', $userId)
151
			->setParameter('model', $model)
152
			->setParameter('is_processed', True);
153
		$resultStatement = $query->execute();
154
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
155
		$resultStatement->closeCursor();
156
157
		return (int)$data[0];
158
	}
159
160
	/**
161
	 * @param IUser|null $user User for which to get images for. If not given, all images from instance are returned.
162
	 */
163 4
	public function findImagesWithoutFaces(IUser $user = null) {
164 4
		$qb = $this->db->getQueryBuilder();
165
		$query = $qb
166 4
			->select(['id', 'user', 'file', 'model'])
167 4
			->from('face_recognition_images')
168 4
			->where($qb->expr()->eq('is_processed',  $qb->createParameter('is_processed')))
169 4
			->setParameter('is_processed', false, IQueryBuilder::PARAM_BOOL);
170 4
		if (!is_null($user)) {
171 4
			$query->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($user->getUID())));
172
		}
173
174 4
		$images = $this->findEntities($qb);
175 4
		return $images;
176
	}
177
178 2
	public function findImages(string $userId, int $model): array {
179 2
		$qb = $this->db->getQueryBuilder();
180 2
		$qb->select('i.id', 'i.file')
181 2
			->from($this->getTableName(), 'i')
182 2
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
183 2
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model)));
184
185 2
		$images = $this->findEntities($qb);
186 2
		return $images;
187
	}
188
189
	public function findImagesFromPerson(string $userId, string $name, int $model): array {
190
		$qb = $this->db->getQueryBuilder();
191
		$qb->select('i.id', 'i.file')
192
			->from('face_recognition_images', 'i')
193
			->innerJoin('i', 'face_recognition_faces', 'f', $qb->expr()->eq('f.image', 'i.id'))
194
			->innerJoin('i', 'face_recognition_persons', 'p', $qb->expr()->eq('f.person', 'p.id'))
195
			->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId)))
196
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model)))
197
			->andWhere($qb->expr()->eq('is_processed', $qb->createNamedParameter(True)))
198
			->andWhere($qb->expr()->like($qb->func()->lower('p.name'), $qb->createParameter('query')));
199
200
		$query = '%' . $this->db->escapeLikeParameter(strtolower($name)) . '%';
201
		$qb->setParameter('query', $query);
202
203
		$images = $this->findEntities($qb);
204
		return $images;
205
	}
206
207
	/**
208
	 * Writes to DB that image has been processed. Previously found faces are deleted and new ones are inserted.
209
	 * If there is exception, its stack trace is also updated.
210
	 *
211
	 * @param Image $image Image to be updated
212
	 * @param Face[] $faces Faces to insert
213
	 * @param int $duration Processing time, in milliseconds
214
	 * @param \Exception|null $e Any exception that happened during image processing
215
	 */
216
	public function imageProcessed(Image $image, array $faces, int $duration, \Exception $e = null) {
217
		$this->db->beginTransaction();
218
		try {
219
			// Update image itself
220
			//
221
			$error = null;
222
			if ($e !== null) {
223
				$error = substr($e->getMessage(), 0, 1024);
224
			}
225
226
			$qb = $this->db->getQueryBuilder();
227
			$qb->update('face_recognition_images')
228
				->set("is_processed", $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
229
				->set("error", $qb->createNamedParameter($error))
230
				->set("last_processed_time", $qb->createNamedParameter(new \DateTime(), IQueryBuilder::PARAM_DATE))
231
				->set("processing_duration", $qb->createNamedParameter($duration))
232
				->where($qb->expr()->eq('id', $qb->createNamedParameter($image->id)))
233
				->execute();
234
235
			// Delete all previous faces
236
			//
237
			$qb = $this->db->getQueryBuilder();
238
			$qb->delete('face_recognition_faces')
239
				->where($qb->expr()->eq('image', $qb->createNamedParameter($image->id)))
240
				->execute();
241
242
			// Insert all faces
243
			//
244
			foreach ($faces as $face) {
245
				// Simple INSERT will close cursor and we want to be in transaction, so use hard way
246
				// todo: should we move this to FaceMapper (don't forget to hand over connection though)
247
				$qb = $this->db->getQueryBuilder();
248
				$qb->insert('face_recognition_faces')
249
					->values([
250
						'image' => $qb->createNamedParameter($image->id),
251
						'person' => $qb->createNamedParameter(null),
252
						'left' => $qb->createNamedParameter($face->left),
253
						'right' => $qb->createNamedParameter($face->right),
254
						'top' => $qb->createNamedParameter($face->top),
255
						'bottom' => $qb->createNamedParameter($face->bottom),
256
						'descriptor' => $qb->createNamedParameter(json_encode($face->descriptor)),
257
						'creation_time' => $qb->createNamedParameter($face->creationTime, IQueryBuilder::PARAM_DATE),
258
					])
259
					->execute();
260
			}
261
262
			$this->db->commit();
263
		} catch (\Exception $e) {
264
			$this->db->rollBack();
265
			throw $e;
266
		}
267
	}
268
269
	/**
270
	 * Resets image by deleting all associated faces and prepares it to be processed again
271
	 *
272
	 * @param Image $image Image to reset
273
	 */
274
	public function resetImage(Image $image) {
275
		$qb = $this->db->getQueryBuilder();
276
		$qb->update($this->getTableName())
277
			->set("is_processed", $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
278
			->set("error", $qb->createNamedParameter(null))
279
			->set("last_processed_time", $qb->createNamedParameter(null))
280
			->where($qb->expr()->eq('user', $qb->createNamedParameter($image->getUser())))
281
			->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($image->getFile())))
282
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($image->getModel())))
283
			->execute();
284
	}
285
286
	/**
287
	 * Deletes all images from that user.
288
	 *
289
	 * @param string $userId User to drop images from table.
290
	 */
291 8
	public function deleteUserImages(string $userId) {
292 8
		$qb = $this->db->getQueryBuilder();
293 8
		$qb->delete($this->getTableName())
294 8
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
295 8
			->execute();
296 8
	}
297
298
}
299