Completed
Push — master ( 679959...72d37e )
by Maxence
02:10
created

FilesService::generateDocumentFromIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
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 OC\App\AppManager;
32
use OCA\Files_FullTextSearch\Exceptions\EmptyUserException;
33
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
34
use OCA\Files_FullTextSearch\Exceptions\FilesNotFoundException;
35
use OCA\Files_FullTextSearch\Exceptions\KnownFileMimeTypeException;
36
use OCA\Files_FullTextSearch\Exceptions\KnownFileSourceException;
37
use OCA\Files_FullTextSearch\Model\FilesDocument;
38
use OCA\Files_FullTextSearch\Provider\FilesProvider;
39
use OCA\FullTextSearch\Exceptions\InterruptException;
40
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
41
use OCA\FullTextSearch\Model\Index;
42
use OCA\FullTextSearch\Model\IndexDocument;
43
use OCA\FullTextSearch\Model\IndexOptions;
44
use OCA\FullTextSearch\Model\Runner;
45
use OCP\AppFramework\IAppContainer;
46
use OCP\Files\File;
47
use OCP\Files\FileInfo;
48
use OCP\Files\Folder;
49
use OCP\Files\InvalidPathException;
50
use OCP\Files\IRootFolder;
51
use OCP\Files\Node;
52
use OCP\Files\NotFoundException;
53
use OCP\Files\NotPermittedException;
54
use OCP\Files\StorageNotAvailableException;
55
use OCP\IUserManager;
56
use OCP\Share\IManager;
57
58
class FilesService {
59
60
	const MIMETYPE_TEXT = 'files_text';
61
	const MIMETYPE_PDF = 'files_pdf';
62
	const MIMETYPE_OFFICE = 'files_office';
63
	const MIMETYPE_OCR = 'files_ocr';
64
	const MIMETYPE_IMAGE = 'files_image';
65
	const MIMETYPE_AUDIO = 'files_audio';
66
67
68
	/** @var IAppContainer */
69
	private $container;
70
71
	/** @var IRootFolder */
72
	private $rootFolder;
73
74
	/** @var IUserManager */
75
	private $userManager;
76
77
	/** @var AppManager */
78
	private $appManager;
79
80
	/** @var IManager */
81
	private $shareManager;
82
83
	/** @var ConfigService */
84
	private $configService;
85
86
	/** @var LocalFilesService */
87
	private $localFilesService;
88
89
	/** @var ExternalFilesService */
90
	private $externalFilesService;
91
92
	/** @var GroupFoldersService */
93
	private $groupFoldersService;
94
95
	/** @var ExtensionService */
96
	private $extensionService;
97
98
	/** @var MiscService */
99
	private $miscService;
100
101
102
	/**
103
	 * FilesService constructor.
104
	 *
105
	 * @param IAppContainer $container
106
	 * @param IRootFolder $rootFolder
107
	 * @param AppManager $appManager
108
	 * @param IUserManager $userManager
109
	 * @param IManager $shareManager
110
	 * @param ConfigService $configService
111
	 * @param LocalFilesService $localFilesService
112
	 * @param ExternalFilesService $externalFilesService
113
	 * @param GroupFoldersService $groupFoldersService
114
	 * @param ExtensionService $extensionService
115
	 * @param MiscService $miscService
116
	 *
117
	 * @internal param IProviderFactory $factory
118
	 */
119
	public function __construct(
120
		IAppContainer $container, IRootFolder $rootFolder, AppManager $appManager,
121
		IUserManager $userManager, IManager $shareManager,
122
		ConfigService $configService, LocalFilesService $localFilesService,
123
		ExternalFilesService $externalFilesService, GroupFoldersService $groupFoldersService,
124
		ExtensionService $extensionService, MiscService $miscService
125
	) {
126
		$this->container = $container;
127
		$this->rootFolder = $rootFolder;
128
		$this->appManager = $appManager;
129
		$this->userManager = $userManager;
130
		$this->shareManager = $shareManager;
131
132
		$this->configService = $configService;
133
		$this->localFilesService = $localFilesService;
134
		$this->externalFilesService = $externalFilesService;
135
		$this->groupFoldersService = $groupFoldersService;
136
		$this->extensionService = $extensionService;
137
138
		$this->miscService = $miscService;
139
	}
140
141
142
	/**
143
	 * @param Runner $runner
144
	 * @param string $userId
145
	 * @param IndexOptions $indexOptions
146
	 *
147
	 * @return FilesDocument[]
148
	 * @throws InterruptException
149
	 * @throws InvalidPathException
150
	 * @throws NotFoundException
151
	 * @throws TickDoesNotExistException
152
	 */
153
	public function getFilesFromUser(Runner $runner, $userId, $indexOptions) {
154
155
		$this->initFileSystems($userId);
156
157
		/** @var Folder $files */
158
		$files = $this->rootFolder->getUserFolder($userId)
159
								  ->get($indexOptions->getOption('path', '/'));
160
161
		if ($files instanceof Folder) {
0 ignored issues
show
Bug introduced by
The class OCP\Files\Folder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
162
			$result = $this->getFilesFromDirectory($runner, $userId, $files);
163
		} else {
164
			$result = [];
165
			try {
166
				$result[] = $this->generateFilesDocumentFromFile($userId, $files);
167
			} catch (FileIsNotIndexableException $e) {
168
				/** we do nothin' */
169
			}
170
		}
171
172
		return $result;
173
	}
174
175
176
	/**
177
	 * @param string $userId
178
	 */
179
	private function initFileSystems($userId) {
180
		if ($userId === '') {
181
			return;
182
		}
183
184
		$this->externalFilesService->initExternalFilesForUser($userId);
185
		$this->groupFoldersService->initGroupSharesForUser($userId);
186
	}
187
188
189
	/**
190
	 * @param Runner $runner
191
	 * @param string $userId
192
	 * @param Folder $node
193
	 *
194
	 * @return FilesDocument[]
195
	 * @throws InterruptException
196
	 * @throws InvalidPathException
197
	 * @throws NotFoundException
198
	 * @throws TickDoesNotExistException
199
	 */
200
	public function getFilesFromDirectory(Runner $runner, $userId, Folder $node) {
201
		$documents = [];
202
203
		try {
204
			if ($node->nodeExists('.noindex')) {
205
				return $documents;
206
			}
207
		} 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...
208
			return $documents;
209
		}
210
211
		$files = $node->getDirectoryListing();
212
		foreach ($files as $file) {
213
			$runner->updateAction('getFilesFromDirectory');
214
215
			try {
216
				$documents[] = $this->generateFilesDocumentFromFile($userId, $file);
217
			} catch (FileIsNotIndexableException $e) {
218
				continue;
219
			}
220
221
			if ($file->getType() === FileInfo::TYPE_FOLDER) {
222
				/** @var $file Folder */
223
				$documents =
224
					array_merge($documents, $this->getFilesFromDirectory($runner, $userId, $file));
225
			}
226
		}
227
228
		return $documents;
229
	}
