|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <[email protected]> |
|
4
|
|
|
* @copyright Copyright (c) 2017, Matias De lellis <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
7
|
|
|
* @author Matias De lellis <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* @license GNU AGPL version 3 or any later version |
|
10
|
|
|
* |
|
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
14
|
|
|
* License, or (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* This program is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU Affero General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
namespace OCA\FaceRecognition; |
|
26
|
|
|
|
|
27
|
|
|
use OCP\Files\Folder; |
|
28
|
|
|
use OCP\Files\IHomeStorage; |
|
29
|
|
|
use OCP\Files\Node; |
|
30
|
|
|
use OCP\IConfig; |
|
31
|
|
|
use OCP\IDBConnection; |
|
32
|
|
|
use OCP\ILogger; |
|
33
|
|
|
use OCP\IUserManager; |
|
34
|
|
|
|
|
35
|
|
|
use OCA\FaceRecognition\BackgroundJob\Tasks\AddMissingImagesTask; |
|
36
|
|
|
use OCA\FaceRecognition\Db\Face; |
|
37
|
|
|
use OCA\FaceRecognition\Db\Image; |
|
38
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
|
39
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
|
40
|
|
|
use OCA\FaceRecognition\Db\PersonMapper; |
|
41
|
|
|
use OCA\FaceRecognition\Helper\Requirements; |
|
42
|
|
|
use OCA\FaceRecognition\Migration\AddDefaultFaceModel; |
|
43
|
|
|
|
|
44
|
|
|
class Watcher { |
|
45
|
|
|
|
|
46
|
|
|
/** @var IConfig Config */ |
|
47
|
|
|
private $config; |
|
48
|
|
|
|
|
49
|
|
|
/** @var ILogger Logger */ |
|
50
|
|
|
private $logger; |
|
51
|
|
|
|
|
52
|
|
|
/** @var IDBConnection */ |
|
53
|
|
|
private $connection; |
|
54
|
|
|
|
|
55
|
|
|
/** @var IUserManager */ |
|
56
|
|
|
private $userManager; |
|
57
|
|
|
|
|
58
|
|
|
/** @var FaceMapper */ |
|
59
|
|
|
private $faceMapper; |
|
60
|
|
|
|
|
61
|
|
|
/** @var ImageMapper */ |
|
62
|
|
|
private $imageMapper; |
|
63
|
|
|
|
|
64
|
|
|
/** @var PersonMapper */ |
|
65
|
|
|
private $personMapper; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Watcher constructor. |
|
69
|
|
|
* |
|
70
|
|
|
* @param IConfig $config |
|
71
|
|
|
* @param ILogger $logger |
|
72
|
|
|
* @param IDBConnection $connection |
|
73
|
|
|
* @param IUserManager $userManager |
|
74
|
|
|
* @param FaceMapper $faceMapper |
|
75
|
|
|
* @param ImageMapper $imageMapper |
|
76
|
|
|
* @param PersonMapper $personMapper |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public function __construct(IConfig $config, |
|
79
|
|
|
ILogger $logger, |
|
80
|
|
|
IDBConnection $connection, |
|
81
|
|
|
IUserManager $userManager, |
|
82
|
|
|
FaceMapper $faceMapper, |
|
83
|
|
|
ImageMapper $imageMapper, |
|
84
|
|
|
PersonMapper $personMapper) |
|
85
|
|
|
{ |
|
86
|
4 |
|
$this->config = $config; |
|
87
|
4 |
|
$this->logger = $logger; |
|
88
|
4 |
|
$this->connection = $connection; |
|
89
|
4 |
|
$this->userManager = $userManager; |
|
90
|
4 |
|
$this->faceMapper = $faceMapper; |
|
91
|
4 |
|
$this->imageMapper = $imageMapper; |
|
92
|
4 |
|
$this->personMapper = $personMapper; |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* A node has been updated. We just store the file id |
|
97
|
|
|
* with the current user in the DB |
|
98
|
|
|
* |
|
99
|
|
|
* @param Node $node |
|
100
|
|
|
*/ |
|
101
|
4 |
|
public function postWrite(Node $node) { |
|
102
|
4 |
|
$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); |
|
103
|
|
|
|
|
104
|
|
|
// FIXME: Can we know if it is a local or shared file? |
|
105
|
|
|
// todo: should we also care about this too: instanceOfStorage(ISharedStorage::class); |
|
106
|
|
|
/*if ($node->getStorage()->instanceOfStorage(IHomeStorage::class) === false) { |
|
107
|
|
|
return; |
|
108
|
|
|
}*/ |
|
109
|
|
|
|
|
110
|
4 |
|
if ($node instanceof Folder) { |
|
111
|
4 |
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// todo: issue #37 - if we detect .nomedia written, reset some global flag such that RemoveMissingImagesTask is triggered. |
|
115
|
|
|
|
|
116
|
|
|
if (!Requirements::isImageTypeSupported($node->getMimeType())) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// If we detect .nomedia file anywhere on the path to root folder (id===null), bail out |
|
121
|
|
|
$parentNode = $node->getParent(); |
|
122
|
|
|
while (($parentNode instanceof Folder) && ($parentNode->getId() !== null)) { |
|
123
|
|
|
if ($parentNode->nodeExists('.nomedia')) { |
|
124
|
|
|
$this->logger->debug( |
|
125
|
|
|
"Skipping inserting image " . $node->getName() . " because directory " . $parentNode->getName() . " contains .nomedia file"); |
|
126
|
|
|
return; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$parentNode = $parentNode->getParent(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$owner = $node->getOwner()->getUid(); |
|
133
|
|
|
|
|
134
|
|
|
if (!$this->userManager->userExists($owner)) { |
|
135
|
|
|
$this->logger->debug( |
|
136
|
|
|
"Skipping inserting image " . $node->getName() . " because it seems that user " . $owner . " doesn't exist"); |
|
137
|
|
|
return; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->logger->debug("Inserting/updating image " . $node->getName() . " for face recognition"); |
|
141
|
|
|
|
|
142
|
|
|
$image = new Image(); |
|
143
|
|
|
$image->setUser($owner); |
|
144
|
|
|
$image->setFile($node->getId()); |
|
145
|
|
|
$image->setModel($model); |
|
146
|
|
|
|
|
147
|
|
|
$imageId = $this->imageMapper->imageExists($image); |
|
148
|
|
|
if ($imageId === null) { |
|
149
|
|
|
// todo: can we have larger transaction with bulk insert? |
|
150
|
|
|
$this->imageMapper->insert($image); |
|
151
|
|
|
} else { |
|
152
|
|
|
$this->imageMapper->resetImage($image); |
|
153
|
|
|
// note that invalidatePersons depends on existence of faces for a given image, |
|
154
|
|
|
// and we must invalidate before we delete faces! |
|
155
|
|
|
$this->personMapper->invalidatePersons($imageId); |
|
156
|
|
|
$this->faceMapper->removeFaces($imageId); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* A node has been delete. Remove faces with file id |
|
162
|
|
|
* with the current user in the DB |
|
163
|
|
|
* |
|
164
|
|
|
* @param Node $node |
|
165
|
|
|
*/ |
|
166
|
|
|
public function postDelete(Node $node) { |
|
167
|
|
|
$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); |
|
168
|
|
|
|
|
169
|
|
|
// todo: should we also care about this too: instanceOfStorage(ISharedStorage::class); |
|
170
|
|
|
if ($node->getStorage()->instanceOfStorage(IHomeStorage::class) === false) { |
|
171
|
|
|
return; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($node instanceof Folder) { |
|
175
|
|
|
return; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if ($node->getName() === '.nomedia') { |
|
179
|
|
|
// If user deleted file named .nomedia, that means all images in this and all child directories should be added. |
|
180
|
|
|
// But, instead of doing that here, better option seem to be to just reset global flag that image scan is not done. |
|
181
|
|
|
// This will trigger another round of image crawling in AddMissingImagesTask and those images will be added. |
|
182
|
|
|
$this->config->setAppValue('facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false'); |
|
183
|
|
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (!Requirements::isImageTypeSupported($node->getMimeType())) { |
|
187
|
|
|
return; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$owner = $node->getOwner()->getUid(); |
|
191
|
|
|
|
|
192
|
|
|
$this->logger->debug("Deleting image " . $node->getName() . " from face recognition"); |
|
193
|
|
|
|
|
194
|
|
|
$image = new Image(); |
|
195
|
|
|
$image->setUser($owner); |
|
196
|
|
|
$image->setFile($node->getId()); |
|
197
|
|
|
$image->setModel($model); |
|
198
|
|
|
|
|
199
|
|
|
$imageId = $this->imageMapper->imageExists($image); |
|
200
|
|
|
if ($imageId !== null) { |
|
201
|
|
|
// note that invalidatePersons depends on existence of faces for a given image, |
|
202
|
|
|
// and we must invalidate before we delete faces! |
|
203
|
|
|
$this->personMapper->invalidatePersons($imageId); |
|
204
|
|
|
$this->faceMapper->removeFaces($imageId); |
|
205
|
|
|
|
|
206
|
|
|
$image->setId($imageId); |
|
207
|
|
|
$this->imageMapper->delete($image); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|