Completed
Push — master ( a3c264...7440ee )
by Maxence
01:54
created

FilesService::setDocumentLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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