230
231
232
	/**
233
	 * @param Node $file
234
	 *
235
	 * @param string $viewerId
236
	 *
237
	 * @return FilesDocument
238
	 * @throws FileIsNotIndexableException
239
	 * @throws InvalidPathException
240
	 * @throws NotFoundException
241
	 * @throws Exception
242
	 */
243
	private function generateFilesDocumentFromFile($viewerId, Node $file) {
244
245
		$source = $this->getFileSource($file);
246
		$document = new FilesDocument(FilesProvider::FILES_PROVIDER_ID, $file->getId());
247
248
		$ownerId = '';
249
		if ($file->getOwner() !== null) {
250
			$ownerId = $file->getOwner()
251
							->getUID();
252
		}
253
		$document->setType($file->getType())
254
				 ->setSource($source)
255
				 ->setOwnerId($ownerId)
256
				 ->setPath($this->getPathFromViewerId($file->getId(), $viewerId))
257
				 ->setViewerId($viewerId)
258
				 ->setModifiedTime($file->getMTime())
259
				 ->setMimetype($file->getMimetype());
260
261
		return $document;
262
	}
263
264
265
	/**
266
	 * @param Node $file
267
	 *
268
	 * @return string
269
	 * @throws FileIsNotIndexableException
270
	 * @throws NotFoundException
271
	 */
272
	private function getFileSource(Node $file) {
273
		$source = '';
274
275
		try {
276
			$this->localFilesService->getFileSource($file, $source);
277
			$this->externalFilesService->getFileSource($file, $source);
278
			$this->groupFoldersService->getFileSource($file, $source);
279
		} catch (KnownFileSourceException $e) {
280
			/** we know the source, just leave. */
281
		}
282
283
		return $source;
284
	}
285
286
287
	/**
288
	 * @param string $userId
289
	 * @param string $path
290
	 *
291
	 * @return Node
292
	 * @throws NotFoundException
293
	 */
294
	public function getFileFromPath($userId, $path) {
295
		return $this->rootFolder->getUserFolder($userId)
296
								->get($path);
297
	}
