Completed
Push — master ( 7736c6...b4978d )
by Maxence
03:13
created

FilesService::updateFilesDocument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.9
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\Model\Index;
40
use OCA\FullTextSearch\Model\IndexDocument;
41
use OCA\FullTextSearch\Model\IndexOptions;
42
use OCA\FullTextSearch\Model\Runner;
43
use OCP\AppFramework\IAppContainer;
44
use OCP\Files\File;
45
use OCP\Files\FileInfo;
46
use OCP\Files\Folder;
47
use OCP\Files\InvalidPathException;
48
use OCP\Files\IRootFolder;
49
use OCP\Files\Node;
50
use OCP\Files\NotFoundException;
51
use OCP\Files\NotPermittedException;
52
use OCP\Files\StorageNotAvailableException;
53
use OCP\IUserManager;
54
use OCP\Share\IManager;
55
56
class FilesService {
57
58
	const MIMETYPE_TEXT = 'files_text';
59
	const MIMETYPE_PDF = 'files_pdf';
60
	const MIMETYPE_OFFICE = 'files_office';
61
	const MIMETYPE_IMAGE = 'files_image';
62
	const MIMETYPE_AUDIO = 'files_audio';
63
64
65
	/** @var IAppContainer */
66
	private $container;
67
68
	/** @var IRootFolder */
69
	private $rootFolder;
70
71
	/** @var IUserManager */
72
	private $userManager;
73
74
	/** @var AppManager */
75
	private $appManager;
76
77
	/** @var IManager */
78
	private $shareManager;
79
80
	/** @var ConfigService */
81
	private $configService;
82
83
	/** @var LocalFilesService */
84
	private $localFilesService;
85
86
	/** @var ExternalFilesService */
87
	private $externalFilesService;
88
89
	/** @var GroupFoldersService */
90
	private $groupFoldersService;
91
92
	/** @var ExtensionService */
93
	private $extensionService;
94
95
	/** @var MiscService */
96
	private $miscService;
97
98
99
	/** @var Runner */
100
	private $runner;
101
102
	/** @var int */
103
	private $sumDocuments;
104
105
	/**
106
	 * FilesService constructor.
107
	 *
108
	 * @param IAppContainer $container
109
	 * @param IRootFolder $rootFolder
110
	 * @param AppManager $appManager
111
	 * @param IUserManager $userManager
112
	 * @param IManager $shareManager
113
	 * @param ConfigService $configService
114
	 * @param LocalFilesService $localFilesService
115
	 * @param ExternalFilesService $externalFilesService
116
	 * @param GroupFoldersService $groupFoldersService
117
	 * @param ExtensionService $extensionService
118
	 * @param MiscService $miscService
119
	 *
120
	 * @internal param IProviderFactory $factory
121
	 */
122
	public function __construct(
123
		IAppContainer $container, IRootFolder $rootFolder, AppManager $appManager,
124
		IUserManager $userManager, IManager $shareManager,
125
		ConfigService $configService, LocalFilesService $localFilesService,
126
		ExternalFilesService $externalFilesService, GroupFoldersService $groupFoldersService,
127
		ExtensionService $extensionService, MiscService $miscService
128
	) {
129
		$this->container = $container;
130
		$this->rootFolder = $rootFolder;
131
		$this->appManager = $appManager;
132
		$this->userManager = $userManager;
133
		$this->shareManager = $shareManager;
134
135
		$this->configService = $configService;
136
		$this->localFilesService = $localFilesService;
137
		$this->externalFilesService = $externalFilesService;
138
		$this->groupFoldersService = $groupFoldersService;
139
		$this->extensionService = $extensionService;
140
141
		$this->miscService = $miscService;
142
	}
143
144
145
	public function setRunner(Runner $runner) {
146
		$this->runner = $runner;
147
	}
148
149
150
	/**
151
	 * @param string $userId
152
	 * @param IndexOptions $indexOptions
153
	 *
154
	 * @return FilesDocument[]
155
	 * @throws InvalidPathException
156
	 * @throws NotFoundException
157
	 */
158
	public function getFilesFromUser($userId, $indexOptions) {
159
160
		$this->initFileSystems($userId);
161
		$this->sumDocuments = 0;
162
163
		/** @var Folder $files */
164
		$files = $this->rootFolder->getUserFolder($userId)
165
								  ->get($indexOptions->getOption('path', '/'));
166
167
		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...
168
			$result = $this->getFilesFromDirectory($userId, $files);
169
		} else {
170
			$result = [];
171
			try {
172
				$result[] = $this->generateFilesDocumentFromFile($userId, $files);
173
			} catch (FileIsNotIndexableException $e) {
174
				/** we do nothin' */
175
			}
176
		}
177
178
		return $result;
179
	}
