api rendering

This commit is contained in:
Dennis Eichhorn 2021-01-31 17:17:50 +01:00
parent 6de9a0c13f
commit 3a4bc10459
13 changed files with 254 additions and 14 deletions

View File

@ -37,6 +37,17 @@ return [
],
],
],
'^.*/media/export(\?+.*|$)' => [
[
'dest' => '\Modules\Media\Controller\ApiController:apiMediaExport',
'verb' => RouteVerb::GET,
'permission' => [
'module' => ApiController::MODULE_NAME,
'type' => PermissionType::READ,
'state' => PermissionState::MEDIA,
],
],
],
'^.*/media/collection(\?+.*|$)' => [
[
'dest' => '\Modules\Media\Controller\ApiController:apiCollectionCreate',

View File

@ -35,6 +35,10 @@ use phpOMS\Model\Message\FormValidation;
use phpOMS\System\File\FileUtils;
use phpOMS\System\File\Local\Directory;
use phpOMS\Utils\Parser\Markdown\Markdown;
use phpOMS\Views\View;
use phpOMS\System\MimeType;
use phpOMS\Model\Html\Head;
use phpOMS\Asset\AssetType;
/**
* Media class.
@ -538,6 +542,52 @@ final class ApiController extends Controller
$response->set('export', $view);
}
/**
* Routing end-point for application behaviour.
*
* @param Media $media Media
* @param HttpRequest $request Request
* @param HttpResponse $response Response
*
* @return View
*
* @since 1.0.0
*/
public function createView(Media $media, RequestAbstract $request, ResponseAbstract $response) : View
{
$view = new View($this->app->l11nManager, $request, $response);
$view->setData('media', $media);
if (($type = $request->getData('type')) === null) {
$view->setTemplate('/Modules/Media/Theme/Api/render');
} elseif ($type === 'html') {
$head = new Head();
$css = \file_get_contents(__DIR__ . '/../../../Web/Backend/css/backend-small.css');
if ($css === false) {
$css = '';
}
$css = \preg_replace('!\s+!', ' ', $css);
$head->setStyle('core', $css ?? '');
$head->addAsset(AssetType::CSS, 'cssOMS/styles.css');
$view->setData('head', $head);
switch (\strtolower($media->extension)) {
case 'xls':
case 'xlsx':
$view->setTemplate('/Modules/Media/Theme/Api/spreadsheetAsHtml');
break;
case 'doc':
case 'docx':
$view->setTemplate('/Modules/Media/Theme/Api/wordAsHtml');
break;
}
}
return $view;
}
/**
* Set header for report/template
*
@ -548,13 +598,15 @@ final class ApiController extends Controller
*
* @return void
*
* @api
*
* @since 1.0.0
*/
private function setMediaResponseHeader(View $view, Media $media, RequestAbstract $request, ResponseAbstract $response) : void
{
switch (\strtolower($media->extension)) {
switch ($request->getData('type') ?? \strtolower($media->extension)) {
case 'htm':
case 'html':
$response->header->set('Content-Type', MimeType::M_HTML, true);
break;
case 'pdf':
$response->header->set('Content-Type', MimeType::M_PDF, true);
break;

View File

@ -2,4 +2,9 @@
// @todo: is this chunked/streamed output or bulk output
// if it is streamed it is not working because of ob_* in the actual response rendering
$media = $this->getData('media');
$fp = \fopen(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath(), 'r');
\fpassthru($fp);
\fclose($fp);

View File

@ -0,0 +1,22 @@
<?php declare(strict_types=1);
use phpOMS\Autoloader;
Autoloader::addPath(__DIR__ . '/../../../../Resources/');
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$media = $this->getData('media');
$reader = IOFactory::createReaderforFile(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath());
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath());
$writer = new Csv($spreadsheet);
$writer->setDelimiter(';');
$writer->setEnclosure('"');
$writer->setLineEnding("\n");
$writer->setUseBOM(true);
$writer->save('php://output');

View File

@ -0,0 +1,21 @@
<?php declare(strict_types=1);
use phpOMS\Autoloader;
Autoloader::addPath(__DIR__ . '/../../../../Resources/');
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$media = $this->getData('media');
$reader = IOFactory::createReaderforFile(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath());
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath());
$writer = new Html($spreadsheet);
$writer->writeAllSheets();
$writer->save('php://output');
echo '<style>body { margin: 0; } table { width: 100%; }</style>';

View File

@ -0,0 +1,17 @@
<?php declare(strict_types=1);
use phpOMS\Autoloader;
Autoloader::addPath(__DIR__ . '/../../../../Resources/');
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Writer\HTML;
use PhpOffice\PhpWord\PhpWord;
$media = $this->getData('media');
$reader = IOFactory::createReader('Word2007');
$doc = $reader->load(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath());
$writer = new HTML($doc);
$writer->save('php://output');

View File

@ -1,7 +1,11 @@
<?php
use phpOMS\Uri\UriFactory;
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<audio width="100%" controls>
<source src="<?= $this->media->getPath(); ?>" type="audio/<?= $this->media->extension; ?>">
<source src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->getId()); ?>" type="audio/<?= $this->media->extension; ?>">
Your browser does not support HTML audio.
</audio>
</div>

View File

@ -1,7 +1,11 @@
<?php
use phpOMS\Uri\UriFactory;
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<div class="h-overflow centerText">
<img style="max-width: 100%" src="<?= $this->media->getPath(); ?>" alt="<?= $this->printHtml($this->media->name); ?>">
<img style="max-width: 100%" src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->getId()); ?>" alt="<?= $this->printHtml($this->media->name); ?>">
</div>
</div>
</section>

View File

@ -0,0 +1,25 @@
<?php
$array = \json_decode($this->getFileContent(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath()), true);
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<div id="media" class="tabview tab-2 m-editor">
<ul class="tab-links">
<li><label tabindex="0" for="media-c-tab-1"><?= $this->getHtml('Preview', 'Media'); ?></label>
<li><label tabindex="0" for="media-c-tab-2"><?= $this->getHtml('Edit', 'Media'); ?></label>
</ul>
<div class="tab-content">
<input type="radio" id="media-c-tab-1" name="tabular-1" checked>
<div class="tab">
<pre><?= \json_encode($array, \JSON_PRETTY_PRINT); ?></pre>
</div>
<input type="radio" id="media-c-tab-2" name="tabular-1">
<div class="tab">
<pre class="textContent" data-tpl-text="/media/content" data-tpl-value="/media/content"><?= $this->printHtml(
$this->getFileContent(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath())
); ?></pre>
</div>
</div>
</div>
</section>

View File

@ -0,0 +1,19 @@
<?php declare(strict_types=1);
use phpOMS\Autoloader;
Autoloader::addPath(__DIR__ . '/../../../../../../Resources/');
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Writer\Html;
use PhpOffice\PhpPresentation\Spreadsheet;
$reader = IOFactory::createReader('PowerPoint2007');
$presentation = $reader->load(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath());
$writer = new Html($presentation);
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<?php $writer->save('php://output'); ?>
</div>
</section>

View File

@ -1,18 +1,65 @@
<?php declare(strict_types=1);
use phpOMS\Autoloader;
Autoloader::addPath(__DIR__ . '/../../../../../../Resources/');
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$reader = IOFactory::createReaderforFile(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath());
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath());
$writer = new Html($spreadsheet);
$writer->writeAllSheets();
use phpOMS\Uri\UriFactory;
use phpOMS\Utils\IO\Csv\CsvSettings;
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<?php $writer->save('php://output'); ?>
<div id="media" class="tabview tab-2 m-editor">
<ul class="tab-links">
<li><label tabindex="0" for="media-c-tab-1"><?= $this->getHtml('Preview', 'Media'); ?></label>
<li><label tabindex="0" for="media-c-tab-2">CSV</label>
</ul>
<div class="tab-content">
<input type="radio" id="media-c-tab-1" name="tabular-1" checked>
<div class="tab">
<iframe src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->getId()); ?>&type=html"></iframe>
</div>
<input type="radio" id="media-c-tab-2" name="tabular-1" checked>
<div class="tab">
<?php
$reader = IOFactory::createReaderforFile(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath());
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath());
$writer = new Csv($spreadsheet);
$writer->setDelimiter(';');
$writer->setEnclosure('"');
$writer->setLineEnding("\n");
$writer->setUseBOM(true);
\ob_start();
$writer->save('php://output');
$data = \ob_get_clean();
$csv = \explode("\n", \trim($data, "\n"));
?>
<table class="default">
<?php
foreach ($csv as $line) {
$lineCsv = \str_getcsv($line, ';', '"');
if ($lineCsv === null) {
break;
}
echo '<tr>';
foreach ($lineCsv as $cell) {
if ($cell === null) {
break;
}
echo '<td>' . \htmlspecialchars($cell);
}
}
?>
</table>
</div>
</div>
</div>
</section>

View File

@ -1,7 +1,11 @@
<?php
use phpOMS\Uri\UriFactory;
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<video width="100%" controls>
<source src="<?= $this->media->getPath(); ?>" type="video/<?= $this->media->extension; ?>">
<source src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->getId()); ?>" type="video/<?= $this->media->extension; ?>">
Your browser does not support HTML video.
</video>
</div>

View File

@ -0,0 +1,9 @@
<?php
use phpOMS\Uri\UriFactory;
?>
<section id="mediaFile" class="portlet">
<div class="portlet-body">
<iframe src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->getId()); ?>&type=html"></iframe>
</div>
</section>