298
299
300
	/**
301
	 * @param string $userId
302
	 * @param int $fileId
303
	 *
304
	 * @return Node
305
	 * @throws FilesNotFoundException
306
	 * @throws EmptyUserException
307
	 */
308
	public function getFileFromId($userId, $fileId) {
309
310
		if ($userId === '') {
311
			throw new EmptyUserException();
312
		}
313
314
		$files = $this->rootFolder->getUserFolder($userId)
315
								  ->getById($fileId);
316
		if (sizeof($files) === 0) {
317
			throw new FilesNotFoundException();
318
		}
319
320
		$file = array_shift($files);
321
322
		return $file;
323
	}
324
325
326
	/**
327
	 * @param Index $index
328
	 *
329
	 * @return Node
330
	 * @throws EmptyUserException
331
	 * @throws FilesNotFoundException
332
	 */
333
	public function getFileFromIndex(Index $index) {
334
		$this->impersonateOwner($index);
335
336
		return $this->getFileFromId($index->getOwnerId(), $index->getDocumentId());
337
	}
338
339
340
	/**
341
	 * @param int $fileId
342
	 * @param string $viewerId
343
	 *
344
	 * @throws Exception
345
	 * @return string
346
	 */
347
	private function getPathFromViewerId($fileId, $viewerId) {
348
349
		$viewerFiles = $this->rootFolder->getUserFolder($viewerId)
350
										->getById($fileId);
351
352
		if (sizeof($viewerFiles) === 0) {
353
			return '';
354
		}
355
356
		$file = array_shift($viewerFiles);
357
358
		// TODO: better way to do this : we remove the '/userid/files/'
359
		$path = MiscService::noEndSlash(substr($file->getPath(), 8 + strlen($viewerId)));
360
361
		return $path;
362
	}
363
364
365
	/**
366
	 * @param FilesDocument $document
367
	 */
368
	public function generateDocument(FilesDocument $document) {
369
370
		try {
371
			$this->updateFilesDocument($document);
372
		} catch (Exception $e) {
373
			// TODO - update $document with a error status instead of just ignore !
374
			$document->getIndex()
375
					 ->setStatus(Index::INDEX_IGNORE);
376
			echo 'Exception: ' . json_encode($e->getTrace()) . ' - ' . $e->getMessage()
377
				 . "\n";
378
		}
379
	}
380
381
382
	/**
383
	 * @param Index $index
384
	 *
385
	 * @return FilesDocument
386
	 * @throws FileIsNotIndexableException
387
	 * @throws InvalidPathException
388
	 * @throws NotFoundException
389
	 * @throws NotPermittedException
390
	 */
391
	private function generateDocumentFromIndex(Index $index) {
392
393
		try {
394
			$file = $this->getFileFromIndex($index);
395
		} catch (Exception $e) {
396
			$index->setStatus(Index::INDEX_REMOVE);
397
			$document = new FilesDocument($index->getProviderId(), $index->getDocumentId());
398
			$document->setIndex($index);
399
400
			return $document;
401
		}
402
403
		$document = $this->generateFilesDocumentFromFile($index->getOwnerId(), $file);
404
		$document->setIndex($index);
405
406
		$this->updateFilesDocumentFromFile($document, $file);
407
408
		return $document;
409
	}
410
411
412
	/**
413
	 * @param IndexDocument $document
414
	 *
415
	 * @return bool
416
	 */
417
	public function isDocumentUpToDate($document) {
418
		$index = $document->getIndex();
419
420
		if (!$this->configService->compareIndexOptions($index)) {
421
			$index->setStatus(Index::INDEX_CONTENT);
422
			$document->setIndex($index);
423
424
			return false;
425
		}
426
427
		if ($index->getStatus() !== Index::INDEX_OK) {
428
			return false;
429
		}
430
431
		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...
432
			return true;
433
		}
434
435
		return false;
436
	}
437
438
439
	/**
440
	 * @param Index $index
441
	 *
442
	 * @return FilesDocument
443
	 * @throws InvalidPathException
444
	 * @throws NotFoundException
445
	 * @throws NotPermittedException
446
	 * @throws FileIsNotIndexableException
447
	 */
448
	public function updateDocument(Index $index) {
449
		$this->impersonateOwner($index);
450
		$this->initFileSystems($index->getOwnerId());
451
452
		return $this->generateDocumentFromIndex($index);
453
	}
454
455
456
	/**
457
	 * @param FilesDocument $document
458
	 *
459
	 * @throws InvalidPathException
460
	 * @throws NotFoundException
461
	 * @throws NotPermittedException
462
	 */
