1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApacheSolrForTypo3\Tika\Controller\Backend; |
6
|
|
|
|
7
|
|
|
/* |
8
|
|
|
* This file is part of the TYPO3 CMS project. |
9
|
|
|
* |
10
|
|
|
* It is free software; you can redistribute it and/or modify it under |
11
|
|
|
* the terms of the GNU General Public License, either version 2 |
12
|
|
|
* of the License, or any later version. |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please read the |
15
|
|
|
* LICENSE.txt file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
* The TYPO3 project - inspiring people to share! |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Tika\Service\Tika\AbstractService; |
21
|
|
|
use ApacheSolrForTypo3\Tika\Service\Tika\AppService; |
22
|
|
|
use ApacheSolrForTypo3\Tika\Service\Tika\ServerService; |
23
|
|
|
use ApacheSolrForTypo3\Tika\Service\Tika\ServiceFactory; |
24
|
|
|
use ApacheSolrForTypo3\Tika\Service\Tika\SolrCellService; |
25
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
26
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
27
|
|
|
use Throwable; |
28
|
|
|
use TYPO3\CMS\Core\Http\HtmlResponse; |
29
|
|
|
use TYPO3\CMS\Core\Http\Response; |
30
|
|
|
use TYPO3\CMS\Core\Resource\ResourceFactory; |
31
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
32
|
|
|
use TYPO3\CMS\Fluid\View\StandaloneView; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class PreviewController |
36
|
|
|
*/ |
37
|
|
|
class PreviewController |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ServerRequestInterface $request |
42
|
|
|
* @return string|Response |
43
|
|
|
* @throws ClientExceptionInterface |
44
|
|
|
*/ |
45
|
2 |
|
public function previewAction(ServerRequestInterface $request) |
46
|
|
|
{ |
47
|
2 |
|
$response = new HtmlResponse(''); |
48
|
2 |
|
if (!$this->getIsAdmin()) { |
49
|
1 |
|
$messageText = 'Only admins can see the tika preview'; |
50
|
1 |
|
$response->getBody()->write($messageText); |
51
|
1 |
|
return $response->withStatus(403, $messageText); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$identifier = (string)$request->getQueryParams()['identifier']; |
55
|
1 |
|
$file = $this->getFileResourceFactory()->getFileObjectFromCombinedIdentifier($identifier); |
56
|
|
|
|
57
|
1 |
|
$tikaService = $this->getConfiguredTikaService(); |
58
|
1 |
|
$metadata = $tikaService->extractMetaData($file); |
|
|
|
|
59
|
1 |
|
$content = $tikaService->extractText($file); |
|
|
|
|
60
|
|
|
|
61
|
|
|
try { |
62
|
1 |
|
$language = $tikaService->detectLanguageFromFile($file); |
|
|
|
|
63
|
|
|
} catch (Throwable $e) { |
64
|
|
|
$language = 'not detectable'; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
$view = $this->getInitializedPreviewView(); |
68
|
|
|
|
69
|
1 |
|
$view->assign('metadata', $metadata); |
70
|
1 |
|
$view->assign('content', $content); |
71
|
1 |
|
$view->assign('language', $language); |
72
|
|
|
|
73
|
1 |
|
$response->getBody()->write($view->render()); |
74
|
|
|
|
75
|
1 |
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return AppService|ServerService|SolrCellService |
80
|
|
|
*/ |
81
|
|
|
protected function getConfiguredTikaService(): AbstractService |
82
|
|
|
{ |
83
|
|
|
return ServiceFactory::getConfiguredTika(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return ResourceFactory |
88
|
|
|
* @noinspection PhpIncompatibleReturnTypeInspection |
89
|
|
|
*/ |
90
|
|
|
protected function getFileResourceFactory(): ResourceFactory |
91
|
|
|
{ |
92
|
|
|
return GeneralUtility::makeInstance(ResourceFactory::class); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return StandaloneView |
97
|
|
|
*/ |
98
|
|
|
protected function getInitializedPreviewView(): StandaloneView |
99
|
|
|
{ |
100
|
|
|
/** @var $view StandaloneView */ |
101
|
|
|
$view = GeneralUtility::makeInstance(StandaloneView::class); |
102
|
|
|
$view->getRequest()->setControllerExtensionName('tika'); |
103
|
|
|
$templatePathAndFile = 'EXT:tika/Resources/Private/Templates/Backend/Preview.html'; |
104
|
|
|
$view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templatePathAndFile)); |
105
|
|
|
return $view; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
protected function getIsAdmin(): bool |
112
|
|
|
{ |
113
|
|
|
return !empty($GLOBALS['BE_USER']) && $GLOBALS['BE_USER']->isAdmin(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|