180
181
182
	/**
183
	 * @param string $userId
184
	 */
185
	private function initFileSystems($userId) {
186
		if ($userId === '') {
187
			return;
188
		}
189
190
		$this->externalFilesService->initExternalFilesForUser($userId);
191
		$this->groupFoldersService->initGroupSharesForUser($userId);
192
	}
193
194
195
	/**
196
	 * @param string $userId
197
	 * @param Folder $node
198
	 *
199
	 * @return FilesDocument[]
200
	 * @throws InvalidPathException
201
	 * @throws NotFoundException
202
	 * @throws Exception
203
	 */
204
	public function getFilesFromDirectory($userId, Folder $node) {
205
		$documents = [];
206
207
		$this->updateRunnerAction('generateIndexFiles');
208
		$this->updateRunnerInfo(
209
			[
210
				'info'          => $node->getPath(),
211
				'documentTotal' => $this->sumDocuments
212
			]
213
		);
214
215
		try {
216
			if ($node->nodeExists('.noindex')) {
217
				return $documents;
218
			}
219
		} 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...
220
			return $documents;
221
		}
222
223
		$files = $node->getDirectoryListing();
224
		foreach ($files as $file) {
225
226
			try {
227
				$documents[] = $this->generateFilesDocumentFromFile($userId, $file);
228
				$this->sumDocuments++;
229
			} catch (FileIsNotIndexableException $e) {
230
				continue;
231
			}
232
233
			if ($file->getType() === FileInfo::TYPE_FOLDER) {
234
				/** @var $file Folder */
235
				$documents =
236
					array_merge($documents, $this->getFilesFromDirectory($userId, $file));
237
			}
238
		}
239
240
		return $documents;
241
	}
242
243
244
	/**
245
	 * @param Node $file
246
	 *
247
	 * @param string $viewerId
248
	 *
249
	 * @return FilesDocument
250
	 * @throws FileIsNotIndexableException
251
	 * @throws InvalidPathException
252
	 * @throws NotFoundException
253
	 * @throws Exception
254
	 */
255
	private function generateFilesDocumentFromFile($viewerId, Node $file) {
256
257
		$source = $this->getFileSource($file);
258
		$document = new FilesDocument(FilesProvider::FILES_PROVIDER_ID, $file->getId());
259
260
		if ($file->getId() === -1) {
261
			throw new FileIsNotIndexableException();
262
		}
263
264
		$ownerId = '';
265
		if ($file->getOwner() !== null) {
266
			$ownerId = $file->getOwner()
267
							->getUID();
268
		}
269
		$document->setType($file->getType())
270
				 ->setSource($source)
271
				 ->setOwnerId($ownerId)
272
				 ->setPath($this->getPathFromViewerId($file->getId(), $viewerId))
273
				 ->setViewerId($viewerId)
274
				 ->setModifiedTime($file->getMTime())
275
				 ->setMimetype($file->getMimetype());
276
277
		return $document;
278
	}
279
280
281
	/**
282
	 * @param Node $file
283
	 *
284
	 * @return string
285
	 * @throws FileIsNotIndexableException
286
	 * @throws NotFoundException
287
	 */
288
	private function getFileSource(Node $file) {
289
		$source = '';
290
291
		try {
292
			$this->localFilesService->getFileSource($file, $source);
293
			$this->externalFilesService->getFileSource($file, $source);
294
			$this->groupFoldersService->getFileSource($file, $source);
295
		} catch (KnownFileSourceException $e) {
296
			/** we know the source, just leave. */
297
		}
298
299
		return $source;
300
	}
301
302
303
	/**
304
	 * @param string $userId
305
	 * @param string $path
306
	 *
307
	 * @return Node
308
	 * @throws NotFoundException
309
	 */
310
	public function getFileFromPath($userId, $path) {
311
		return $this->rootFolder->getUserFolder($userId)
312
								->get($path);
313
	}
314
315
316
	/**
317
	 * @param string $userId
318
	 * @param int $fileId
319
	 *
320
	 * @return Node
321
	 * @throws FilesNotFoundException
322
	 * @throws EmptyUserException
323
	 */
324
	public function getFileFromId($userId, $fileId) {
325
326
		if ($userId === '') {
327
			throw new EmptyUserException();
328
		}
329
330
		$files = $this->rootFolder->getUserFolder($userId)
331
								  ->getById($fileId);
332
		if (sizeof($files) === 0) {
333
			throw new FilesNotFoundException();
334
		}
335
336
		$file = array_shift($files);
337
338
		return $file;
339
	}