463
	private function updateFilesDocument(FilesDocument $document) {
464
		$userFolder = $this->rootFolder->getUserFolder($document->getViewerId());
465
		$file = $userFolder->get($document->getPath());
466
467
		try {
468
			$this->updateFilesDocumentFromFile($document, $file);
469
		} catch (FileIsNotIndexableException $e) {
470
			$document->getIndex()
471
					 ->setStatus(Index::INDEX_IGNORE);
472
		}
473
	}
474
475
476
	/**
477
	 * @param FilesDocument $document
478
	 * @param Node $file
479
	 *
480
	 * @throws InvalidPathException
481
	 * @throws NotFoundException
482
	 * @throws NotPermittedException
483
	 */
484
	private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) {
485
486
		$document->getIndex()
487
				 ->setSource($document->getSource());
488
489
		$this->updateDocumentAccess($document, $file);
490
		$this->updateContentFromFile($document, $file);
491
492
		$document->addMetaTag($document->getSource());
493
494
		$this->extensionService->fileIndexing($document, $file);
495
	}
496
497
498
	/**
499
	 * @param FilesDocument $document
500
	 * @param Node $file
501
	 */
502
	private function updateDocumentAccess(FilesDocument $document, Node $file) {
503
504
		$index = $document->getIndex();
505
506
		if (!$index->isStatus(Index::INDEX_FULL)
507
			&& !$index->isStatus(FilesDocument::STATUS_FILE_ACCESS)) {
508
			return;
509
		}
510
511
		$this->localFilesService->updateDocumentAccess($document, $file);
512
		$this->externalFilesService->updateDocumentAccess($document, $file);
513
		$this->groupFoldersService->updateDocumentAccess($document, $file);
514
515
		$this->updateShareNames($document, $file);
516
	}
517
518
519
	/**
520
	 * @param FilesDocument $document
521
	 * @param Node $file
522
	 *
523
	 * @throws InvalidPathException
524
	 * @throws NotFoundException
525
	 * @throws NotPermittedException
526
	 */
527
	private function updateContentFromFile(FilesDocument $document, Node $file) {
528
529
		$document->setTitle($document->getPath());
530
531
		if (!$document->getIndex()
532
					  ->isStatus(Index::INDEX_CONTENT)
533
			|| $file->getType() !== FileInfo::TYPE_FILE) {
534
			return;
535
		}
536
537
		/** @var File $file */
538
		if ($file->getSize() <
539
			($this->configService->getAppValue(ConfigService::FILES_SIZE) * 1024 * 1024)) {
540
			$this->extractContentFromFileText($document, $file);
541
			$this->extractContentFromFileOffice($document, $file);
542
			$this->extractContentFromFilePDF($document, $file);
543
		}
544
545
		if ($document->getContent() === null) {
546
			$document->getIndex()
547
					 ->unsetStatus(Index::INDEX_CONTENT);
548
		}
549
	}
550
551
552
	/**
553
	 * @param FilesDocument $document
554
	 * @param Node $file
555
	 *
556
	 * @return array
557
	 */
558
	private function updateShareNames(FilesDocument $document, Node $file) {
559
560
		$users = [];
561
562
		$this->localFilesService->getShareUsersFromFile($file, $users);
563
		$this->externalFilesService->getShareUsers($document, $users);
564
		$this->groupFoldersService->getShareUsers($document, $users);
565
566
		$shareNames = [];
567
		foreach ($users as $username) {
568
			try {
569
				$user = $this->userManager->get($username);
570
				if ($user === null || $user->getLastLogin() === 0) {
571
					continue;
572
				}
573
574
				$path = $this->getPathFromViewerId($file->getId(), $username);
575
				$shareNames[MiscService::secureUsername($username)] =
576
					(!is_string($path)) ? $path = '' : $path;
577
578
			} catch (Exception $e) {
579
				$this->miscService->log(
580
					'Issue while getting information on documentId:' . $document->getId(), 0
581
				);
582
			}
583
		}
584
585
		$document->setInfo('share_names', $shareNames);
586
587
		return $shareNames;
588
	}
589
590
591
	/**
592
	 * @param string $mimeType
593
	 *
594
	 * @return string
595
	 */
596
	private function parseMimeType($mimeType) {
597
598
		$parsed = '';
599
		try {
600
			$this->parseMimeTypeText($mimeType, $parsed);
601
			$this->parseMimeTypePDF($mimeType, $parsed);
602
			$this->parseMimeTypeOffice($mimeType, $parsed);
603
		} catch (KnownFileMimeTypeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
604
		}
605
606
		return $parsed;
607
	}
