Completed
Push — master ( 0c68cd...766ef1 )
by Maxence
02:02
created

FilesService::generateDocumentFromIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
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
114
		$this->localFilesService = $localFilesService;
115
		$this->externalFilesService = $externalFilesService;
116
		$this->groupFoldersService = $groupFoldersService;
117
118
		$this->miscService = $miscService;
119
	}
120
121
122
	/**
123
	 * @param Runner $runner
124
	 * @param string $userId
125
	 *
126
	 * @return FilesDocument[]
127
	 * @throws InterruptException
128
	 * @throws InvalidPathException
129
	 * @throws NotFoundException
130
	 * @throws TickDoesNotExistException
131
	 */
132
	public function getFilesFromUser(Runner $runner, $userId) {
133
134
		$this->externalFilesService->initExternalFilesForUser($userId);
135
		$this->groupFoldersService->initGroupShares();
136
137
		/** @var Folder $files */
138
		$files = $this->rootFolder->getUserFolder($userId)
139
								  ->get('/');
140
		$result = $this->getFilesFromDirectory($runner, $userId, $files);
141
142
		return $result;
143
	}
144
145
146
	/**
147
	 * @param Runner $runner
148
	 * @param string $userId
149
	 * @param Folder $node
150
	 *
151
	 * @return FilesDocument[]
152
	 * @throws InterruptException
153
	 * @throws InvalidPathException
154
	 * @throws NotFoundException
155
	 * @throws TickDoesNotExistException
156
	 */
157
	public function getFilesFromDirectory(Runner $runner, $userId, Folder $node) {
158
		$documents = [];
159
160
		try {
161
			if ($node->nodeExists('.noindex')) {
162
				return $documents;
163
			}
164
		} catch (StorageNotAvailableException $e) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\StorageNotAvailableException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file 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.

Loading history...
165
			return $documents;
166
		}
167
168
		$files = $node->getDirectoryListing();
169
		foreach ($files as $file) {
170
			$runner->update('getFilesFromDirectory');
171
172
			try {
173
				$documents[] = $this->generateFilesDocumentFromFile($file, $userId);
174
			} catch (FileIsNotIndexableException $e) {
175
				continue;
176
			}
177
178
			if ($file->getType() === FileInfo::TYPE_FOLDER) {
179
				/** @var $file Folder */
180
				$documents =
181
					array_merge($documents, $this->getFilesFromDirectory($runner, $userId, $file));
182
			}
183
184
		}
185
186
		return $documents;
187
	}
188
189
190
	/**
191
	 * @param Node $file
192
	 *
193
	 * @param string $viewerId
194
	 *
195
	 * @return FilesDocument
196
	 * @throws FileIsNotIndexableException
197
	 * @throws InvalidPathException
198
	 * @throws NotFoundException
199
	 */
200
	private function generateFilesDocumentFromFile(Node $file, $viewerId = '') {
201
202
		$source = $this->getFileSource($file);
203
		$document = new FilesDocument(FilesProvider::FILES_PROVIDER_ID, $file->getId());
204
205
		$ownerId = $file->getOwner()
206
						->getUID();
207
208
		$document->setType($file->getType())
209
				 ->setSource($source)
210
				 ->setOwnerId($ownerId)
211
				 ->setViewerId($viewerId)
212
				 ->setModifiedTime($file->getMTime())
213
				 ->setMimetype($file->getMimetype());
214
215
		return $document;
216
	}
217
218
219
	/**
220
	 * @param Node $file
221
	 *
222
	 * @return string
223
	 * @throws FileIsNotIndexableException
224
	 * @throws NotFoundException
225
	 */
226
	private function getFileSource(Node $file) {
227
		$source = '';
228
229
		try {
230
			$this->localFilesService->getFileSource($file, $source);
231
			$this->externalFilesService->getFileSource($file, $source);
232
			$this->groupFoldersService->getFileSource($file, $source);
233
		} catch (KnownFileSourceException $e) {
234
			/** we know the source, just leave. */
235
		}
236
237
		return $source;
238
	}
239
240
241
	/**
242
	 * @param string $userId
243
	 * @param string $path
244
	 *
245
	 * @return Node
246
	 * @throws NotFoundException
247
	 */
248
	public function getFileFromPath($userId, $path) {
249
		return $this->rootFolder->getUserFolder($userId)
250
								->get($path);
251
	}
252
253
254
	/**
255
	 * @param string $userId
256
	 * @param int $fileId
257
	 *
258
	 * @return Node
259
	 */
260
	public function getFileFromId($userId, $fileId) {
261
262
		try {
263
			$files = $this->rootFolder->getUserFolder($userId)
264
									  ->getById($fileId);
265
		} catch (Exception $e) {
266
			return null;
267
		}
268
269
		if (sizeof($files) === 0) {
270
			return null;
271
		}
272
273
		$file = array_shift($files);
274
275
		return $file;
276
	}