340
341
342
	/**
343
	 * @param Index $index
344
	 *
345
	 * @return Node
346
	 * @throws EmptyUserException
347
	 * @throws FilesNotFoundException
348
	 */
349
	public function getFileFromIndex(Index $index) {
350
		$this->impersonateOwner($index);
351
352
		return $this->getFileFromId($index->getOwnerId(), $index->getDocumentId());
353
	}
354
355
356
	/**
357
	 * @param int $fileId
358
	 * @param string $viewerId
359
	 *
360
	 * @throws Exception
361
	 * @return string
362
	 */
363
	private function getPathFromViewerId($fileId, $viewerId) {
364
365
		$viewerFiles = $this->rootFolder->getUserFolder($viewerId)
366
										->getById($fileId);
367
368
		if (sizeof($viewerFiles) === 0) {
369
			return '';
370
		}
371
372
		$file = array_shift($viewerFiles);
373
374
		// TODO: better way to do this : we remove the '/userid/files/'
375
		$path = MiscService::noEndSlash(substr($file->getPath(), 8 + strlen($viewerId)));
376
377
		return $path;
378
	}
379
380
381
	/**
382
	 * @param FilesDocument $document
383
	 */
384
	public function generateDocument(FilesDocument $document) {
385
386
		try {
387
			$this->updateFilesDocument($document);
388
		} catch (Exception $e) {
389
			// TODO - update $document with a error status instead of just ignore !
390
			$document->getIndex()
391
					 ->setStatus(Index::INDEX_IGNORE);
392
			echo 'Exception: ' . json_encode($e->getTrace()) . ' - ' . $e->getMessage()
393
				 . "\n";
394
		}
395
	}
396
397
398
	/**
399
	 * @param Index $index
400
	 *
401
	 * @return FilesDocument
402
	 * @throws FileIsNotIndexableException
403
	 * @throws InvalidPathException
404
	 * @throws NotFoundException
405
	 * @throws NotPermittedException
406
	 */
407
	private function generateDocumentFromIndex(Index $index) {
408
409
		try {
410
			$file = $this->getFileFromIndex($index);
411
		} catch (Exception $e) {
412
			$index->setStatus(Index::INDEX_REMOVE);
413
			$document = new FilesDocument($index->getProviderId(), $index->getDocumentId());
414
			$document->setIndex($index);
415
416
			return $document;
417
		}
418
419
		$document = $this->generateFilesDocumentFromFile($index->getOwnerId(), $file);
420
		$document->setIndex($index);
421
422
		$this->updateFilesDocumentFromFile($document, $file);
423
424
		return $document;
425
	}
426
427
428
	/**
429
	 * @param IndexDocument $document
430
	 *
431
	 * @return bool
432
	 */
433
	public function isDocumentUpToDate($document) {
434
		$index = $document->getIndex();
435
436
		if (!$this->configService->compareIndexOptions($index)) {
437
			$index->setStatus(Index::INDEX_CONTENT);
438
			$document->setIndex($index);
439
440
			return false;
441
		}
442
443
		if ($index->getStatus() !== Index::INDEX_OK) {
444
			return false;
445
		}
446
447
		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...
448
			return true;
449
		}
450
451
		return false;
452
	}
453
454
455
	/**
456
	 * @param Index $index
457
	 *
458
	 * @return FilesDocument
459
	 * @throws InvalidPathException
460
	 * @throws NotFoundException
461
	 * @throws NotPermittedException
462
	 * @throws FileIsNotIndexableException
463
	 */
464
	public function updateDocument(Index $index) {
465
		$this->impersonateOwner($index);
466
		$this->initFileSystems($index->getOwnerId());
467
468
		return $this->generateDocumentFromIndex($index);
469
	}
470
471
472
	/**
473
	 * @param FilesDocument $document
474
	 *
475
	 * @throws InvalidPathException
476
	 * @throws NotFoundException
477
	 * @throws NotPermittedException
478
	 */
479
	private function updateFilesDocument(FilesDocument $document) {
480
		$userFolder = $this->rootFolder->getUserFolder($document->getViewerId());
481
		$file = $userFolder->get($document->getPath());
482
483
		try {
484
			$this->updateFilesDocumentFromFile($document, $file);
485
		} catch (FileIsNotIndexableException $e) {
486
			$document->getIndex()
487
					 ->setStatus(Index::INDEX_IGNORE);
488
		}
489
	}
490
491
492
	/**
493
	 * @param FilesDocument $document
494
	 * @param Node $file
495
	 *
496
	 * @throws InvalidPathException
497
	 * @throws NotFoundException
498
	 * @throws NotPermittedException
499
	 */
