external_media/Classes/Soundcloud/Renderer.php

90 lines
2.8 KiB
PHP

<?php
namespace Codappix\ExternalTypo3Multimedia\Soundcloud;
/*
* Copyright (C) 2020 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\Resource\FileInterface;
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($width . '%', $height, $options);
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)
{
$urlParams = [];
if (!empty($options['autoplay'])) {
$urlParams[] = 'auto_play=1';
}
$file = $this->iframeRenderer->getOriginalFile($file);
return sprintf(
'https://' . Registration::EMBED_DOMAIN . '/player/?url=%s?%s',
rawurlencode($this->iframeRenderer->getOnlineMediaHelper($file)->getPublicUrl($file)),
implode('&', $urlParams)
);
}
}