Passed
Push — master ( a86ea0...028966 )
by Matias
05:02
created

AddMissingImagesTask::execute()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
nc 4
nop 1
dl 0
loc 27
ccs 15
cts 17
cp 0.8824
crap 5.0406
rs 9.4222
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017-2020 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\BackgroundJob\Tasks;
25
26
use OCP\IUser;
27
28
use OCP\Files\File;
29
use OCP\Files\Folder;
30
31
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
32
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
33
use OCA\FaceRecognition\Db\Image;
34
use OCA\FaceRecognition\Db\ImageMapper;
35
36
use OCA\FaceRecognition\Service\FileService;
37
use OCA\FaceRecognition\Service\SettingsService;
38
39
/**
40
 * Task that, for each user, crawls for all images in filesystem and insert them in database.
41
 * This is job that normally does file watcher, but this should be done at least once,
42
 * after app is installed (or re-enabled).
43
 */
44
class AddMissingImagesTask extends FaceRecognitionBackgroundTask {
45
	const FULL_IMAGE_SCAN_DONE_KEY = "full_image_scan_done";
46
47
	/** @var ImageMapper Image mapper */
48
	private $imageMapper;
49
50
	/** @var FileService */
51
	private $fileService;
52
53
	/** @var SettingsService Settings service */
54
	private $settingsService;
55
56
	/**
57
	 * @param ImageMapper $imageMapper Image mapper
58
	 * @param FileService $fileService File Service
59
	 * @param SettingsService $settingsService Settings Service
60
	 */
61 10
	public function __construct(ImageMapper     $imageMapper,
62
	                            FileService     $fileService,
63
	                            SettingsService $settingsService)
64
	{
65 10
		parent::__construct();
66
67 10
		$this->imageMapper     = $imageMapper;
68 10
		$this->fileService     = $fileService;
69 10
		$this->settingsService = $settingsService;
70
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75 4
	public function description() {
76 4
		return "Crawl for missing images for each user and insert them in DB";
77
	}
78
79
	/**
80
	 * @inheritdoc
81
	 */
82 10
	public function execute(FaceRecognitionContext $context) {
83 10
		$this->setContext($context);
84
85
		// Check if we are called for one user only, or for all user in instance.
86 10
		$insertedImages = 0;
87 10
		$eligable_users = $this->context->getEligibleUsers();
88 10
		foreach($eligable_users as $user) {
89 10
			if (!$this->settingsService->getUserEnabled($user)) {
90
				// Completely skip this task for this user, seems that disable analysis
91 2
				$this->logInfo('Skipping image scan for user ' . $user . ' that has disabled the analysis');
92 2
				continue;
93
			}
94
95 10
			if (!$this->context->isRunningInSyncMode() &&
96 10
			    $this->settingsService->getUserFullScanDone($user)) {
97
				// Completely skip this task for this user, seems that we already did full scan for him
98
				$this->logDebug('Skipping full image scan for user ' . $user);
99
				continue;
100
			}
101
102 10
			$insertedImages += $this->addMissingImagesForUser($user, $this->settingsService->getCurrentFaceModel());
103 10
			$this->settingsService->setUserFullScanDone(true, $user);
104 10
			yield;
105
		}
106
107 10
		$this->context->propertyBag['AddMissingImagesTask_insertedImages'] = $insertedImages;
108 10
		return true;
109
	}
110
111
	/**
112
	 * Crawl filesystem for a given user
113
	 *
114
	 * @param string $userId ID of the user for which to crawl images for
115
	 * @param int $model Used model
116
	 * @return int Number of missing images found
117
	 */
118 10
	private function addMissingImagesForUser(string $userId, int $model): int {
119 10
		$this->logInfo(sprintf('Finding missing images for user %s', $userId));
120 10
		$this->fileService->setupFS($userId);
121
122 10
		$userFolder = $this->fileService->getUserFolder($userId);
123 10
		return $this->parseUserFolder($userId, $model, $userFolder);
124
	}
125
126
	/**
127
	 * Recursively crawls given folder for a given user
128
	 *
129
	 * @param int $model Used model
130
	 * @param Folder $folder Folder to recursively search images in
131
	 * @return int Number of missing images found
132
	 */
133 10
	private function parseUserFolder(string $userId, int $model, Folder $folder): int {
134 10
		$insertedImages = 0;
135 10
		$nodes = $this->fileService->getPicturesFromFolder($folder);
136 10
		foreach ($nodes as $file) {
137 7
			$this->logDebug('Found ' . $file->getPath());
138
139 7
			$image = new Image();
140 7
			$image->setUser($userId);
141 7
			$image->setFile($file->getId());
142 7
			$image->setModel($model);
143
			// todo: this check/insert logic for each image is so inefficient it hurts my mind
144 7
			if ($this->imageMapper->imageExists($image) === null) {
145
				// todo: can we have larger transaction with bulk insert?
146 7
				$this->imageMapper->insert($image);
147 7
				$insertedImages++;
148
			}
149
		}
150
151 10
		return $insertedImages;
152
	}
153
154
}
155