Passed
Push — release-11.0.x ( ef2028...d0f0d9 )
by Rafael
18:22 queued 15:35
created

PreviewController::previewAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 3.004

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 23
nc 3
nop 1
dl 0
loc 40
ccs 24
cts 26
cp 0.9231
crap 3.004
rs 9.552
c 1
b 0
f 0
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\ResponseInterface;
27
use Psr\Http\Message\ServerRequestInterface;
28
use Throwable;
29
use TYPO3\CMS\Core\Http\HtmlResponse;
30
use TYPO3\CMS\Core\Http\Response;
31
use TYPO3\CMS\Core\Resource\ResourceFactory;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Fluid\View\StandaloneView;
34
35
/**
36
 * Class PreviewController
37
 */
38
class PreviewController
39
{
40
    /**
41
     * @param ServerRequestInterface $request
42
     * @return string|Response
43
     * @throws ClientExceptionInterface
44
     * @throws Throwable
45
     */
46 2
    public function previewAction(ServerRequestInterface $request): ResponseInterface
47
    {
48 2
        $response = new HtmlResponse('');
49 2
        if (!$this->getIsAdmin()) {
50 1
            $messageText = 'Only admins can see the tika preview';
51 1
            $response->getBody()->write($messageText);
52 1
            return $response->withStatus(403, $messageText);
53
        }
54
55 1
        $identifier = (string)$request->getQueryParams()['identifier'];
56 1
        $file = $this->getFileResourceFactory()->getFileObjectFromCombinedIdentifier($identifier);
57
58 1
        $tikaService = $this->getConfiguredTikaService();
59 1
        $metadata = $tikaService->extractMetaData(
60
            /** @scrutinizer ignore-type because checked in {@link \ApacheSolrForTypo3\Tika\ContextMenu\Preview::canHandle()} */
61 1
            $file
62 1
        );
63 1
        $content = $tikaService->extractText(
64
            /** @scrutinizer ignore-type because checked in {@link \ApacheSolrForTypo3\Tika\ContextMenu\Preview::canHandle()} */
65 1
            $file
66 1
        );
67
68
        try {
69 1
            $language = $tikaService->detectLanguageFromFile(
70
                /** @scrutinizer ignore-type because checked in {@link \ApacheSolrForTypo3\Tika\ContextMenu\Preview::canHandle()} */
71 1
                $file
72 1
            );
73
        } catch (Throwable $e) {
74
            $language = 'not detectable';
75
        }
76
77 1
        $view = $this->getInitializedPreviewView();
78
79 1
        $view->assign('metadata', $metadata);
80 1
        $view->assign('content', $content);
81 1
        $view->assign('language', $language);
82
83 1
        $response->getBody()->write($view->render() ?? '');
84
85 1
        return $response;
86
    }
87
88
    /**
89
     * @return AppService|ServerService|SolrCellService
90
     */
91
    protected function getConfiguredTikaService(): AbstractService
92
    {
93
        return ServiceFactory::getConfiguredTika();
94
    }
95
96
    /**
97
     * @return ResourceFactory
98
     * @noinspection PhpIncompatibleReturnTypeInspection
99
     */
100
    protected function getFileResourceFactory(): ResourceFactory
101
    {
102
        return GeneralUtility::makeInstance(ResourceFactory::class);
103
    }
104
105
    /**
106
     * @return StandaloneView
107
     */
108
    protected function getInitializedPreviewView(): StandaloneView
109
    {
110
        /** @var $view StandaloneView */
111
        $view = GeneralUtility::makeInstance(StandaloneView::class);
112
        $view->getRequest()->setControllerExtensionName('tika');
113
        $templatePathAndFile = 'EXT:tika/Resources/Private/Templates/Backend/Preview.html';
114
        $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName($templatePathAndFile));
115
        return $view;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121
    protected function getIsAdmin(): bool
122
    {
123
        return !empty($GLOBALS['BE_USER']) && $GLOBALS['BE_USER']->isAdmin();
124
    }
125
}
126