608
609
610
	/**
611
	 * @param string $mimeType
612
	 * @param string $parsed
613
	 *
614
	 * @throws KnownFileMimeTypeException
615
	 */
616
	private function parseMimeTypeText($mimeType, &$parsed) {
617
618
		if (substr($mimeType, 0, 5) === 'text/') {
619
			$parsed = self::MIMETYPE_TEXT;
620
			throw new KnownFileMimeTypeException();
621
		}
622
623
		$textMimes = [
624
			'application/epub+zip'
625
		];
626
627 View Code Duplication
		foreach ($textMimes as $mime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
628
			if (strpos($mimeType, $mime) === 0) {
629
				$parsed = self::MIMETYPE_TEXT;
630
				throw new KnownFileMimeTypeException();
631
			}
632
		}
633
	}
634
635
636
	/**
637
	 * @param string $mimeType
638
	 * @param string $parsed
639
	 *
640
	 * @throws KnownFileMimeTypeException
641
	 */
642
	private function parseMimeTypePDF($mimeType, &$parsed) {
643
644
		if ($mimeType === 'application/pdf') {
645
			$parsed = self::MIMETYPE_PDF;
646
			throw new KnownFileMimeTypeException();
647
		}
648
	}
649
650
651
	/**
652
	 * @param string $mimeType
653
	 * @param string $parsed
654
	 *
655
	 * @throws KnownFileMimeTypeException
656
	 */
657
	private function parseMimeTypeOffice($mimeType, &$parsed) {
658
659
		$officeMimes = [
660
			'application/msword',
661
			'application/vnd.oasis.opendocument',
662
			'application/vnd.sun.xml',
663
			'application/vnd.openxmlformats-officedocument',
664
			'application/vnd.ms-word',
665
			'application/vnd.ms-powerpoint',
666
			'application/vnd.ms-excel'
667
		];
668
669 View Code Duplication
		foreach ($officeMimes as $mime) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
670
			if (strpos($mimeType, $mime) === 0) {
671
				$parsed = self::MIMETYPE_OFFICE;
672
				throw new KnownFileMimeTypeException();
673
			}
674
		}
675
	}
676
677
678
	/**
679
	 * @param FilesDocument $document
680
	 * @param File $file
681
	 *
682
	 * @throws NotPermittedException
683
	 */
684
	private function extractContentFromFileText(FilesDocument $document, File $file) {
685
686
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_TEXT) {
687
			return;
688
		}
689
690
		if (!$this->isSourceIndexable($document)) {
691
			return;
692
		}
693
694
		$document->setContent(
695
			base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64
696
		);
697
	}
698
699
700
	/**
701
	 * @param FilesDocument $document
702
	 * @param File $file
703
	 *
704
	 * @throws NotPermittedException
705
	 */
706 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...
707
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_PDF) {
708
			return;
709
		}
710
711
		$this->configService->setDocumentIndexOption($document, ConfigService::FILES_PDF);
712
		if (!$this->isSourceIndexable($document)) {
713
			return;
714
		}
715
716
		if ($this->configService->getAppValue(ConfigService::FILES_PDF) !== '1') {
717
			$document->setContent('');
718
719
			return;
720
		}
721
722
		$document->setContent(
723
			base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64
724
		);
725
	}
726
727
728
	/**
729
	 * @param FilesDocument $document
730
	 * @param File $file
731
	 *
732
	 * @throws NotPermittedException
733
	 */
734 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...
735
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_OFFICE) {
736
			return;
737
		}
738
739
		$this->configService->setDocumentIndexOption($document, ConfigService::FILES_OFFICE);
740
		if (!$this->isSourceIndexable($document)) {
741
			return;
742
		}
743
744
		if ($this->configService->getAppValue(ConfigService::FILES_OFFICE) !== '1') {
745
			$document->setContent('');
746
747
			return;
748
		}
749
750
		$document->setContent(
751
			base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64
752
		);
753
	}
754
755
756
	/**
757
	 * @param FilesDocument $document
758
	 *
759
	 * @return bool
760
	 */
761
	private function isSourceIndexable(FilesDocument $document) {
762
		$this->configService->setDocumentIndexOption($document, $document->getSource());
763
		if ($this->configService->getAppValue($document->getSource()) !== '1') {
764
			$document->setContent('');
765
766
			return false;
767
		}
768
769
		return true;
770
	}
771
772
773
	private function impersonateOwner(Index $index) {
774
		if ($index->getOwnerId() !== '') {
775
			return;
776
		}
777
778
		$this->groupFoldersService->impersonateOwner($index);
779
		$this->externalFilesService->impersonateOwner($index);
780
	}
781
782
}
783
784