277
278
279
	/**
280
	 * @param int $fileId
281
	 * @param string $viewerId
282
	 *
283
	 * @throws Exception
284
	 * @return string
285
	 */
286
	private function getPathFromViewerId($fileId, $viewerId) {
287
288
		$viewerFiles = $this->rootFolder->getUserFolder($viewerId)
289
										->getById($fileId);
290
291
		if (sizeof($viewerFiles) === 0) {
292
			return '';
293
		}
294
295
		$file = array_shift($viewerFiles);
296
297
		// TODO: better way to do this : we remove the '/userid/files/'
298
		$path = MiscService::noEndSlash(substr($file->getPath(), 8 + strlen($viewerId)));
299
300
		return $path;
301
	}
302
303
304
	/**
305
	 * @param FilesDocument $document
306
	 */
307
	public function setDocumentInfo(FilesDocument $document) {
308
309
		$viewerId = $document->getAccess()
310
							 ->getViewerId();
311
312
		$viewerFiles = $this->rootFolder->getUserFolder($viewerId)
313
										->getById($document->getId());
314
315
		if (sizeof($viewerFiles) === 0) {
316
			return;
317
		}
318
		// we only take the first file
319
		$file = array_shift($viewerFiles);
320
321
		// TODO: better way to do this : we remove the '/userId/files/'
322
		$path = MiscService::noEndSlash(substr($file->getPath(), 7 + strlen($viewerId)));
323
324
		$document->setPath($path);
325
		$document->setFileName($file->getName());
326
	}
327
328
329
	/**
330
	 * @param FilesDocument $document
331
	 */
332
	public function setDocumentTitle(FilesDocument $document) {
333
		$document->setTitle($document->getPath());
334
	}
335
336
337
	/**
338
	 * @param FilesDocument $document
339
	 */
340
	public function setDocumentLink(FilesDocument $document) {
341
342
		$path = $document->getPath();
343
		$filename = $document->getFileName();
344
		$dir = substr($path, 0, -strlen($filename));
345
346
		$document->setLink(
347
			\OC::$server->getURLGenerator()
348
						->linkToRoute(
349
							'files.view.index',
350
							[
351
								'dir'      => $dir,
352
								'scrollto' => $filename,
353
							]
354
						)
355
		);
356
	}
357
358
359
	/**
360
	 * @param FilesDocument $document
361
	 *
362
	 * @throws InvalidPathException
363
	 * @throws NotFoundException
364
	 */
365
	public function setDocumentMore(FilesDocument $document) {
366
367
		$access = $document->getAccess();
368
		$file = $this->getFileFromId($access->getViewerId(), $document->getId());
369
370
		if ($file === null) {
371
			return;
372
		}
373
374
		// TODO: better way to do this : we remove the '/userid/files/'
375
		$path =
376
			MiscService::noEndSlash(substr($file->getPath(), 7 + strlen($access->getViewerId())));
377
378
		$more = [
379
			'webdav'             => $this->getWebdavId($document->getId()),
380
			'path'               => $path,
381
			'timestamp'          => $file->getMTime(), // FIXME: get the creation date of the file
382
			'mimetype'           => $file->getMimetype(),
383
			'modified_timestamp' => $file->getMTime(),
384
			'etag'               => $file->getEtag(),
385
			'permissions'        => $file->getPermissions(),
386
			'size'               => $file->getSize(),
387
			'favorite'           => false // FIXME: get the favorite status
388
		];
389
390
		$document->setMore($more);
391
	}
392
393
394
	/**
395
	 * @param FilesDocument[] $documents
396
	 *
397
	 * @return FilesDocument[]
398
	 */
399
	public function generateDocuments($documents) {
400
401
		$index = [];
402
403
		foreach ($documents as $document) {
404
			if (!($document instanceof FilesDocument)) {
405
				continue;
406
			}
407
408
			try {
409
				$document->setPath(
410
					$this->getPathFromViewerId($document->getId(), $document->getViewerId())
411
				);
412
413
				//echo '---- ' . $document->getPath() . ' ---- ' . $document->getSource() . "\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
414
				$this->updateDocumentFromFilesDocument($document);
415
			} catch (Exception $e) {
416
				// TODO - update $document with a error status instead of just ignore !
417
				$document->getIndex()
418
						 ->setStatus(Index::INDEX_IGNORE);
419
				echo 'Exception: ' . json_encode($e->getTrace()) . ' - ' . $e->getMessage() . "\n";
420
			}
421
422
			$index[] = $document;
423
		}
424
425
		return $index;
426
	}
