Update proxy implementation

This commit is contained in:
Beatrycze Volk 2022-10-27 16:18:16 +02:00
parent 31c339f360
commit 2578e7a2be
5 changed files with 502 additions and 272 deletions

View File

@ -40,27 +40,59 @@ class View3DController extends AbstractController
// Quit without doing anything if required variables are not set.
return '';
} else {
$url = $this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[1]]['files']['DEFAULT']);
$proxy = '';
if ($this->settings['useInternalProxy']) {
// Configure @action URL for form.
$uri = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri(!empty($this->settings['forceAbsoluteUrl']) ? true : false)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $url,
'uHash' => 'View3DController'
])
->build();
$proxy = $uri;
$model = trim($this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[1]]['files']['DEFAULT']));
$this->view->assign('3d', $model);
$modelConverted = trim($this->document->getDoc()->getFileLocation($this->document->getDoc()->physicalStructureInfo[$this->document->getDoc()->physicalStructure[1]]['files']['CONVERTED']));
$xml = $this->requestData['id'];
$settingsParts = explode("/", $model);
$fileName = end($settingsParts);
$path = substr($model, 0, strrpos($model, $fileName));
$modelSettings = $path . "metadata/" . $fileName . "_viewer";
if (!empty($modelConverted)) {
$model = $modelConverted;
}
$this->view->assign('url', $url);
$this->view->assign('proxy', $proxy);
$this->view->assign('scriptMain', '/typo3conf/ext/dlf/Resources/Public/Javascript/3DViewer/main.js');
$this->view->assign('scriptToastify', '/typo3conf/ext/dlf/Resources/Public/Javascript/Toastify/toastify.js');
$this->view->assign('scriptSpinner', '/typo3conf/ext/dlf/Resources/Public/Javascript/3DViewer/spinner/main.js');
if ($this->settings['useInternalProxy']) {
$absoluteUri = !empty($this->settings['forceAbsoluteUrl']) ? true : false;
$model = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri($absoluteUri)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $model,
'uHash' => GeneralUtility::hmac($model, 'PageViewProxy')
])
->build();
$xml = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri($absoluteUri)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $xml,
'uHash' => GeneralUtility::hmac($xml, 'PageViewProxy')
])
->build();
$modelSettings = $this->uriBuilder->reset()
->setTargetPageUid($GLOBALS['TSFE']->id)
->setCreateAbsoluteUri($absoluteUri)
->setArguments([
'eID' => 'tx_dlf_pageview_proxy',
'url' => $modelSettings,
'uHash' => GeneralUtility::hmac($modelSettings, 'PageViewProxy')
])
->build();
}
$this->view->assign('model', $model);
$this->view->assign('xml', $xml);
$this->view->assign('settings', $modelSettings);
$this->view->assign('proxy', $this->settings['useInternalProxy']);
}
}
}

View File

@ -67,7 +67,7 @@ class PageViewProxy
$origin = (string) ($request->getHeaderLine('Origin') ? : '*');
return $response
->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS')
->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS, HEAD')
->withHeader('Access-Control-Allow-Origin', $origin)
->withHeader('Access-Control-Max-Age', '86400');
}
@ -113,6 +113,39 @@ class PageViewProxy
return $this->withCorsResponseHeaders($response, $request);
}
/**
* Handle an HEAD request.
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function handleHead(ServerRequestInterface $request): ResponseInterface
{
$queryParams = $request->getQueryParams();
$url = (string) ($queryParams['url'] ?? '');
try {
$targetResponse = $this->requestFactory->request($url, 'HEAD', [
'headers' => [
'User-Agent' => $this->extConf['useragent'] ?? 'Kitodo.Presentation Proxy',
]
]);
} catch (\Exception $e) {
return new JsonResponse(['message' => 'Could not fetch resource of given URL.'], 500);
}
$clientResponse = GeneralUtility::makeInstance(Response::class)
->withStatus($targetResponse->getStatusCode());
$clientResponse = $this->copyHeaders($targetResponse, $clientResponse, [
'Content-Length',
'Content-Type',
'Last-Modified',
]);
return $this->withCorsResponseHeaders($clientResponse, $request);
}
/**
* Handle a GET request.
*
@ -130,12 +163,9 @@ class PageViewProxy
// get and verify the uHash
$uHash = (string) ($queryParams['uHash'] ?? '');
if ($uHash == 'View3DController') {
// don't verify hash
} else if (!hash_equals(GeneralUtility::hmac($url, 'PageViewProxy'), $uHash)) {
if (!hash_equals(GeneralUtility::hmac($url, 'PageViewProxy'), $uHash)) {
return new JsonResponse(['message' => 'No valid uHash passed!'], 401);
}
try {
$targetResponse = $this->requestFactory->request($url, 'GET', [
'headers' => [
@ -187,6 +217,9 @@ class PageViewProxy
case 'GET':
return $this->handleGet($request);
case 'HEAD':
return $this->handleHead($request);
default:
// 405 Method Not Allowed
return GeneralUtility::makeInstance(Response::class)

View File

@ -5,6 +5,7 @@ plugin.tx_dlf {
}
settings {
storagePid = {$plugin.tx_dlf.persistence.storagePid}
useInternalProxy = 1
}
}

View File

@ -13,8 +13,11 @@
xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<p id="DFG_3DViewer" 3d="{url}" 3d="{proxy}" class="tx-dlf-view-3d"></p>
<script src="{scriptMain}" type="module"></script>
<script src="{scriptToastify}"></script>
<script src="{scriptSpinner}"></script>
<p id="DFG_3DViewer" 3d="{3d}" model="{model}" xml="{xml}" settings="{settings}" proxy="{proxy}" class="tx-dlf-view-3d"></p>
<f:section name="FooterAssets">
<script src="/typo3conf/ext/dlf/Resources/Public/Javascript/3DViewer/main.js" type="module"></script>
<script src="/typo3conf/ext/dlf/Resources/Public/Javascript/Toastify/toastify.js" type="text/javascript"></script>
<script src="/typo3conf/ext/dlf/Resources/Public/Javascript/3DViewer/spinner/main.js" type="text/javascript"></script>
</f:section>
</html>

File diff suppressed because it is too large Load Diff