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\BackgroundJob\Tasks; |
25
|
|
|
|
26
|
|
|
use OCP\IConfig; |
27
|
|
|
use OCP\IUser; |
28
|
|
|
|
29
|
|
|
use OCP\Files\File; |
30
|
|
|
use OCP\Files\Folder; |
31
|
|
|
|
32
|
|
|
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask; |
33
|
|
|
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext; |
34
|
|
|
use OCA\FaceRecognition\Db\Image; |
35
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
36
|
|
|
use OCA\FaceRecognition\Helper\Requirements; |
37
|
|
|
use OCA\FaceRecognition\Migration\AddDefaultFaceModel; |
38
|
|
|
use OCA\FaceRecognition\Service\FileService; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Task that, for each user, crawls for all images in filesystem and insert them in database. |
42
|
|
|
* This is job that normally does file watcher, but this should be done at least once, |
43
|
|
|
* after app is installed (or re-enabled). |
44
|
|
|
*/ |
45
|
|
|
class AddMissingImagesTask extends FaceRecognitionBackgroundTask { |
46
|
|
|
const FULL_IMAGE_SCAN_DONE_KEY = "full_image_scan_done"; |
47
|
|
|
|
48
|
|
|
/** @var IConfig Config */ |
49
|
|
|
private $config; |
50
|
|
|
|
51
|
|
|
/** @var ImageMapper Image mapper */ |
52
|
|
|
private $imageMapper; |
53
|
|
|
|
54
|
|
|
/** @var FileService */ |
55
|
|
|
private $fileService; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param IConfig $config Config |
59
|
|
|
* @param ImageMapper $imageMapper Image mapper |
60
|
|
|
* @param FileService $fileService File Service |
61
|
|
|
*/ |
62
|
|
|
public function __construct(IConfig $config, |
63
|
|
|
ImageMapper $imageMapper, |
64
|
|
|
FileService $fileService) { |
65
|
|
|
parent::__construct(); |
66
|
|
|
$this->config = $config; |
67
|
|
|
$this->imageMapper = $imageMapper; |
68
|
|
|
$this->fileService = $fileService; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @inheritdoc |
73
|
|
|
*/ |
74
|
|
|
public function description() { |
75
|
|
|
return "Crawl for missing images for each user and insert them in DB"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function execute(FaceRecognitionContext $context) { |
82
|
|
|
$this->setContext($context); |
83
|
|
|
|
84
|
|
|
$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); |
85
|
|
|
|
86
|
|
|
// Check if we are called for one user only, or for all user in instance. |
87
|
|
|
$insertedImages = 0; |
88
|
|
|
$eligable_users = array(); |
89
|
|
|
if (is_null($this->context->user)) { |
90
|
|
|
$this->context->userManager->callForSeenUsers(function (IUser $user) use (&$eligable_users) { |
91
|
|
|
$eligable_users[] = $user->getUID(); |
92
|
|
|
}); |
93
|
|
|
} else { |
94
|
|
|
$eligable_users[] = $this->context->user->getUID(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach($eligable_users as $user) { |
98
|
|
|
$userEnabled = $this->config->getUserValue($user, 'facerecognition', 'enabled', 'false'); |
99
|
|
|
if ($userEnabled === 'false') { |
100
|
|
|
// Completely skip this task for this user, seems that disable analysis |
101
|
|
|
$this->logInfo('Skipping image scan for user ' . $user . ' that has disabled the analysis'); |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$fullImageScanDone = $this->config->getUserValue($user, 'facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false'); |
106
|
|
|
if ($fullImageScanDone === 'true') { |
107
|
|
|
// Completely skip this task for this user, seems that we already did full scan for him |
108
|
|
|
$this->logDebug('Skipping full image scan for user ' . $user); |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$insertedImages += $this->addMissingImagesForUser($user, $model); |
113
|
|
|
$this->config->setUserValue($user, 'facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'true'); |
114
|
|
|
yield; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$this->context->propertyBag['AddMissingImagesTask_insertedImages'] = $insertedImages; |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Crawl filesystem for a given user |
123
|
|
|
* |
124
|
|
|
* @param string $userId ID of the user for which to crawl images for |
125
|
|
|
* @param int $model Used model |
126
|
|
|
* @return int Number of missing images found |
127
|
|
|
*/ |
128
|
|
|
private function addMissingImagesForUser(string $userId, int $model): int { |
129
|
|
|
$this->logInfo(sprintf('Finding missing images for user %s', $userId)); |
130
|
|
|
$this->fileService->setupFS($userId); |
131
|
|
|
|
132
|
|
|
$userFolder = $this->context->rootFolder->getUserFolder($userId); |
133
|
|
|
return $this->parseUserFolder($userId, $model, $userFolder); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Recursively crawls given folder for a given user |
138
|
|
|
* |
139
|
|
|
* @param int $model Used model |
140
|
|
|
* @param Folder $folder Folder to recursively search images in |
141
|
|
|
* @return int Number of missing images found |
142
|
|
|
*/ |
143
|
|
|
private function parseUserFolder(string $userId, int $model, Folder $folder): int { |
144
|
|
|
$insertedImages = 0; |
145
|
|
|
$nodes = $this->getPicturesFromFolder($folder); |
146
|
|
|
foreach ($nodes as $file) { |
147
|
|
|
$this->logDebug('Found ' . $file->getPath()); |
148
|
|
|
|
149
|
|
|
$image = new Image(); |
150
|
|
|
$image->setUser($userId); |
151
|
|
|
$image->setFile($file->getId()); |
152
|
|
|
$image->setModel($model); |
153
|
|
|
// todo: this check/insert logic for each image is so inefficient it hurts my mind |
154
|
|
|
if ($this->imageMapper->imageExists($image) === null) { |
155
|
|
|
// todo: can we have larger transaction with bulk insert? |
156
|
|
|
$this->imageMapper->insert($image); |
157
|
|
|
$insertedImages++; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $insertedImages; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Return all images from a given folder. |
166
|
|
|
* |
167
|
|
|
* TODO: It is inefficient since it copies the array recursively. |
168
|
|
|
* |
169
|
|
|
* @param Folder $folder Folder to get images from |
170
|
|
|
* @return array List of all images and folders to continue recursive crawling |
171
|
|
|
*/ |
172
|
|
|
private function getPicturesFromFolder(Folder $folder, $results = array()) { |
173
|
|
|
$handleSharedFiles = $this->config->getAppValue('facerecognition', 'handle-shared-files', 'false'); |
174
|
|
|
|
175
|
|
|
$nodes = $folder->getDirectoryListing(); |
176
|
|
|
foreach ($nodes as $node) { |
177
|
|
|
if (!$this->fileService->isUserFile($node) && |
178
|
|
|
($this->fileService->isSharedFile($node) && $handleSharedFiles !== 'true')) { |
179
|
|
|
$this->logDebug('Ignore ' . $node->getPath() . ' since is shared and is disabled'); |
180
|
|
|
continue; |
181
|
|
|
} |
182
|
|
|
if ($node instanceof Folder and !$node->nodeExists('.nomedia')) { |
183
|
|
|
$results = $this->getPicturesFromFolder($node, $results); |
184
|
|
|
} else if ($node instanceof File) { |
185
|
|
|
if (Requirements::isImageTypeSupported($node->getMimeType())) { |
186
|
|
|
$results[] = $node; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $results; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|