Quick backup before crash

This commit is contained in:
Dennis Eichhorn 2023-06-13 18:55:51 +00:00
parent 10b99b6f27
commit 53d38e71cb
13 changed files with 175 additions and 14 deletions

View File

@ -34,6 +34,8 @@ use Modules\Media\Models\Reference;
use Modules\Media\Models\ReferenceMapper; use Modules\Media\Models\ReferenceMapper;
use Modules\Media\Models\UploadFile; use Modules\Media\Models\UploadFile;
use Modules\Media\Models\UploadStatus; use Modules\Media\Models\UploadStatus;
use Modules\Media\Theme\Backend\Components\Media\ElementView;
use Modules\Media\Views\MediaView;
use Modules\Tag\Models\NullTag; use Modules\Tag\Models\NullTag;
use phpOMS\Account\PermissionType; use phpOMS\Account\PermissionType;
use phpOMS\Application\ApplicationAbstract; use phpOMS\Application\ApplicationAbstract;
@ -1137,6 +1139,16 @@ final class ApiController extends Controller
$response->set('export', $view); $response->set('export', $view);
} }
/**
* Decrypt an encrypted media element
*
* @param Media $media Media model
* @param RequestAbstract $request Request model
*
* @return Media
*
* @since 1.0.0
*/
private function prepareEncryptedMedia(Media $media, RequestAbstract $request) : Media private function prepareEncryptedMedia(Media $media, RequestAbstract $request) : Media
{ {
$path = ''; $path = '';
@ -1148,6 +1160,8 @@ final class ApiController extends Controller
$path = '../../../Temp/' . $randomName . '.' . $media->getExtension(); $path = '../../../Temp/' . $randomName . '.' . $media->getExtension();
$absolutePath = __DIR__ . '/' . $path; $absolutePath = __DIR__ . '/' . $path;
++$counter;
} while (!\is_file($absolutePath) && $counter < 1000); } while (!\is_file($absolutePath) && $counter < 1000);
if ($counter >= 1000) { if ($counter >= 1000) {
@ -1182,8 +1196,8 @@ final class ApiController extends Controller
*/ */
public function createView(Media $media, RequestAbstract $request, ResponseAbstract $response) : View public function createView(Media $media, RequestAbstract $request, ResponseAbstract $response) : View
{ {
$view = new View($this->app->l11nManager, $request, $response); $view = new ElementView($this->app->l11nManager, $request, $response);
$view->data['media'] = $media; $view->media = $media;
if (!\headers_sent()) { if (!\headers_sent()) {
$response->endAllOutputBuffering(); // for large files $response->endAllOutputBuffering(); // for large files
@ -1205,6 +1219,34 @@ final class ApiController extends Controller
$view->data['head'] = $head; $view->data['head'] = $head;
switch (\strtolower($media->extension)) { switch (\strtolower($media->extension)) {
case 'jpg':
case 'jpeg':
case 'gif':
case 'png':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/image_raw');
break;
case 'pdf':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/pdf_raw');
break;
case 'c':
case 'cpp':
case 'h':
case 'php':
case 'js':
case 'css':
case 'rs':
case 'py':
case 'r':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/text_raw');
break;
case 'txt':
case 'cfg':
case 'log':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/text_raw');
break;
case 'md':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/markdown_raw');
break;
case 'xls': case 'xls':
case 'xlsx': case 'xlsx':
$view->setTemplate('/Modules/Media/Theme/Api/spreadsheetAsHtml'); $view->setTemplate('/Modules/Media/Theme/Api/spreadsheetAsHtml');
@ -1213,6 +1255,15 @@ final class ApiController extends Controller
case 'docx': case 'docx':
$view->setTemplate('/Modules/Media/Theme/Api/wordAsHtml'); $view->setTemplate('/Modules/Media/Theme/Api/wordAsHtml');
break; break;
case 'mp3':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/audio_raw');
break;
case 'mp4':
case 'mpeg':
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/video_raw');
break;
default:
$view->setTemplate('/Modules/Media/Theme/Backend/Components/Media/default');
} }
} }

View File

@ -96,7 +96,8 @@ final class BackendController extends Controller
/** @var Media[] $media */ /** @var Media[] $media */
$media = $mediaMapper->execute(); $media = $mediaMapper->execute();
$collectionMapper = CollectionMapper::getParentCollection($path)->where('tags/title/language', $request->header->l11n->language); $collectionMapper = CollectionMapper::getParentCollection($path)
->where('tags/title/language', $request->header->l11n->language);
if (!$hasPermission) { if (!$hasPermission) {
$permWhere = PermissionAbstractMapper::helper($this->app->dbPool->get('select')) $permWhere = PermissionAbstractMapper::helper($this->app->dbPool->get('select'))
@ -112,9 +113,10 @@ final class BackendController extends Controller
$collectionMapper->where('', $permWhere); $collectionMapper->where('', $permWhere);
} }
/** @var \Modules\Media\Models\Collection $collection */
$collection = $collectionMapper->execute(); $collection = $collectionMapper->execute();
if ((\is_array($collection) || $collection->id === 0) && \is_dir(__DIR__ . '/../Files' . $path)) { if ((empty($collection) || $collection->id === 0) && \is_dir(__DIR__ . '/../Files' . $path)) {
$collection = new Collection(); $collection = new Collection();
$collection->name = \basename($path); $collection->name = \basename($path);
$collection->setVirtualPath(\dirname($path)); $collection->setVirtualPath(\dirname($path));

View File

@ -247,7 +247,7 @@ class Media implements \JsonSerializable
*/ */
public function encrypt(string $key, string $outputPath = null) : bool public function encrypt(string $key, string $outputPath = null) : bool
{ {
return EncryptionHelper::encryptFile($this->getAbsolutePath(), $outputPath, $key); return EncryptionHelper::encryptFile($this->getAbsolutePath(), $outputPath ?? $this->getAbsolutePath(), $key);
} }
/** /**
@ -262,7 +262,7 @@ class Media implements \JsonSerializable
*/ */
public function decrypt(string $key, string $outputPath = null) : bool public function decrypt(string $key, string $outputPath = null) : bool
{ {
return EncryptionHelper::decryptFile($this->getAbsolutePath(), $outputPath, $key); return EncryptionHelper::decryptFile($this->getAbsolutePath(), $outputPath ?? $this->getAbsolutePath(), $key);
} }
/** /**

View File

@ -15,7 +15,7 @@ declare(strict_types=1);
use Modules\Media\Models\NullMedia; use Modules\Media\Models\NullMedia;
/** @var \Modules\Media\Models\Media $media */ /** @var \Modules\Media\Models\Media $media */
$media = $this->getData('media') ?? new NullMedia(); $media = $this->media ?? new NullMedia();
$fp = \fopen(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath(), 'r'); $fp = \fopen(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath(), 'r');
\fpassthru($fp); \fpassthru($fp);

View File

@ -31,10 +31,10 @@ class ElementView extends MediaView
/** /**
* Media files * Media files
* *
* @var \Modules\Media\Models\Media * @var null|\Modules\Media\Models\Media
* @since 1.0.0 * @since 1.0.0
*/ */
protected Media $media; public ?Media $media = null;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -42,7 +42,7 @@ class ElementView extends MediaView
public function render(mixed ...$data) : string public function render(mixed ...$data) : string
{ {
/** @var array{0:\Modules\Media\Models\Media} $data */ /** @var array{0:\Modules\Media\Models\Media} $data */
$this->media = $data[0]; $this->media = $data[0] ?? $this->media;
return parent::render(); return parent::render();
} }

View File

@ -0,0 +1,21 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Template
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
use phpOMS\Uri\UriFactory;
?>
<audio width="100%" controls>
<source src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->id); ?>" type="audio/<?= $this->media->extension; ?>">
Your browser does not support HTML audio.
</audio>

View File

@ -0,0 +1,22 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Template
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
use phpOMS\Uri\UriFactory;
?>
<img alt="<?= $this->printHtml($this->media->name); ?>" style="max-width: 100%" src="<?= $this->media->id !== 0
? UriFactory::build('{/api}media/export?id=' . $this->media->id)
: UriFactory::build('{/api}media/export?path=' . \urlencode($this->media->getPath()));
?>">

View File

@ -0,0 +1,21 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Template
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
use phpOMS\Utils\Parser\Markdown\Markdown;
?>
<article><?= Markdown::parse(
$this->getFileContent(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath())
); ?></article>

View File

@ -0,0 +1,19 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\Media
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
use \phpOMS\Uri\UriFactory;
?>
<iframe class="col-simple" id="iHelperFrame" src="<?= UriFactory::build('Resources/mozilla/Pdf/web/viewer.html?file=' . \urlencode(UriFactory::build('{/api}media/export?id=' . $this->media->id))); ?>" allowfullscreen></iframe>

View File

@ -0,0 +1,3 @@
<pre class="textContent" data-tpl-text="/media/content" data-tpl-value="/media/content"><?= $this->printHtml(
$this->getFileContent(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath())
); ?>

View File

@ -0,0 +1,22 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Template
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
use phpOMS\Uri\UriFactory;
?>
<video width="100%" controls>
<source src="<?= UriFactory::build('{/api}media/export?id=' . $this->media->id); ?>" type="video/<?= $this->media->extension; ?>">
Your browser does not support HTML video.
</video>

View File

@ -74,7 +74,7 @@ class BaseView extends View
$this->form = $data[0]; $this->form = $data[0];
$this->name = $data[1] ?? 'UNDEFINED'; $this->name = $data[1] ?? 'UNDEFINED';
$this->virtualPath = $data[2] ?? $this->virtualPath; $this->virtualPath = $data[2] ?? $this->virtualPath;
$this->files = $data[3] ?? $this->files; $this->files = $data[3] ?? $this->files;
return parent::render(); return parent::render();
} }
} }

View File

@ -63,7 +63,7 @@ use phpOMS\Uri\UriFactory;
<section class="portlet"> <section class="portlet">
<div class="portlet-head"><?= $this->getHtml('Files', 'Media', 'Backend'); ?><i class="lni lni-download download btn end-xs"></i></div> <div class="portlet-head"><?= $this->getHtml('Files', 'Media', 'Backend'); ?><i class="lni lni-download download btn end-xs"></i></div>
<div class="slider"> <div class="slider">
<table class="default"> <table id="iFiles" class="default">
<thead> <thead>
<tr> <tr>
<td> <td>
@ -82,7 +82,7 @@ use phpOMS\Uri\UriFactory;
<?php foreach ($this->files as $file) : ?> <?php foreach ($this->files as $file) : ?>
<tr data-tpl-value="/id" data-value="" data-uuid="" data-name="media-list"> <tr data-tpl-value="/id" data-value="" data-uuid="" data-name="media-list">
<td><label class="radio" for="iFile-<?= $file->id; ?>"> <td><label class="radio" for="iFile-<?= $file->id; ?>">
<input id="iFile-<?= $file->id; ?>" type="radio" name="media_file" value="1"<?= \end($this->files)->id === $file->id ? ' checked' : ''; ?>> <input id="iFile-<?= $file->id; ?>" type="radio" name="media_file" value="<?= $file->id; ?>"<?= \end($this->files)->id === $file->id ? ' checked' : ''; ?>>
<span class="checkmark"></span> <span class="checkmark"></span>
</label> </label>
<td data-tpl-text="/id" data-tpl-value="/id" data-value=""><?= $this->printHtml((string) $file->id); ?></td> <td data-tpl-text="/id" data-tpl-value="/id" data-value=""><?= $this->printHtml((string) $file->id); ?></td>
@ -101,7 +101,7 @@ use phpOMS\Uri\UriFactory;
<section id="mediaFile" class="portlet col-simple"> <section id="mediaFile" class="portlet col-simple">
<div class="portlet-body col-simple"> <div class="portlet-body col-simple">
<?php if (!empty($this->files)) : ?> <?php if (!empty($this->files)) : ?>
<iframe class="col-simple" id="iMediaFile" src="Resources/mozilla/Pdf/web/viewer.html?file=<?= \urlencode(UriFactory::build('{/api}media/export?id=' . \end($this->files)->id)); ?>" loading="lazy" allowfullscreen></iframe> <iframe class="col-simple" id="iMediaFile" data-src="<?= UriFactory::build('{/api}media/export') . '?id={!#iFiles [name=media_file]:checked}&type=html'; ?>" allowfullscreen></iframe>
<?php else : ?> <?php else : ?>
<img width="100%" src="Web/Backend/img/logo_grey.png"> <img width="100%" src="Web/Backend/img/logo_grey.png">
<?php endif; ?> <?php endif; ?>