500
	private function updateFilesDocumentFromFile(FilesDocument $document, Node $file) {
501
502
		$document->getIndex()
503
				 ->setSource($document->getSource());
504
505
		$this->updateDocumentAccess($document, $file);
506
		$this->updateContentFromFile($document, $file);
507
508
		$document->addMetaTag($document->getSource());
509
	}
510
511
512
	/**
513
	 * @param FilesDocument $document
514
	 * @param Node $file
515
	 */
516
	private function updateDocumentAccess(FilesDocument $document, Node $file) {
517
518
		$index = $document->getIndex();
519
520
		if (!$index->isStatus(Index::INDEX_FULL)
521
			&& !$index->isStatus(FilesDocument::STATUS_FILE_ACCESS)) {
522
			return;
523
		}
524
525
		$this->localFilesService->updateDocumentAccess($document, $file);
526
		$this->externalFilesService->updateDocumentAccess($document, $file);
527
		$this->groupFoldersService->updateDocumentAccess($document, $file);
528
529
		$this->updateShareNames($document, $file);
530
	}
531
532
533
	/**
534
	 * @param FilesDocument $document
535
	 * @param Node $file
536
	 *
537
	 * @throws InvalidPathException
538
	 * @throws NotFoundException
539
	 * @throws NotPermittedException
540
	 */
541
	private function updateContentFromFile(FilesDocument $document, Node $file) {
542
543
		$document->setTitle($document->getPath());
544
545
		if (!$document->getIndex()
546
					  ->isStatus(Index::INDEX_CONTENT)
547
			|| $file->getType() !== FileInfo::TYPE_FILE) {
548
			return;
549
		}
550
551
		/** @var File $file */
552
		if ($file->getSize() <
553
			($this->configService->getAppValue(ConfigService::FILES_SIZE) * 1024 * 1024)) {
554
			$this->extractContentFromFileText($document, $file);
555
			$this->extractContentFromFileOffice($document, $file);
556
			$this->extractContentFromFilePDF($document, $file);
557
558
			$this->extensionService->fileIndexing($document, $file);
559
		}
560
561
		if ($document->getContent() === null) {
562
			$document->getIndex()
563
					 ->unsetStatus(Index::INDEX_CONTENT);
564
		}
565
	}
566
567
568
	/**
569
	 * @param FilesDocument $document
570
	 * @param Node $file
571
	 *
572
	 * @return array
573
	 */
574
	private function updateShareNames(FilesDocument $document, Node $file) {
575
576
		$users = [];
577
578
		$this->localFilesService->getShareUsersFromFile($file, $users);
579
		$this->externalFilesService->getShareUsers($document, $users);
580
		$this->groupFoldersService->getShareUsers($document, $users);
581
582
		$shareNames = [];
583
		foreach ($users as $username) {
584
			try {
585
				$user = $this->userManager->get($username);
586
				if ($user === null || $user->getLastLogin() === 0) {
587
					continue;
588
				}
589
590
				$path = $this->getPathFromViewerId($file->getId(), $username);
591
				$shareNames[MiscService::secureUsername($username)] =
592
					(!is_string($path)) ? $path = '' : $path;
593
594
			} catch (Exception $e) {
595
				$this->miscService->log(
596
					'Issue while getting information on documentId:' . $document->getId(), 0
597
				);
598
			}
599
		}
600
601
		$document->setInfo('share_names', $shareNames);
602
603
		return $shareNames;
604
	}
605
606
607
	/**
608
	 * @param string $mimeType
609
	 *
610
	 * @return string
611
	 */
612
	private function parseMimeType($mimeType) {
613
614
		$parsed = '';
615
		try {
616
			$this->parseMimeTypeText($mimeType, $parsed);
617
			$this->parseMimeTypePDF($mimeType, $parsed);
618
			$this->parseMimeTypeOffice($mimeType, $parsed);
619
		} catch (KnownFileMimeTypeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
620
		}
621
622
		return $parsed;
623
	}
624
625
626
	/**
627
	 * @param string $mimeType
628
	 * @param string $parsed
629
	 *
630
	 * @throws KnownFileMimeTypeException
631
	 */
632
	private function parseMimeTypeText($mimeType, &$parsed) {
633
634
		if (substr($mimeType, 0, 5) === 'text/') {
635
			$parsed = self::MIMETYPE_TEXT;
636
			throw new KnownFileMimeTypeException();
637
		}
638
639
		$textMimes = [
640
			'application/epub+zip'
641
		];
642
643 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...
644
			if (strpos($mimeType, $mime) === 0) {
645
				$parsed = self::MIMETYPE_TEXT;
646
				throw new KnownFileMimeTypeException();
647
			}
648
		}
