|
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
|
|
|
use OCP\Files\IHomeStorage; |
|
32
|
|
|
|
|
33
|
|
|
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask; |
|
34
|
|
|
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext; |
|
35
|
|
|
use OCA\FaceRecognition\Db\Image; |
|
36
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
|
37
|
|
|
use OCA\FaceRecognition\Helper\Requirements; |
|
38
|
|
|
use OCA\FaceRecognition\Migration\AddDefaultFaceModel; |
|
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
|
|
|
/** |
|
55
|
|
|
* @param IConfig $config Config |
|
56
|
|
|
* @param ImageMapper $imageMapper Image mapper |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function __construct(IConfig $config, ImageMapper $imageMapper) { |
|
59
|
4 |
|
parent::__construct(); |
|
60
|
4 |
|
$this->config = $config; |
|
61
|
4 |
|
$this->imageMapper = $imageMapper; |
|
62
|
4 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public function description() { |
|
68
|
4 |
|
return "Crawl for missing images for each user and insert them in DB"; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function execute(FaceRecognitionContext $context) { |
|
75
|
4 |
|
$this->setContext($context); |
|
76
|
|
|
|
|
77
|
4 |
|
$fullImageScanDone = $this->config->getAppValue('facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false'); |
|
78
|
4 |
|
if ($fullImageScanDone == 'true') { |
|
79
|
|
|
// Completely skip this task, seems that we already did full scan |
|
80
|
|
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); |
|
84
|
|
|
|
|
85
|
|
|
// Check if we are called for one user only, or for all user in instance. |
|
86
|
4 |
|
$insertedImages = 0; |
|
87
|
4 |
|
$eligable_users = array(); |
|
88
|
4 |
|
if (is_null($this->context->user)) { |
|
89
|
|
|
$this->context->userManager->callForSeenUsers(function (IUser $user) use (&$eligable_users) { |
|
90
|
2 |
|
$eligable_users[] = $user->getUID(); |
|
91
|
2 |
|
}); |
|
92
|
|
|
} else { |
|
93
|
2 |
|
$eligable_users[] = $this->context->user->getUID(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
foreach($eligable_users as $user) { |
|
97
|
4 |
|
$insertedImages += $this->addMissingImagesForUser($user, $model); |
|
98
|
4 |
|
yield; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
if (is_null($this->context->user)) { |
|
102
|
2 |
|
$this->config->setAppValue('facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'true'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
4 |
|
$this->context->propertyBag['AddMissingImagesTask_insertedImages'] = $insertedImages; |
|
106
|
4 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Crawl filesystem for a given user |
|
111
|
|
|
* TODO: duplicated from Queue.php, figure out how to merge |
|
112
|
|
|
* (or delete this Queue.php when not needed) |
|
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
|
4 |
|
private function addMissingImagesForUser(string $userId, int $model): int { |
|
119
|
4 |
|
$this->logInfo(sprintf('Finding missing images for user %s', $userId)); |
|
120
|
4 |
|
\OC_Util::tearDownFS(); |
|
|
|
|
|
|
121
|
4 |
|
\OC_Util::setupFS($userId); |
|
122
|
|
|
|
|
123
|
4 |
|
$userFolder = $this->context->rootFolder->getUserFolder($userId); |
|
124
|
4 |
|
return $this->parseUserFolder($model, $userFolder); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Recursively crawls given folder for a given user |
|
129
|
|
|
* |
|
130
|
|
|
* @param int $model Used model |
|
131
|
|
|
* @param Folder $folder Folder to recursively search images in |
|
132
|
|
|
* @return int Number of missing images found |
|
133
|
|
|
*/ |
|
134
|
4 |
|
private function parseUserFolder(int $model, Folder $folder): int { |
|
135
|
4 |
|
$insertedImages = 0; |
|
136
|
4 |
|
$nodes = $this->getPicturesFromFolder($folder); |
|
137
|
4 |
|
foreach ($nodes as $file) { |
|
138
|
1 |
|
$this->logDebug('Found ' . $file->getPath()); |
|
139
|
|
|
|
|
140
|
1 |
|
$image = new Image(); |
|
141
|
1 |
|
$image->setUser($file->getOwner()->getUid()); |
|
142
|
1 |
|
$image->setFile($file->getId()); |
|
143
|
1 |
|
$image->setModel($model); |
|
144
|
|
|
// todo: this check/insert logic for each image is so inefficient it hurts my mind |
|
145
|
1 |
|
if ($this->imageMapper->imageExists($image) == null) { |
|
|
|
|
|
|
146
|
|
|
// todo: can we have larger transaction with bulk insert? |
|
147
|
1 |
|
$this->imageMapper->insert($image); |
|
148
|
1 |
|
$insertedImages++; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
4 |
|
return $insertedImages; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Return all images from a given folder. |
|
157
|
|
|
* |
|
158
|
|
|
* TODO: It is inefficient since it copies the array recursively. |
|
159
|
|
|
* |
|
160
|
|
|
* @param Folder $folder Folder to get images from |
|
161
|
|
|
* @return array List of all images and folders to continue recursive crawling |
|
162
|
|
|
*/ |
|
163
|
4 |
|
private function getPicturesFromFolder(Folder $folder, $results = array()) { |
|
164
|
|
|
// todo: should we also care about this too: instanceOfStorage(ISharedStorage::class); |
|
165
|
4 |
|
if ($folder->getStorage()->instanceOfStorage(IHomeStorage::class) === false) { |
|
166
|
|
|
return $results; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
4 |
|
$nodes = $folder->getDirectoryListing(); |
|
170
|
|
|
|
|
171
|
4 |
|
foreach ($nodes as $node) { |
|
172
|
2 |
|
if ($node instanceof Folder and !$node->nodeExists('.nomedia')) { |
|
173
|
1 |
|
$results = $this->getPicturesFromFolder($node, $results); |
|
174
|
2 |
|
} else if ($node instanceof File) { |
|
175
|
2 |
|
if (Requirements::isImageTypeSupported($node->getMimeType())) { |
|
176
|
2 |
|
$results[] = $node; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
4 |
|
return $results; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths