diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 52da41e..d74b053 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -34,6 +34,8 @@ use Modules\Media\Models\Reference; use Modules\Media\Models\ReferenceMapper; use Modules\Media\Models\UploadFile; use Modules\Media\Models\UploadStatus; +use Modules\Media\Theme\Backend\Components\Media\ElementView; +use Modules\Media\Views\MediaView; use Modules\Tag\Models\NullTag; use phpOMS\Account\PermissionType; use phpOMS\Application\ApplicationAbstract; @@ -1137,6 +1139,16 @@ final class ApiController extends Controller $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 { $path = ''; @@ -1148,6 +1160,8 @@ final class ApiController extends Controller $path = '../../../Temp/' . $randomName . '.' . $media->getExtension(); $absolutePath = __DIR__ . '/' . $path; + + ++$counter; } while (!\is_file($absolutePath) && $counter < 1000); if ($counter >= 1000) { @@ -1182,8 +1196,8 @@ final class ApiController extends Controller */ public function createView(Media $media, RequestAbstract $request, ResponseAbstract $response) : View { - $view = new View($this->app->l11nManager, $request, $response); - $view->data['media'] = $media; + $view = new ElementView($this->app->l11nManager, $request, $response); + $view->media = $media; if (!\headers_sent()) { $response->endAllOutputBuffering(); // for large files @@ -1205,6 +1219,34 @@ final class ApiController extends Controller $view->data['head'] = $head; 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 'xlsx': $view->setTemplate('/Modules/Media/Theme/Api/spreadsheetAsHtml'); @@ -1213,6 +1255,15 @@ final class ApiController extends Controller case 'docx': $view->setTemplate('/Modules/Media/Theme/Api/wordAsHtml'); 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'); } } diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 039e826..4eee326 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -96,7 +96,8 @@ final class BackendController extends Controller /** @var Media[] $media */ $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) { $permWhere = PermissionAbstractMapper::helper($this->app->dbPool->get('select')) @@ -112,9 +113,10 @@ final class BackendController extends Controller $collectionMapper->where('', $permWhere); } + /** @var \Modules\Media\Models\Collection $collection */ $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->name = \basename($path); $collection->setVirtualPath(\dirname($path)); diff --git a/Models/Media.php b/Models/Media.php index 9a0d7d7..756fc66 100755 --- a/Models/Media.php +++ b/Models/Media.php @@ -247,7 +247,7 @@ class Media implements \JsonSerializable */ 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 { - return EncryptionHelper::decryptFile($this->getAbsolutePath(), $outputPath, $key); + return EncryptionHelper::decryptFile($this->getAbsolutePath(), $outputPath ?? $this->getAbsolutePath(), $key); } /** diff --git a/Theme/Api/render.tpl.php b/Theme/Api/render.tpl.php index b5da545..b336813 100755 --- a/Theme/Api/render.tpl.php +++ b/Theme/Api/render.tpl.php @@ -15,7 +15,7 @@ declare(strict_types=1); use Modules\Media\Models\NullMedia; /** @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'); \fpassthru($fp); diff --git a/Theme/Backend/Components/Media/ElementView.php b/Theme/Backend/Components/Media/ElementView.php index 204f173..ad3eb25 100755 --- a/Theme/Backend/Components/Media/ElementView.php +++ b/Theme/Backend/Components/Media/ElementView.php @@ -31,10 +31,10 @@ class ElementView extends MediaView /** * Media files * - * @var \Modules\Media\Models\Media + * @var null|\Modules\Media\Models\Media * @since 1.0.0 */ - protected Media $media; + public ?Media $media = null; /** * {@inheritdoc} @@ -42,7 +42,7 @@ class ElementView extends MediaView public function render(mixed ...$data) : string { /** @var array{0:\Modules\Media\Models\Media} $data */ - $this->media = $data[0]; + $this->media = $data[0] ?? $this->media; return parent::render(); } diff --git a/Theme/Backend/Components/Media/audio_raw.tpl.php b/Theme/Backend/Components/Media/audio_raw.tpl.php new file mode 100755 index 0000000..ddd897f --- /dev/null +++ b/Theme/Backend/Components/Media/audio_raw.tpl.php @@ -0,0 +1,21 @@ + + \ No newline at end of file diff --git a/Theme/Backend/Components/Media/image_raw.tpl.php b/Theme/Backend/Components/Media/image_raw.tpl.php new file mode 100755 index 0000000..3688052 --- /dev/null +++ b/Theme/Backend/Components/Media/image_raw.tpl.php @@ -0,0 +1,22 @@ + + +<?= $this->printHtml($this->media->name); ?> diff --git a/Theme/Backend/Components/Media/markdown_raw.tpl.php b/Theme/Backend/Components/Media/markdown_raw.tpl.php new file mode 100755 index 0000000..e4bfbdd --- /dev/null +++ b/Theme/Backend/Components/Media/markdown_raw.tpl.php @@ -0,0 +1,21 @@ + + +
getFileContent(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath()) +); ?>
diff --git a/Theme/Backend/Components/Media/pdf_raw.tpl.php b/Theme/Backend/Components/Media/pdf_raw.tpl.php new file mode 100755 index 0000000..382065c --- /dev/null +++ b/Theme/Backend/Components/Media/pdf_raw.tpl.php @@ -0,0 +1,19 @@ + + + \ No newline at end of file diff --git a/Theme/Backend/Components/Media/text_raw.tpl.php b/Theme/Backend/Components/Media/text_raw.tpl.php new file mode 100755 index 0000000..03ba2e2 --- /dev/null +++ b/Theme/Backend/Components/Media/text_raw.tpl.php @@ -0,0 +1,3 @@ +
printHtml(
+    $this->getFileContent(($this->media->isAbsolute ? '' : __DIR__ . '/../../../../../../') . $this->media->getPath())
+); ?>
\ No newline at end of file
diff --git a/Theme/Backend/Components/Media/video_raw.tpl.php b/Theme/Backend/Components/Media/video_raw.tpl.php
new file mode 100755
index 0000000..6cb58a6
--- /dev/null
+++ b/Theme/Backend/Components/Media/video_raw.tpl.php
@@ -0,0 +1,22 @@
+
+
+
diff --git a/Theme/Backend/Components/Upload/BaseView.php b/Theme/Backend/Components/Upload/BaseView.php
index 4f8fa07..a79acc5 100755
--- a/Theme/Backend/Components/Upload/BaseView.php
+++ b/Theme/Backend/Components/Upload/BaseView.php
@@ -74,7 +74,7 @@ class BaseView extends View
         $this->form        = $data[0];
         $this->name        = $data[1] ?? 'UNDEFINED';
         $this->virtualPath = $data[2] ?? $this->virtualPath;
-        $this->files = $data[3] ?? $this->files;
+        $this->files       = $data[3] ?? $this->files;
         return parent::render();
     }
 }
diff --git a/Theme/Backend/Components/Upload/upload-list.tpl.php b/Theme/Backend/Components/Upload/upload-list.tpl.php
index bb33e69..a3c5c50 100755
--- a/Theme/Backend/Components/Upload/upload-list.tpl.php
+++ b/Theme/Backend/Components/Upload/upload-list.tpl.php
@@ -63,7 +63,7 @@ use phpOMS\Uri\UriFactory;
         
getHtml('Files', 'Media', 'Backend'); ?>
- +
@@ -101,7 +101,7 @@ use phpOMS\Uri\UriFactory;
files)) : ?> - +
@@ -82,7 +82,7 @@ use phpOMS\Uri\UriFactory; files as $file) : ?>
printHtml((string) $file->id); ?>