649
	}
650
651
652
	/**
653
	 * @param string $mimeType
654
	 * @param string $parsed
655
	 *
656
	 * @throws KnownFileMimeTypeException
657
	 */
658
	private function parseMimeTypePDF($mimeType, &$parsed) {
659
660
		if ($mimeType === 'application/pdf') {
661
			$parsed = self::MIMETYPE_PDF;
662
			throw new KnownFileMimeTypeException();
663
		}
664
	}
665
666
667
	/**
668
	 * @param string $mimeType
669
	 * @param string $parsed
670
	 *
671
	 * @throws KnownFileMimeTypeException
672
	 */
673
	private function parseMimeTypeOffice($mimeType, &$parsed) {
674
675
		$officeMimes = [
676
			'application/msword',
677
			'application/vnd.oasis.opendocument',
678
			'application/vnd.sun.xml',
679
			'application/vnd.openxmlformats-officedocument',
680
			'application/vnd.ms-word',
681
			'application/vnd.ms-powerpoint',
682
			'application/vnd.ms-excel'
683
		];
684
685 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...
686
			if (strpos($mimeType, $mime) === 0) {
687
				$parsed = self::MIMETYPE_OFFICE;
688
				throw new KnownFileMimeTypeException();
689
			}
690
		}
691
	}
692
693
694
	/**
695
	 * @param FilesDocument $document
696
	 * @param File $file
697
	 *
698
	 * @throws NotPermittedException
699
	 */
700
	private function extractContentFromFileText(FilesDocument $document, File $file) {
701
702
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_TEXT) {
703
			return;
704
		}
705
706
		if (!$this->isSourceIndexable($document)) {
707
			return;
708
		}
709
710
		$document->setContent(
711
			base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64
712
		);
713
	}
714
715
716
	/**
717
	 * @param FilesDocument $document
718
	 * @param File $file
719
	 *
720
	 * @throws NotPermittedException
721
	 */
722 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...
723
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_PDF) {
724
			return;
725
		}
726
727
		$this->configService->setDocumentIndexOption($document, ConfigService::FILES_PDF);
728
		if (!$this->isSourceIndexable($document)) {
729
			return;
730
		}
731
732
		if ($this->configService->getAppValue(ConfigService::FILES_PDF) !== '1') {
733
			$document->setContent('');
734
735
			return;
736
		}
737
738
		$document->setContent(
739
			base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64
740
		);
741
	}
742
743
744
	/**
745
	 * @param FilesDocument $document
746
	 * @param File $file
747
	 *
748
	 * @throws NotPermittedException
749
	 */
750 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...
751
		if ($this->parseMimeType($document->getMimeType()) !== self::MIMETYPE_OFFICE) {
752
			return;
753
		}
754
755
		$this->configService->setDocumentIndexOption($document, ConfigService::FILES_OFFICE);
756
		if (!$this->isSourceIndexable($document)) {
757
			return;
758
		}
759
760
		if ($this->configService->getAppValue(ConfigService::FILES_OFFICE) !== '1') {
761
			$document->setContent('');
762
763
			return;
764
		}
765
766
		$document->setContent(
767
			base64_encode($file->getContent()), IndexDocument::ENCODED_BASE64
768
		);
769
	}
770
771
772
	/**
773
	 * @param FilesDocument $document
774
	 *
775
	 * @return bool
776
	 */
777
	private function isSourceIndexable(FilesDocument $document) {
778
		$this->configService->setDocumentIndexOption($document, $document->getSource());
779
		if ($this->configService->getAppValue($document->getSource()) !== '1') {
780
			$document->setContent('');
781
782
			return false;
783
		}
784
785
		return true;
786
	}
787
788
789
	private function impersonateOwner(Index $index) {
790
		if ($index->getOwnerId() !== '') {
791
			return;
792
		}
793
794
		$this->groupFoldersService->impersonateOwner($index);
795
		$this->externalFilesService->impersonateOwner($index);
796
	}
797
798
799
	/**
800
	 * @param $action
801
	 * @param bool $force
802
	 *
803
	 * @throws Exception
804
	 */
805
	private function updateRunnerAction($action, $force = false) {
806
		if ($this->runner === null) {
807
			return;
808
		}
809
810
		$this->runner->updateAction($action, $force);
811
	}
812
813
814
	/**
815
	 * @param array $data
816
	 */
817
	private function updateRunnerInfo($data) {
818
		if ($this->runner === null) {
819
			return;
820
		}
821
822
		$this->runner->setInfoArray($data);
823
	}
824
825
826
}
827
828