adds media ingester/renderer

This commit is contained in:
buttle 2021-04-09 17:07:09 +02:00
parent 72b32d0401
commit d25dbea8d0
6 changed files with 131 additions and 3 deletions

View File

@ -17,6 +17,16 @@ return [
Form\PeertubeVideoBlockForm::class => Form\PeertubeVideoBlockForm::class, Form\PeertubeVideoBlockForm::class => Form\PeertubeVideoBlockForm::class,
], ],
], ],
'media_ingesters' => [
'factories' => [
'PeertubeVideo_media' => Service\Media\Ingester\PeertubeMediaFactory::class,
],
],
'media_renderers' => [
'invokables' => [
'PeertubeVideo_media' => Media\Renderer\PeertubeMediaRenderer::class,
],
],
'DefaultSettings' => [ 'DefaultSettings' => [
'PeertubeVideoBlockForm' => [ 'PeertubeVideoBlockForm' => [
'url' => '', 'url' => '',

View File

@ -10,6 +10,7 @@ class PeertubeVideoBlockForm extends Form
public function init() public function init()
{ {
/*
$this->add([ $this->add([
'name' => 'o:block[__blockIndex__][o:data][url]', 'name' => 'o:block[__blockIndex__][o:data][url]',
'type' => Element\Text::class, 'type' => Element\Text::class,
@ -17,7 +18,7 @@ class PeertubeVideoBlockForm extends Form
'label' => 'Video URL', 'label' => 'Video URL',
] ]
]); ]);
*/
$this->add([ $this->add([
'name' => 'o:block[__blockIndex__][o:data][ratio]', 'name' => 'o:block[__blockIndex__][o:data][ratio]',
'type' => Element\Select::class, 'type' => Element\Select::class,

View File

@ -0,0 +1,69 @@
<?php
namespace PeertubeVideo\Media\Ingester;
use Omeka\Api\Request;
use Omeka\Entity\Media;
use Omeka\Media\Ingester\IngesterInterface;
use Omeka\Stdlib\ErrorStore;
use Zend\Form\Element\Text;
use Zend\Http\Client;
use Zend\View\Renderer\PhpRenderer;
class PeertubeMediaIngester implements IngesterInterface
{
protected $client;
public function __construct($client)
{
$this->client = $client;
}
public function getLabel()
{
return 'PeerTube video'; // @translate
}
public function getRenderer()
{
return 'PeertubeVideo_media';
}
public function form(PhpRenderer $view, array $options = [])
{
$input = new Text('o:media[__index__][o:source]');
$input->setOptions([
'label' => 'Video URL', // @translate
'info' => 'URL for the video to embed.', // @translate
]);
$input->setAttributes([
'required' => true,
]);
return $view->formRow($input);
}
public function ingest(Media $media, Request $request, ErrorStore $errorStore)
{
// Validate the request data.
$data = $request->getContent();
if (!isset($data['o:source'])) {
$errorStore->addError('o:source', 'No video URL specified');
return;
}
// Get the video JSON data.
//$url = urlencode($data['o:source']);
$url = $data['o:source'];
$parsed_url = parse_url(trim($url));
$base_url = $parsed_url['scheme']. '://' .$parsed_url['host'];
$path_parts = explode("/", $parsed_url['path']);
$api_url .= $base_url . '/api/v1/videos/' . end($path_parts);
$response = $this->client->setUri($api_url)->send();
if (!$response->isOk()) {
$errorStore->addError('o:source', sprintf(
'Error reading video: %s (%s) %s',
$response->getReasonPhrase(),
$response->getStatusCode(),
$base_url
));
return false;
}
// Set the Media source and data.
$media->setSource($url);
$media->setData(json_decode($response->getBody(), true));
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace PeertubeVideo\Media\Renderer;
use Omeka\Api\Representation\MediaRepresentation;
use Omeka\Media\Renderer\RendererInterface;
use Zend\View\Renderer\PhpRenderer;
class PeertubeMediaRenderer implements RendererInterface
{
public function render(PhpRenderer $view,
MediaRepresentation $media,
array $options = [])
{
$url = str_replace('watch', 'embed', $media->source());
if (parse_url($url, PHP_URL_QUERY)) {
$url .= '&peertubeLink=0';
} else {
$url .= '?peertubeLink=0';
}
$html = '<iframe ';
$html .= 'sandbox="allow-same-origin allow-scripts allow-popups" ';
$html .= 'height="100%" width="100%" ';
$html .= 'src="'. $url .'"';
$html .= '></iframe>';
return $html;
//return $media->mediaData()['html'];
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace PeertubeVideo\Service\Media\Ingester;
use PeertubeVideo\Media\Ingester\PeertubeMediaIngester;
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;
class PeertubeMediaFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $services,
$requestedName,
array $options = null)
{
return new PeertubeMediaIngester($services->get('Omeka\HttpClient'));
}
}

View File

@ -55,11 +55,11 @@ class PeertubeVideo extends AbstractBlockLayout
$form->prepare(); $form->prepare();
$html = ''; $html = '';
//$html .= $view->blockAttachmentsForm($block); $html .= $view->blockAttachmentsForm($block);
$html .= '<a href="#" class="collapse" aria-label="collapse"><h4>' . $view->translate('Options'). '</h4></a>'; $html .= '<a href="#" class="collapse" aria-label="collapse"><h4>' . $view->translate('Options'). '</h4></a>';
$html .= '<div class="collapsible" style="padding-top:6px;">'; $html .= '<div class="collapsible" style="padding-top:6px;">';
$html .= $view->formCollection($form); $html .= $view->formCollection($form);
$html .= '</div>'; $html .= '</div>';
return $html; return $html;
} }
@ -76,6 +76,10 @@ class PeertubeVideo extends AbstractBlockLayout
} else { } else {
$height = $width * $block->dataValue('ratio'); $height = $width * $block->dataValue('ratio');
} }
//$media = $attachments[0]->media();
$url = $block->dataValue('url'); $url = $block->dataValue('url');
$url = str_replace('watch', 'embed', $url); $url = str_replace('watch', 'embed', $url);
if (parse_url($url, PHP_URL_QUERY)) { if (parse_url($url, PHP_URL_QUERY)) {