427
428
429
	/**
430
	 * @param Index $index
431
	 *
432
	 * @return FilesDocument
433
	 * @throws FileIsNotIndexableException
434
	 * @throws InvalidPathException
435
	 * @throws NotFoundException
436
	 * @throws NotPermittedException
437
	 */
438
	private function generateDocumentFromIndex(Index $index) {
439
		$file = $this->getFileFromId($index->getOwnerId(), $index->getDocumentId());
440
441
		if ($file === null) {
442
			$index->setStatus(Index::INDEX_REMOVE);
443
			$document = new FilesDocument($index->getProviderId(), $index->getDocumentId());
444
			$document->setIndex($index);
445
446
			return $document;
447
		}
448
449
		$document = $this->generateFilesDocumentFromFile($file, $index->getOwnerId());
450
		$document->setIndex($index);
451
452
		$this->updateFilesDocumentFromFile($document, $file);
453
454
		return $document;
455
	}
456
457
458
	/**
459
	 * @param IndexDocument $document
460
	 *
461
	 * @return bool
462
	 */
463
	public function isDocumentUpToDate($document) {
464
		$index = $document->getIndex();
465
466
		if ($index->getStatus() !== Index::INDEX_OK) {
467
			return false;
468
		}
469
470
		if ($index->getLastIndex() >= $document->getModifiedTime()) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $index->getLastIn...ent->getModifiedTime();.
Loading history...
471
			return true;
472
		}
473
474
		return false;
475
	}
476
477
478
	/**
479
	 * @param Index $index
480
	 *
481
	 * @return FilesDocument
0 ignored issues
show
Documentation introduced by
Should the return type not be FilesDocument|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
482
	 * @throws InvalidPathException
483
	 * @throws NotFoundException
484
	 * @throws NotPermittedException
485
	 */
486
	public function updateDocument(Index $index) {
487
		try {
488
			$document = $this->generateDocumentFromIndex($index);
489
490
			return $document;
491
		} catch (FileIsNotIndexableException $e) {
492
			return null;
493
		}
494
	}
495
496
497
	/**
498
	 * @param FilesDocument $document
499
	 *
500
	 * @throws InvalidPathException
501
	 * @throws NotFoundException
502
	 * @throws NotPermittedException
503
	 */
504
	private function updateDocumentFromFilesDocument(FilesDocument $document) {
505
		$userFolder = $this->rootFolder->getUserFolder($document->getViewerId());
506
		$file = $userFolder->get($document->getPath());
507
508
		try {
509
			$this->updateFilesDocumentFromFile($document, $file);
510
		} catch (FileIsNotIndexableException $e) {
511
			$document->getIndex()
512
					 ->setStatus(Index::INDEX_IGNORE);
513
		}
514
	}
515
516
517
	/**
518
	 * @param FilesDocument $document
519
	 * @param Node $file
520
	 *
521
	 * @throws InvalidPathException
522
	 * @throws NotFoundException
523
	 * @throws NotPermittedException
524
	 */
525
	private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) {
526
		$this->updateDocumentAccess($document, $file);
527
		$this->updateShareNames($document, $file);
528
		$this->updateContentFromFile($document, $file);
529
530
		$document->addTag($document->getSource());
531
	}
532
533
534
	/**
535
	 * @param FilesDocument $document
536
	 * @param Node $file
537
	 */
538
	private function updateDocumentAccess(FilesDocument $document, Node $file) {
539
540
		$index = $document->getIndex();
541
		if (!$index->isStatus(Index::INDEX_FULL)
542
			&& !$index->isStatus(FilesDocument::STATUS_FILE_ACCESS)) {
543
			return;
544
		}
545
546
		$this->localFilesService->updateDocumentAccess($document, $file);
547
		$this->externalFilesService->updateDocumentAccess($document, $file);
548
		$this->groupFoldersService->updateDocumentAccess($document, $file);
549
		//$document->setAccess($access);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
550
551
//		$this->updateDocumentWithLocalFiles($document, $file);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
552
//		$this->groupFoldersService->updateDocumentWithExternalFiles($document, $file);
553
//		$this->externalFilesService->updateDocumentWithExternalFiles($document, $file);
554
	}
555
556
557
	/**
558
	 * @param FilesDocument $document
559
	 * @param Node $file
560
	 *
561
	 * @throws InvalidPathException
562
	 * @throws NotFoundException
563
	 * @throws NotPermittedException
564
	 */
