|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Files_FullTextSearch - Index the content of your files |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maxence Lange <[email protected]> |
|
9
|
|
|
* @copyright 2018 |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OCA\Files_FullTextSearch\Service; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
use Exception; |
|
31
|
|
|
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException; |
|
32
|
|
|
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException; |
|
33
|
|
|
use OCA\Files_FullTextSearch\Model\FilesDocument; |
|
34
|
|
|
use OCA\Files_FullTextSearch\Provider\FilesProvider; |
|
35
|
|
|
use OCA\FullTextSearch\Exceptions\InterruptException; |
|
36
|
|
|
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException; |
|
37
|
|
|
use OCA\FullTextSearch\Model\Index; |
|
38
|
|
|
use OCA\FullTextSearch\Model\IndexDocument; |
|
39
|
|
|
use OCA\FullTextSearch\Model\Runner; |
|
40
|
|
|
use OCP\Files\File; |
|
41
|
|
|
use OCP\Files\FileInfo; |
|
42
|
|
|
use OCP\Files\Folder; |
|
43
|
|
|
use OCP\Files\InvalidPathException; |
|
44
|
|
|
use OCP\Files\IRootFolder; |
|
45
|
|
|
use OCP\Files\Node; |
|
46
|
|
|
use OCP\Files\NotFoundException; |
|
47
|
|
|
use OCP\Files\NotPermittedException; |
|
48
|
|
|
use OCP\Files\StorageNotAvailableException; |
|
49
|
|
|
use OCP\IUserManager; |
|
50
|
|
|
use OCP\Share\IManager; |
|
51
|
|
|
|
|
52
|
|
|
class FilesService { |
|
53
|
|
|
|
|
54
|
|
|
const MIMETYPE_TEXT = 'files_text'; |
|
55
|
|
|
const MIMETYPE_PDF = 'files_pdf'; |
|
56
|
|
|
const MIMETYPE_OFFICE = 'files_office'; |
|
57
|
|
|
const MIMETYPE_IMAGE = 'files_image'; |
|
58
|
|
|
const MIMETYPE_AUDIO = 'files_audio'; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** @var IRootFolder */ |
|
62
|
|
|
private $rootFolder; |
|
63
|
|
|
|
|
64
|
|
|
/** @var IUserManager */ |
|
65
|
|
|
private $userManager; |
|
66
|
|
|
|
|
67
|
|
|
/** @var IManager */ |
|
68
|
|
|
private $shareManager; |
|
69
|
|
|
|
|
70
|
|
|
/** @var ConfigService */ |
|
71
|
|
|
private $configService; |
|
72
|
|
|
|
|
73
|
|
|
/** @var LocalFilesService */ |
|
74
|
|
|
private $localFilesService; |
|
75
|
|
|
|
|
76
|
|
|
/** @var ExternalFilesService */ |
|
77
|
|
|
private $externalFilesService; |
|
78
|
|
|
|
|
79
|
|
|
/** @var GroupFoldersService */ |
|
80
|
|
|
private $groupFoldersService; |
|
81
|
|
|
|
|
82
|
|
|
/** @var MiscService */ |
|
83
|
|
|
private $miscService; |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* FilesService constructor. |
|
88
|
|
|
* |
|
89
|
|
|
* @param IRootFolder $rootFolder |
|
90
|
|
|
* @param IUserManager $userManager |
|
91
|
|
|
* @param IManager $shareManager |
|
92
|
|
|
* @param ConfigService $configService |
|
93
|
|
|
* @param LocalFilesService $localFilesService |
|
94
|
|
|
* @param ExternalFilesService $externalFilesService |
|
95
|
|
|
* @param GroupFoldersService $groupFoldersService |
|
96
|
|
|
* @param MiscService $miscService |
|
97
|
|
|
* |
|
98
|
|
|
* @internal param IProviderFactory $factory |
|
99
|
|
|
*/ |
|
100
|
|
|
public function __construct( |
|
101
|
|
|
IRootFolder $rootFolder, IUserManager $userManager, IManager $shareManager, |
|
102
|
|
|
ConfigService $configService, |
|
103
|
|
|
LocalFilesService $localFilesService, |
|
104
|
|
|
ExternalFilesService $externalFilesService, |
|
105
|
|
|
GroupFoldersService $groupFoldersService, |
|
106
|
|
|
MiscService $miscService |
|
107
|
|
|
) { |
|
108
|
|
|
$this->rootFolder = $rootFolder; |
|
109
|
|
|
$this->userManager = $userManager; |
|
110
|
|
|
$this->shareManager = $shareManager; |
|
111
|
|
|
|
|
112
|
|
|
$this->configService = $configService; |
|
113
|
|
|
$this->localFilesService = $localFilesService; |
|
114
|
|
|
$this->externalFilesService = $externalFilesService; |
|
115
|
|
|
$this->groupFoldersService = $groupFoldersService; |
|
116
|
|
|
|
|
117
|
|
|
$this->miscService = $miscService; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param Runner $runner |
|
123
|
|
|
* @param string $userId |
|
124
|
|
|
* |
|
125
|
|
|
* @return FilesDocument[] |
|
126
|
|
|
* @throws InterruptException |
|
127
|
|
|
* @throws InvalidPathException |
|
128
|
|
|
* @throws NotFoundException |
|
129
|
|
|
* @throws TickDoesNotExistException |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getFilesFromUser(Runner $runner, $userId) { |
|
132
|
|
|
|
|
133
|
|
|
$this->initFileSystems($userId); |
|
134
|
|
|
|
|
135
|
|
|
/** @var Folder $files */ |
|
136
|
|
|
$files = $this->rootFolder->getUserFolder($userId) |
|
137
|
|
|
->get('/'); |
|
138
|
|
|
$result = $this->getFilesFromDirectory($runner, $userId, $files); |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $userId |
|
146
|
|
|
*/ |
|
147
|
|
|
private function initFileSystems($userId) { |
|
148
|
|
|
$this->externalFilesService->initExternalFilesForUser($userId); |
|
149
|
|
|
$this->groupFoldersService->initGroupSharesForUser($userId); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param Runner $runner |
|
155
|
|
|
* @param string $userId |
|
156
|
|
|
* @param Folder $node |
|
157
|
|
|
* |
|
158
|
|
|
* @return FilesDocument[] |
|
159
|
|
|
* @throws InterruptException |
|
160
|
|
|
* @throws InvalidPathException |
|
161
|
|
|
* @throws NotFoundException |
|
162
|
|
|
* @throws TickDoesNotExistException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getFilesFromDirectory(Runner $runner, $userId, Folder $node) { |
|
165
|
|
|
$documents = []; |
|
166
|
|
|
|
|
167
|
|
|
try { |
|
168
|
|
|
if ($node->nodeExists('.noindex')) { |
|
169
|
|
|
return $documents; |
|
170
|
|
|
} |
|
171
|
|
|
} catch (StorageNotAvailableException $e) { |
|
|
|
|
|
|
172
|
|
|
return $documents; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$files = $node->getDirectoryListing(); |
|
176
|
|
|
foreach ($files as $file) { |
|
177
|
|
|
$runner->update('getFilesFromDirectory'); |
|
178
|
|
|
|
|
179
|
|
|
try { |
|
180
|
|
|
$documents[] = $this->generateFilesDocumentFromFile($file, $userId); |
|
181
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
182
|
|
|
continue; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ($file->getType() === FileInfo::TYPE_FOLDER) { |
|
186
|
|
|
/** @var $file Folder */ |
|
187
|
|
|
$documents = |
|
188
|
|
|
array_merge($documents, $this->getFilesFromDirectory($runner, $userId, $file)); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $documents; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param Node $file |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $viewerId |
|
200
|
|
|
* |
|
201
|
|
|
* @return FilesDocument |
|
202
|
|
|
* @throws FileIsNotIndexableException |
|
203
|
|
|
* @throws InvalidPathException |
|
204
|
|
|
* @throws NotFoundException |
|
205
|
|
|
* @throws Exception |
|
206
|
|
|
*/ |
|
207
|
|
|
private function generateFilesDocumentFromFile(Node $file, $viewerId) { |
|
208
|
|
|
|
|
209
|
|
|
$source = $this->getFileSource($file); |
|
210
|
|
|
$document = new FilesDocument(FilesProvider::FILES_PROVIDER_ID, $file->getId()); |
|
211
|
|
|
|
|
212
|
|
|
$ownerId = ''; |
|
213
|
|
|
if ($file->getOwner() !== null) { |
|
214
|
|
|
$ownerId = $file->getOwner() |
|
215
|
|
|
->getUID(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$document->setType($file->getType()) |
|
219
|
|
|
->setSource($source) |
|
220
|
|
|
->setOwnerId($ownerId) |
|
221
|
|
|
->setPath($this->getPathFromViewerId($file->getId(), $viewerId)) |
|
222
|
|
|
->setViewerId($viewerId) |
|
223
|
|
|
->setModifiedTime($file->getMTime()) |
|
224
|
|
|
->setMimetype($file->getMimetype()); |
|
225
|
|
|
|
|
226
|
|
|
return $document; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param Node $file |
|
232
|
|
|
* |
|
233
|
|
|
* @return string |
|
234
|
|
|
* @throws FileIsNotIndexableException |
|
235
|
|
|
* @throws NotFoundException |
|
236
|
|
|
*/ |
|
237
|
|
|
private function getFileSource(Node $file) { |
|
238
|
|
|
$source = ''; |
|
239
|
|
|
|
|
240
|
|
|
try { |
|
241
|
|
|
$this->localFilesService->getFileSource($file, $source); |
|
242
|
|
|
$this->externalFilesService->getFileSource($file, $source); |
|
243
|
|
|
$this->groupFoldersService->getFileSource($file, $source); |
|
244
|
|
|
} catch (KnownFileSourceException $e) { |
|
245
|
|
|
/** we know the source, just leave. */ |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
return $source; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @param string $userId |
|
254
|
|
|
* @param string $path |
|
255
|
|
|
* |
|
256
|
|
|
* @return Node |
|
257
|
|
|
* @throws NotFoundException |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getFileFromPath($userId, $path) { |
|
260
|
|
|
return $this->rootFolder->getUserFolder($userId) |
|
261
|
|
|
->get($path); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param string $userId |
|
267
|
|
|
* @param int $fileId |
|
268
|
|
|
* |
|
269
|
|
|
* @return Node |
|
270
|
|
|
*/ |
|
271
|
|
|
public function getFileFromId($userId, $fileId) { |
|
272
|
|
|
|
|
273
|
|
|
try { |
|
274
|
|
|
$files = $this->rootFolder->getUserFolder($userId) |
|
275
|
|
|
->getById($fileId); |
|
276
|
|
|
} catch (Exception $e) { |
|
277
|
|
|
return null; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (sizeof($files) === 0) { |
|
281
|
|
|
return null; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$file = array_shift($files); |
|
285
|
|
|
|
|
286
|
|
|
return $file; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param int $fileId |
|
292
|
|
|
* @param string $viewerId |
|
293
|
|
|
* |
|
294
|
|
|
* @throws Exception |
|
295
|
|
|
* @return string |
|
296
|
|
|
*/ |
|
297
|
|
|
private function getPathFromViewerId($fileId, $viewerId) { |
|
298
|
|
|
|
|
299
|
|
|
$viewerFiles = $this->rootFolder->getUserFolder($viewerId) |
|
300
|
|
|
->getById($fileId); |
|
301
|
|
|
|
|
302
|
|
|
if (sizeof($viewerFiles) === 0) { |
|
303
|
|
|
return ''; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
$file = array_shift($viewerFiles); |
|
307
|
|
|
|
|
308
|
|
|
// TODO: better way to do this : we remove the '/userid/files/' |
|
309
|
|
|
$path = MiscService::noEndSlash(substr($file->getPath(), 8 + strlen($viewerId))); |
|
310
|
|
|
|
|
311
|
|
|
return $path; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @param FilesDocument $document |
|
317
|
|
|
*/ |
|
318
|
|
|
public function setDocumentInfo(FilesDocument $document) { |
|
319
|
|
|
|
|
320
|
|
|
$viewerId = $document->getAccess() |
|
321
|
|
|
->getViewerId(); |
|
322
|
|
|
|
|
323
|
|
|
$viewerFiles = $this->rootFolder->getUserFolder($viewerId) |
|
324
|
|
|
->getById($document->getId()); |
|
325
|
|
|
|
|
326
|
|
|
if (sizeof($viewerFiles) === 0) { |
|
327
|
|
|
return; |
|
328
|
|
|
} |
|
329
|
|
|
// we only take the first file |
|
330
|
|
|
$file = array_shift($viewerFiles); |
|
331
|
|
|
|
|
332
|
|
|
// TODO: better way to do this : we remove the '/userId/files/' |
|
333
|
|
|
$path = MiscService::noEndSlash(substr($file->getPath(), 7 + strlen($viewerId))); |
|
334
|
|
|
|
|
335
|
|
|
$document->setPath($path); |
|
336
|
|
|
$document->setFileName($file->getName()); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* @param FilesDocument $document |
|
342
|
|
|
*/ |
|
343
|
|
|
public function setDocumentTitle(FilesDocument $document) { |
|
344
|
|
|
$document->setTitle($document->getPath()); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
|
|
348
|
|
|
/** |
|
349
|
|
|
* @param FilesDocument $document |
|
350
|
|
|
*/ |
|
351
|
|
|
public function setDocumentLink(FilesDocument $document) { |
|
352
|
|
|
|
|
353
|
|
|
$path = $document->getPath(); |
|
354
|
|
|
$filename = $document->getFileName(); |
|
355
|
|
|
$dir = substr($path, 0, -strlen($filename)); |
|
356
|
|
|
|
|
357
|
|
|
$document->setLink( |
|
358
|
|
|
\OC::$server->getURLGenerator() |
|
359
|
|
|
->linkToRoute( |
|
360
|
|
|
'files.view.index', |
|
361
|
|
|
[ |
|
362
|
|
|
'dir' => $dir, |
|
363
|
|
|
'scrollto' => $filename, |
|
364
|
|
|
] |
|
365
|
|
|
) |
|
366
|
|
|
); |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param FilesDocument $document |
|
372
|
|
|
* |
|
373
|
|
|
* @throws InvalidPathException |
|
374
|
|
|
* @throws NotFoundException |
|
375
|
|
|
*/ |
|
376
|
|
|
public function setDocumentMore(FilesDocument $document) { |
|
377
|
|
|
|
|
378
|
|
|
$access = $document->getAccess(); |
|
379
|
|
|
$file = $this->getFileFromId($access->getViewerId(), $document->getId()); |
|
380
|
|
|
|
|
381
|
|
|
if ($file === null) { |
|
382
|
|
|
return; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
// TODO: better way to do this : we remove the '/userid/files/' |
|
386
|
|
|
$path = |
|
387
|
|
|
MiscService::noEndSlash(substr($file->getPath(), 7 + strlen($access->getViewerId()))); |
|
388
|
|
|
|
|
389
|
|
|
$more = [ |
|
390
|
|
|
'webdav' => $this->getWebdavId($document->getId()), |
|
391
|
|
|
'path' => $path, |
|
392
|
|
|
'timestamp' => $file->getMTime(), // FIXME: get the creation date of the file |
|
393
|
|
|
'mimetype' => $file->getMimetype(), |
|
394
|
|
|
'modified_timestamp' => $file->getMTime(), |
|
395
|
|
|
'etag' => $file->getEtag(), |
|
396
|
|
|
'permissions' => $file->getPermissions(), |
|
397
|
|
|
'size' => $file->getSize(), |
|
398
|
|
|
'favorite' => false // FIXME: get the favorite status |
|
399
|
|
|
]; |
|
400
|
|
|
|
|
401
|
|
|
$document->setMore($more); |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
|
|
405
|
|
|
/** |
|
406
|
|
|
* @param FilesDocument[] $documents |
|
407
|
|
|
* |
|
408
|
|
|
* @return FilesDocument[] |
|
409
|
|
|
*/ |
|
410
|
|
|
public function generateDocuments($documents) { |
|
411
|
|
|
|
|
412
|
|
|
$index = []; |
|
413
|
|
|
|
|
414
|
|
|
foreach ($documents as $document) { |
|
415
|
|
|
if (!($document instanceof FilesDocument)) { |
|
416
|
|
|
continue; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
try { |
|
420
|
|
|
$this->updateFilesDocument($document); |
|
421
|
|
|
} catch (Exception $e) { |
|
422
|
|
|
// TODO - update $document with a error status instead of just ignore ! |
|
423
|
|
|
$document->getIndex() |
|
424
|
|
|
->setStatus(Index::INDEX_IGNORE); |
|
425
|
|
|
echo 'Exception: ' . json_encode($e->getTrace()) . ' - ' . $e->getMessage() . "\n"; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
$index[] = $document; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
return $index; |
|
432
|
|
|
} |
|
433
|
|
|
|
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* @param Index $index |
|
437
|
|
|
* |
|
438
|
|
|
* @return FilesDocument |
|
439
|
|
|
* @throws FileIsNotIndexableException |
|
440
|
|
|
* @throws InvalidPathException |
|
441
|
|
|
* @throws NotFoundException |
|
442
|
|
|
* @throws NotPermittedException |
|
443
|
|
|
*/ |
|
444
|
|
|
private function generateDocumentFromIndex(Index $index) { |
|
445
|
|
|
$file = $this->getFileFromId($index->getOwnerId(), $index->getDocumentId()); |
|
446
|
|
|
|
|
447
|
|
|
if ($file === null) { |
|
448
|
|
|
$index->setStatus(Index::INDEX_REMOVE); |
|
449
|
|
|
$document = new FilesDocument($index->getProviderId(), $index->getDocumentId()); |
|
450
|
|
|
$document->setIndex($index); |
|
451
|
|
|
|
|
452
|
|
|
return $document; |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
$document = $this->generateFilesDocumentFromFile($file, $index->getOwnerId()); |
|
456
|
|
|
$document->setIndex($index); |
|
457
|
|
|
|
|
458
|
|
|
$this->updateFilesDocumentFromFile($document, $file); |
|
459
|
|
|
|
|
460
|
|
|
return $document; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
|
|
464
|
|
|
/** |
|
465
|
|
|
* @param IndexDocument $document |
|
466
|
|
|
* |
|
467
|
|
|
* @return bool |
|
468
|
|
|
*/ |
|
469
|
|
|
public function isDocumentUpToDate($document) { |
|
470
|
|
|
$index = $document->getIndex(); |
|
471
|
|
|
|
|
472
|
|
|
if (!$this->configService->compareIndexOptions($index)) { |
|
473
|
|
|
$index->setStatus(Index::INDEX_CONTENT); |
|
474
|
|
|
$document->setIndex($index); |
|
475
|
|
|
|
|
476
|
|
|
return false; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
if ($index->getStatus() !== Index::INDEX_OK) { |
|
480
|
|
|
return false; |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
if ($index->getLastIndex() >= $document->getModifiedTime()) { |
|
|
|
|
|
|
484
|
|
|
return true; |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
return false; |
|
488
|
|
|
} |
|
489
|
|
|
|
|
490
|
|
|
|
|
491
|
|
|
/** |
|
492
|
|
|
* @param Index $index |
|
493
|
|
|
* |
|
494
|
|
|
* @return FilesDocument |
|
|
|
|
|
|
495
|
|
|
* @throws InvalidPathException |
|
496
|
|
|
* @throws NotFoundException |
|
497
|
|
|
* @throws NotPermittedException |
|
498
|
|
|
*/ |
|
499
|
|
|
public function updateDocument(Index $index) { |
|
500
|
|
|
$this->initFileSystems($index->getOwnerId()); |
|
501
|
|
|
|
|
502
|
|
|
try { |
|
503
|
|
|
$document = $this->generateDocumentFromIndex($index); |
|
504
|
|
|
|
|
505
|
|
|
return $document; |
|
506
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
507
|
|
|
return null; |
|
508
|
|
|
} |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
|
|
512
|
|
|
/** |
|
513
|
|
|
* @param FilesDocument $document |
|
514
|
|
|
* |
|
515
|
|
|
* @throws InvalidPathException |
|
516
|
|
|
* @throws NotFoundException |
|
517
|
|
|
* @throws NotPermittedException |
|
518
|
|
|
*/ |
|
519
|
|
|
private function updateFilesDocument(FilesDocument $document) { |
|
520
|
|
|
$userFolder = $this->rootFolder->getUserFolder($document->getViewerId()); |
|
521
|
|
|
$file = $userFolder->get($document->getPath()); |
|
522
|
|
|
|
|
523
|
|
|
try { |
|
524
|
|
|
$this->updateFilesDocumentFromFile($document, $file); |
|
525
|
|
|
} catch (FileIsNotIndexableException $e) { |
|
526
|
|
|
$document->getIndex() |
|
527
|
|
|
->setStatus(Index::INDEX_IGNORE); |
|
528
|
|
|
} |
|
529
|
|
|
} |
|
530
|
|
|
|
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* @param FilesDocument $document |
|
534
|
|
|
* @param Node $file |
|
535
|
|
|
* |
|
536
|
|
|
* @throws InvalidPathException |
|
537
|
|
|
* @throws NotFoundException |
|
538
|
|
|
* @throws NotPermittedException |
|
539
|
|
|
*/ |
|
540
|
|
|
private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) { |
|
541
|
|
|
$this->updateDocumentAccess($document, $file); |
|
542
|
|
|
$this->updateContentFromFile($document, $file); |
|
543
|
|
|
|
|
544
|
|
|
$document->addTag($document->getSource()); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
|
|
548
|
|
|
/** |
|
549
|
|
|
* @param FilesDocument $document |
|
550
|
|
|
* @param Node $file |
|
551
|
|
|
*/ |
|
552
|
|
|
private function updateDocumentAccess(FilesDocument $document, Node $file) { |
|
553
|
|
|
|
|
554
|
|
|
$index = $document->getIndex(); |
|
555
|
|
|
|
|
556
|
|
|
if (!$index->isStatus(Index::INDEX_FULL) |
|
557
|
|
|
&& !$index->isStatus(FilesDocument::STATUS_FILE_ACCESS)) { |
|
558
|
|
|
return; |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
$this->localFilesService->updateDocumentAccess($document, $file); |
|
562
|
|
|
$this->externalFilesService->updateDocumentAccess($document, $file); |
|
563
|
|
|
$this->groupFoldersService->updateDocumentAccess($document, $file); |
|
564
|
|
|
|
|
565
|
|
|
$this->updateShareNames($document, $file); |
|
566
|
|
|
} |
|
567
|
|
|
|
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* @param FilesDocument $document |
|
571
|
|
|
* @param Node $file |
|
572
|
|
|
* |
|
573
|
|
|
* @throws InvalidPathException |
|
574
|
|
|
* @throws NotFoundException |
|
575
|
|
|
* @throws NotPermittedException |
|
576
|
|
|
*/ |
|
577
|
|
|
private function updateContentFromFile(FilesDocument $document, Node $file) { |
|
578
|
|
|
|
|
579
|
|
|
$document->setTitle($document->getPath()); |
|
580
|
|
|
|
|
581
|
|
|
if (!$document->getIndex() |
|
582
|
|
|
->isStatus(Index::INDEX_CONTENT) |
|
583
|
|
|
|| $file->getType() !== FileInfo::TYPE_FILE) { |
|
584
|
|
|
return; |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** @var File $file */ |
|
588
|
|
|
if ($file->getSize() < |
|
589
|
|
|
($this->configService->getAppValue(ConfigService::FILES_SIZE) * 1024 * 1024)) { |
|
590
|
|
|
$this->extractContentFromFileText($document, $file); |
|
591
|
|
|
$this->extractContentFromFileOffice($document, $file); |
|
592
|
|
|
$this->extractContentFromFilePDF($document, $file); |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
if ($document->getContent() === null) { |
|
596
|
|
|
$document->getIndex() |
|
597
|
|
|
->unsetStatus(Index::INDEX_CONTENT); |
|
598
|
|
|
} |
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
|
|
602
|
|
|
/** |
|
603
|
|
|
* @param FilesDocument $document |
|
604
|
|
|
* @param Node $file |
|
605
|
|
|
* |
|
606
|
|
|
* @return array |
|
607
|
|
|
*/ |
|
608
|
|
|
private function updateShareNames(FilesDocument $document, Node $file) { |
|
609
|
|
|
|
|
610
|
|
|
$users = []; |
|
611
|
|
|
|
|
612
|
|
|
$this->localFilesService->getShareUsersFromFile($file, $users); |
|
613
|
|
|
$this->externalFilesService->getShareUsers($document, $users); |
|
614
|
|
|
$this->groupFoldersService->getShareUsers($document, $users); |
|
615
|
|
|
|
|
616
|
|
|
$shareNames = []; |
|
617
|
|
|
foreach ($users as $user) { |
|
618
|
|
|
try { |
|
619
|
|
|
$shareNames[MiscService::secureUsername($user)] = |
|
620
|
|
|
$this->getPathFromViewerId($file->getId(), $user); |
|
621
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
622
|
|
|
} |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
$document->setInfo('share_names', $shareNames); |
|
626
|
|
|
|
|
627
|
|
|
// if ($file->getStorage() |
|
|
|
|
|
|
628
|
|
|
// ->isLocal() === false) { |
|
629
|
|
|
// $shares = $this->externalFilesService->getAllSharesFromExternalFile($access); |
|
630
|
|
|
// } else { |
|
631
|
|
|
// $shares = $this->getAllSharesFromFile($file); |
|
632
|
|
|
// } |
|
633
|
|
|
// |
|
634
|
|
|
// foreach ($shares as $user) { |
|
635
|
|
|
// try { |
|
636
|
|
|
// $shareNames[$user] = $this->getPathFromViewerId($file->getId(), $user); |
|
637
|
|
|
// } catch (Exception $e) { |
|
638
|
|
|
// } |
|
639
|
|
|
// } |
|
640
|
|
|
// |
|
641
|
|
|
return $shareNames; |
|
642
|
|
|
|
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* @param int $fileId |
|
647
|
|
|
* |
|
648
|
|
|
* @return string |
|
649
|
|
|
*/ |
|
650
|
|
|
private function getWebdavId($fileId) { |
|
651
|
|
|
$instanceId = $this->configService->getSystemValue('instanceid'); |
|
652
|
|
|
|
|
653
|
|
|
return sprintf("%08s", $fileId) . $instanceId; |
|
654
|
|
|
} |
|
655
|
|
|
|
|
656
|
|
|
|
|
657
|
|
|
/** |
|
658
|
|
|
* @param string $mimeType |
|
659
|
|
|
* |
|
660
|
|
|
* @return string |
|
661
|
|
|
*/ |
|
662
|
|
|
private function parseMimeType($mimeType) { |
|
663
|
|
|
|
|
664
|
|
|
// text file |
|
665
|
|
|
if ($mimeType === 'application/octet-stream' |
|
666
|
|
|
|| substr($mimeType, 0, 5) === 'text/') { |
|
667
|
|
|
return self::MIMETYPE_TEXT; |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
// PDF file |
|
671
|
|
|
if ($mimeType === 'application/pdf') { |
|
672
|
|
|
return self::MIMETYPE_PDF; |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
// Office file |
|
676
|
|
|
$officeMimes = [ |
|
677
|
|
|
'application/msword', |
|
678
|
|
|
'application/vnd.oasis.opendocument', |
|
679
|
|
|
'application/vnd.sun.xml', |
|
680
|
|
|
'application/vnd.openxmlformats-officedocument', |
|
681
|
|
|
'application/vnd.ms-word', |
|
682
|
|
|
'application/vnd.ms-powerpoint', |
|
683
|
|
|
'application/vnd.ms-excel' |
|
684
|
|
|
]; |
|
685
|
|
|
|
|
686
|
|
|
foreach ($officeMimes as $mime) { |
|
687
|
|
|
if (strpos($mimeType, $mime) === 0) { |
|
688
|
|
|
return self::MIMETYPE_OFFICE; |
|
689
|
|
|
} |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
return ''; |
|
693
|
|
|
} |
|
694
|
|
|
|
|
695
|
|
|
|
|
696
|
|
|
/** |
|
697
|
|
|
* @param FilesDocument $document |
|
698
|
|
|
* @param File $file |
|
699
|
|
|
* |
|
700
|
|
|
* @throws NotPermittedException |
|
701
|
|
|
*/ |
|
702
|
|
|
private function extractContentFromFileText(FilesDocument $document, File $file) { |
|
703
|
|
|
if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_TEXT) { |
|
704
|
|
|
return; |
|
705
|
|
|
} |
|
706
|
|
|
|
|
707
|
|
|
if (!$this->isSourceIndexable($document)) { |
|
708
|
|
|
return; |
|
709
|
|
|
} |
|
710
|
|
|
|
|
711
|
|
|
// on simple text file, elastic search+attachment pipeline can still detect language, useful ? |
|
712
|
|
|
// $document->setContent($file->getContent(), IndexDocument::NOT_ENCODED); |
|
713
|
|
|
|
|
714
|
|
|
// We try to avoid error with some base encoding of the document: |
|
715
|
|
|
$content = $file->getContent(); |
|
716
|
|
|
|
|
717
|
|
|
$document->setContent(base64_encode($content), IndexDocument::ENCODED_BASE64); |
|
718
|
|
|
} |
|
719
|
|
|
|
|
720
|
|
|
|
|
721
|
|
|
/** |
|
722
|
|
|
* @param FilesDocument $document |
|
723
|
|
|
* @param File $file |
|
724
|
|
|
* |
|
725
|
|
|
* @throws NotPermittedException |
|
726
|
|
|
*/ |
|
727
|
|
View Code Duplication |
private function extractContentFromFilePDF(FilesDocument $document, File $file) { |
|
|
|
|
|
|
728
|
|
|
if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_PDF) { |
|
729
|
|
|
return; |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
|
|
$this->configService->setDocumentIndexOption($document, ConfigService::FILES_PDF); |
|
733
|
|
|
if (!$this->isSourceIndexable($document)) { |
|
734
|
|
|
return; |
|
735
|
|
|
} |
|
736
|
|
|
|
|
737
|
|
|
if ($this->configService->getAppValue(ConfigService::FILES_PDF) !== '1') { |
|
738
|
|
|
$document->setContent(''); |
|
739
|
|
|
|
|
740
|
|
|
return; |
|
741
|
|
|
} |
|
742
|
|
|
|
|
743
|
|
|
$document->setContent(base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64); |
|
744
|
|
|
} |
|
745
|
|
|
|
|
746
|
|
|
|
|
747
|
|
|
/** |
|
748
|
|
|
* @param FilesDocument $document |
|
749
|
|
|
* @param File $file |
|
750
|
|
|
* |
|
751
|
|
|
* @throws NotPermittedException |
|
752
|
|
|
*/ |
|
753
|
|
View Code Duplication |
private function extractContentFromFileOffice(FilesDocument $document, File $file) { |
|
|
|
|
|
|
754
|
|
|
if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_OFFICE) { |
|
755
|
|
|
return; |
|
756
|
|
|
} |
|
757
|
|
|
|
|
758
|
|
|
$this->configService->setDocumentIndexOption($document, ConfigService::FILES_OFFICE); |
|
759
|
|
|
if (!$this->isSourceIndexable($document)) { |
|
760
|
|
|
return; |
|
761
|
|
|
} |
|
762
|
|
|
|
|
763
|
|
|
if ($this->configService->getAppValue(ConfigService::FILES_OFFICE) !== '1') { |
|
764
|
|
|
$document->setContent(''); |
|
765
|
|
|
|
|
766
|
|
|
return; |
|
767
|
|
|
} |
|
768
|
|
|
|
|
769
|
|
|
$document->setContent(base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64); |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
|
|
|
|
773
|
|
|
/** |
|
774
|
|
|
* @param FilesDocument $document |
|
775
|
|
|
* |
|
776
|
|
|
* @return bool |
|
777
|
|
|
*/ |
|
778
|
|
|
private function isSourceIndexable(FilesDocument $document) { |
|
779
|
|
|
$this->configService->setDocumentIndexOption($document, $document->getSource()); |
|
780
|
|
|
if ($this->configService->getAppValue($document->getSource()) !== '1') { |
|
781
|
|
|
$document->setContent(''); |
|
782
|
|
|
|
|
783
|
|
|
return false; |
|
784
|
|
|
} |
|
785
|
|
|
|
|
786
|
|
|
return true; |
|
787
|
|
|
} |
|
788
|
|
|
|
|
789
|
|
|
} |
|
790
|
|
|
|
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.