102 lines
3.1 KiB
PHP
102 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Codappix\ExternalTypo3Multimedia\Anchor;
|
|
|
|
/*
|
|
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
use Codappix\ExternalTypo3Multimedia\IFrameRenderer;
|
|
use TYPO3\CMS\Core\Http\Uri;
|
|
use TYPO3\CMS\Core\Resource\FileInterface;
|
|
use TYPO3\CMS\Core\Resource\FileReference;
|
|
use TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
class Renderer implements FileRendererInterface
|
|
{
|
|
/**
|
|
* @var IFrameRenderer
|
|
*/
|
|
private $iframeRenderer;
|
|
|
|
public function __construct(
|
|
IFrameRenderer $iframeRenderer
|
|
) {
|
|
$this->iframeRenderer = $iframeRenderer;
|
|
}
|
|
|
|
public function getPriority()
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
public function canRender(FileInterface $file)
|
|
{
|
|
return (
|
|
$file->getMimeType() === Registration::FILE_MIME_TYPE
|
|
|| $file->getExtension() === Registration::FILE_EXTENSION
|
|
)
|
|
&& $this->iframeRenderer->getOnlineMediaHelper($file) !== false
|
|
;
|
|
}
|
|
|
|
public function render(
|
|
FileInterface $file,
|
|
$width,
|
|
$height,
|
|
array $options = [],
|
|
$usedPathsRelativeToCurrentScript = false
|
|
) {
|
|
$options = $this->iframeRenderer->collectOptions($options, $file);
|
|
$attributes = $this->iframeRenderer->collectAttributes('400px', '102px', $options);
|
|
$attributes['frameborder'] = 0;
|
|
$attributes['scrolling'] = 'no';
|
|
|
|
return sprintf(
|
|
'<iframe src="%s"%s></iframe>',
|
|
htmlspecialchars($this->createUrl($options, $file), ENT_QUOTES | ENT_HTML5),
|
|
empty($attributes) ? '' : ' ' . GeneralUtility::implodeAttributes($attributes)
|
|
);
|
|
}
|
|
|
|
private function createUrl(array $options, FileInterface $file)
|
|
{
|
|
if ($file instanceof FileReference) {
|
|
$file = $file->getOriginalFile();
|
|
}
|
|
|
|
$url = $this->iframeRenderer->getOnlineMediaHelper($file)->getPublicUrl($file);
|
|
$url = $this->addEmbedPathSegment($url);
|
|
|
|
return $url;
|
|
}
|
|
|
|
private function addEmbedPathSegment(string $url): string
|
|
{
|
|
$url = new Uri($url);
|
|
|
|
$pathSegments = GeneralUtility::trimExplode('/', $url->getPath(), true);
|
|
$url = $url->withPath(implode('/', array_merge([
|
|
$pathSegments[0],
|
|
'embed',
|
|
], array_slice($pathSegments, 1))));
|
|
|
|
return (string) $url;
|
|
}
|
|
}
|