565
	private function updateContentFromFile(FilesDocument $document, Node $file) {
566
567
		$document->setTitle($document->getPath());
568
569
		if (!$document->getIndex()
570
					  ->isStatus(Index::INDEX_CONTENT)
571
			|| $file->getType() !== FileInfo::TYPE_FILE) {
572
			return;
573
		}
574
575
		/** @var File $file */
576
		if ($file->getSize() <
577
			($this->configService->getAppValue(ConfigService::FILES_SIZE) * 1024 * 1024)) {
578
			$this->extractContentFromFileText($document, $file);
579
			$this->extractContentFromFileOffice($document, $file);
580
			$this->extractContentFromFilePDF($document, $file);
581
		}
582
583
		if ($document->getContent() === null) {
584
			$document->getIndex()
585
					 ->unsetStatus(Index::INDEX_CONTENT);
586
		}
587
	}
588
589
590
	/**
591
	 * @param FilesDocument $document
592
	 * @param Node $file
593
	 *
594
	 * @return array
595
	 */
596
	private function updateShareNames(FilesDocument $document, Node $file) {
597
		$users = [];
598
599
		$this->localFilesService->getShareUsers($document, $file, $users);
600
		$this->externalFilesService->getShareUsers($document, $file, $users);
601
//		$this->groupFoldersService->getShareUsers($document, $file, $users);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
602
603
		$shareNames = [];
604
		foreach ($users as $user) {
605
			try {
606
				$shareNames[$user] = $this->getPathFromViewerId($file->getId(), $user);
607
			} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
608
			}
609
		}
610
611
		$document->setInfo('share_names', $shareNames);
612
613
//			if ($file->getStorage()
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
614
//					 ->isLocal() === false) {
615
//				$shares = $this->externalFilesService->getAllSharesFromExternalFile($access);
616
//			} else {
617
//				$shares = $this->getAllSharesFromFile($file);
618
//			}
619
//
620
//			foreach ($shares as $user) {
621
//				try {
622
//					$shareNames[$user] = $this->getPathFromViewerId($file->getId(), $user);
623
//				} catch (Exception $e) {
624
//				}
625
//			}
626
//
627
		return $shareNames;
628
629
	}
630
631
	/**
632
	 * @param int $fileId
633
	 *
634
	 * @return string
635
	 */
636
	private function getWebdavId($fileId) {
637
		$instanceId = $this->configService->getSystemValue('instanceid');
638
639
		return sprintf("%08s", $fileId) . $instanceId;
640
	}
641
642
643
	/**
644
	 * @param string $mimeType
645
	 *
646
	 * @return string
647
	 */
648
	private function parseMimeType($mimeType) {
649
650
		// text file
651
		if ($mimeType === 'application/octet-stream'
652
			|| substr($mimeType, 0, 5) === 'text/') {
653
			return self::MIMETYPE_TEXT;
654
		}
655
656
		// PDF file
657
		if ($mimeType === 'application/pdf') {
658
			return self::MIMETYPE_PDF;
659
		}
660
661
		// Office file
662
		$officeMimes = [
663
			'application/msword',
664
			'application/vnd.oasis.opendocument',
665
			'application/vnd.sun.xml',
666
			'application/vnd.openxmlformats-officedocument',
667
			'application/vnd.ms-word',
668
			'application/vnd.ms-powerpoint',
669
			'application/vnd.ms-excel'
670
		];
671
672
		foreach ($officeMimes as $mime) {
673
			if (strpos($mimeType, $mime) === 0) {
674
				return self::MIMETYPE_OFFICE;
675
			}
676
		}
677
678
		return '';
679
	}
680
681
682
	/**
683
	 * @param FilesDocument $document
684
	 * @param File $file
685
	 *
686
	 * @throws NotPermittedException
687
	 */
688
	private function extractContentFromFileText(FilesDocument $document, File $file) {
689
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_TEXT) {
690
			return;
691
		}
692
693
		// on simple text file, elastic search+attachment pipeline can still detect language, useful ?
694
//		$document->setContent($file->getContent(), IndexDocument::NOT_ENCODED);
695
696
		// We try to avoid error with some base encoding of the document:
697
		$document->setContent(base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64);
698
	}
699
700
701
	/**
702
	 * @param FilesDocument $document
703
	 * @param File $file
704
	 *
705
	 * @throws NotPermittedException
706
	 */
707 View Code Duplication
	private function extractContentFromFilePDF(FilesDocument $document, File $file) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
708
709
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_PDF) {
710
			return;
711
		}
712
713
		if ($this->configService->getAppValue('files_pdf') !== '1') {
714
			return;
715
		}
716
717
		$document->setContent(base64_encode($file->getContent()), 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 extractContentFromFileOffice(FilesDocument $document, File $file) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
728
729
		if ($this->configService->getAppValue('files_office') !== '1') {
730
			return;
731
		}
732
733
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_OFFICE) {
734
			return;
735
		}
736
737
		$document->setContent(base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64);
738
	}
739
740
741
}
742