From 00b1c84163d9af1613d74fe9ed03ee84e9814712 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 12 Apr 2024 00:52:07 +0000 Subject: [PATCH] fix bugs --- Admin/Install/Navigation.install.json | 4 +- Admin/Install/SearchCommands.php | 2 + Controller/ApiController.php | 118 ++++++++++++++++-- Controller/BackendController.php | 8 +- Models/Media.php | 7 +- Models/MediaMapper.php | 6 +- Theme/Backend/Components/Media/audio.tpl.php | 2 +- .../Components/Media/audio_raw.tpl.php | 2 +- Theme/Backend/Components/Media/image.tpl.php | 4 +- .../Components/Media/image_raw.tpl.php | 4 +- Theme/Backend/Components/Media/pdf.tpl.php | 2 +- .../Backend/Components/Media/pdf_raw.tpl.php | 4 +- .../Components/Media/spreadsheet.tpl.php | 2 +- Theme/Backend/Components/Media/video.tpl.php | 2 +- .../Components/Media/video_raw.tpl.php | 2 +- Theme/Backend/Components/Media/word.tpl.php | 2 +- .../Components/Upload/upload-list.tpl.php | 4 +- Theme/Backend/media-list.tpl.php | 4 +- Theme/Backend/media-view.tpl.php | 2 +- 19 files changed, 141 insertions(+), 40 deletions(-) diff --git a/Admin/Install/Navigation.install.json b/Admin/Install/Navigation.install.json index 7560ea2..99e0693 100755 --- a/Admin/Install/Navigation.install.json +++ b/Admin/Install/Navigation.install.json @@ -5,7 +5,7 @@ "type": 2, "subtype": 1, "name": "Media", - "uri": "{/base}/media/list?{?}", + "uri": "{/base}/media/list", "target": "self", "icon": null, "order": 20, @@ -21,7 +21,7 @@ "type": 2, "subtype": 2, "name": "Media", - "uri": "{/base}/profile/view/media?{?}", + "uri": "{/base}/profile/view/media", "target": "self", "icon": null, "order": 3, diff --git a/Admin/Install/SearchCommands.php b/Admin/Install/SearchCommands.php index ff9121e..8259aad 100644 --- a/Admin/Install/SearchCommands.php +++ b/Admin/Install/SearchCommands.php @@ -22,6 +22,7 @@ return [ [ 'dest' => '\Modules\Media\Controller\SearchController:searchGeneral', 'verb' => RouteVerb::ANY, + 'active' => true, 'permission' => [ 'module' => SearchController::NAME, 'type' => PermissionType::READ, @@ -33,6 +34,7 @@ return [ [ 'dest' => '\Modules\Media\Controller\SearchController:searchTag', 'verb' => RouteVerb::ANY, + 'active' => true, 'permission' => [ 'module' => SearchController::NAME, 'type' => PermissionType::READ, diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 272e689..6130043 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -19,6 +19,7 @@ use Modules\Admin\Models\NullAccount; use Modules\Media\Models\Collection; use Modules\Media\Models\CollectionMapper; use Modules\Media\Models\Media; +use Modules\Media\Models\MediaClass; use Modules\Media\Models\MediaContent; use Modules\Media\Models\MediaContentMapper; use Modules\Media\Models\MediaMapper; @@ -154,7 +155,8 @@ final class ApiController extends Controller pathSettings: $request->getDataInt('pathsettings') ?? PathSettings::RANDOM_PATH, // IMPORTANT!!! hasAccountRelation: $request->getDataBool('link_account') ?? false, readContent: $request->getDataBool('parse_content') ?? false, - unit: $request->getDataInt('unit') + unit: $request->getDataInt('unit'), + createCollection: $request->getDataBool('create_collection') ?? false, ); $ids = []; @@ -317,11 +319,16 @@ final class ApiController extends Controller int $pathSettings = PathSettings::RANDOM_PATH, bool $hasAccountRelation = true, bool $readContent = false, - ?int $unit = null - ) : array + ?int $unit = null, + bool $createCollection = true, + ?int $type = null, + ?int $rel = null, + string $mapper = '', + string $field = '' + ) : Collection { if (empty($files)) { - return []; + return new NullCollection(); } $outputDir = ''; @@ -333,11 +340,11 @@ final class ApiController extends Controller $outputDir = \rtrim($basePath, '/\\'); $absolute = true; } else { - return []; + return new NullCollection(); } if (!Guard::isSafePath($outputDir, __DIR__ . '/../../../')) { - return []; + return new NullCollection(); } $upload = new UploadFile(); @@ -356,7 +363,7 @@ final class ApiController extends Controller // Possible: name != filename (name = database media name, filename = name on the file system) $stat['name'] = $sameLength ? $names[$nCounter] : $stat['filename']; - $created[] = self::createDbEntry( + $media = self::createDbEntry( $stat, $account, $virtualPath, @@ -366,9 +373,34 @@ final class ApiController extends Controller password: $password, isEncrypted: !empty($encryptionKey) ); + + // Create relation to type + if (!empty($type)) { + $this->createModelRelation($account, $media->id, $type, MediaMapper::class, 'types', '', '127.0.0.1'); + } + + // Create relation to model + if (!empty($rel)) { + $this->createModelRelation($account, $rel, $media->id, $mapper, $field, '', '127.0.0.1'); + } + + $created[] = $media; } - return $created; + if (!$createCollection) { + $collection = new NullCollection(); + $collection->sources = $created; + + return $collection; + } + + $collection = $this->createRecursiveMediaCollection($virtualPath, $account, $basePath); + foreach ($created as $media) { + $this->createModelRelation($account, $collection->id, $media->id, CollectionMapper::class, 'sources', '','127.0.0.1'); + $collection->sources[] = $media; + } + + return $collection; } /** @@ -975,11 +1007,19 @@ final class ApiController extends Controller */ public function createRecursiveMediaCollection(string $path, int $account, string $physicalPath = '') : Collection { - $status = false; + $status = true; if (!empty($physicalPath)) { $status = \is_dir($physicalPath) ? true : \mkdir($physicalPath, 0755, true); } + if (!$status) { + $this->app->logger?->error(\phpOMS\Log\FileLogger::MSG_FULL, [ + 'message' => 'Couldn\'t create directory "' . $physicalPath . '"', + 'line' => __LINE__, + 'file' => self::class, + ]); + } + $virtualPath = \trim($path, '/'); $virtualPaths = \explode('/', $virtualPath); $tempVirtualPaths = $virtualPaths; @@ -996,10 +1036,15 @@ final class ApiController extends Controller $virtual = '/' . \implode('/', $tempVirtualPaths); /** @var Collection $parentCollection */ - $parentCollection = CollectionMapper::getParentCollection($virtual)->execute(); + $parentCollection = CollectionMapper::get() + ->where('virtualPath', \dirname($virtual)) + ->where('class', MediaClass::COLLECTION) + ->where('name', \basename($virtual)) + ->limit(1) + ->execute(); if ($parentCollection->id > 0) { - $real = $parentCollection->getPath(); + $real = \rtrim($parentCollection->path, '/') . '/' . $parentCollection->name; break; } @@ -1012,10 +1057,10 @@ final class ApiController extends Controller $childCollection = new Collection(); $childCollection->name = $virtualPaths[$i]; $childCollection->createdBy = new NullAccount($account); - $childCollection->setVirtualPath('/'. \ltrim($virtual, '/')); + $childCollection->setVirtualPath('/'. \trim($virtual, '/')); // We assume that the new path is real path of the first found parent directory + the new virtual path - $childCollection->setPath(\rtrim($real, '/') . '/' . \ltrim($newVirtual, '/')); + $childCollection->setPath(\rtrim($real, '/') . '/' . \trim($newVirtual, '/')); $this->createModel($account, $childCollection, CollectionMapper::class, 'collection', '127.0.0.1'); $this->createModelRelation( @@ -1035,6 +1080,53 @@ final class ApiController extends Controller return $parentCollection; } + public function addMediaToCollectionAndModel( + int $account, + array $files, + ?int $rel = null, string $mapper = '', string $field = '', + string $collectionPath = '' + ) : void + { + $mediaFiles = MediaMapper::getAll()->where('id', $files)->executeGetArray(); + $collection = null; + + foreach ($mediaFiles as $media) { + if ($rel !== null) { + $this->createModelRelation($account, $rel, $media->id, $mapper, $field, '', '127.0.0.1'); + } + + if (!empty($addToCollection)) { + $ref = new Reference(); + $ref->name = $media->name; + $ref->source = new NullMedia($media->id); + $ref->createdBy = new NullAccount($account); + $ref->setVirtualPath($collectionPath); + + $this->createModel($account, $ref, ReferenceMapper::class, 'media_reference', '127.0.0.1'); + + if ($collection === null) { + /** @var \Modules\Media\Models\Collection $collection */ + $collection = CollectionMapper::get() + ->where('virtualPath', \dirname($collectionPath)) + ->where('class', MediaClass::COLLECTION) + ->where('name', \basename($collectionPath)) + ->limit(1) + ->execute(); + + if ($collection->id === 0) { + $collection = $this->app->moduleManager->get('Media', 'Api')->createRecursiveMediaCollection( + $collectionPath, + $account, + __DIR__ . '/../../../Modules/Media/Files' . $collectionPath + ); + } + } + + $this->createModelRelation($account, $collection->id, $ref->id, CollectionMapper::class, 'sources', '', '127.0.0.1'); + } + } + } + /** * Api method to create media file. * diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 7bda55a..e294cf1 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -94,10 +94,12 @@ final class BackendController extends Controller } /** @var Media[] $media */ - $media = $mediaMapper->execute(); + $media = $mediaMapper->executeGetArray(); - $collectionMapper = CollectionMapper::getParentCollection($path) - ->where('tags/title/language', $request->header->l11n->language); + $collectionMapper = CollectionMapper::getAll() + ->where('virtualPath', \dirname($path)) + ->where('class', MediaClass::COLLECTION) + ->where('name', \basename($path)); if (!$hasPermission) { $permWhere = PermissionAbstractMapper::helper($this->app->dbPool->get('select')) diff --git a/Models/Media.php b/Models/Media.php index 591bc44..1b4495e 100755 --- a/Models/Media.php +++ b/Models/Media.php @@ -369,7 +369,7 @@ class Media implements \JsonSerializable */ public function setPath(string $path) : void { - $this->path = \strtr($path, '\\', '/'); + $this->path = \rtrim(\strtr($path, '\\', '/'), '/'); } /** @@ -381,7 +381,10 @@ class Media implements \JsonSerializable */ public function setVirtualPath(string $path) : void { - $this->virtualPath = \strtr($path, '\\', '/'); + $this->virtualPath = \rtrim(\strtr($path, '\\', '/'), '/'); + if ($this->virtualPath === '') { + $this->virtualPath = '/'; + } } /** diff --git a/Models/MediaMapper.php b/Models/MediaMapper.php index 4bfe03c..9861775 100755 --- a/Models/MediaMapper.php +++ b/Models/MediaMapper.php @@ -155,11 +155,11 @@ class MediaMapper extends DataMapperFactory * and in fact most of the time it is different. This is because the location on a hard drive or web * drive should not have any impact on the media file/media structure in the application. * - * As a result media files are structured by virutal path in the app, by file path on the file system + * As a result media files are structured by virtual path in the app, by file path on the file system * and by Collections which can have sub-collections as well. Collections allow to reference files - * in a different virtual path and are therfore similar to "symlinks", except that they don't reference + * in a different virtual path and are therefore similar to "symlinks", except that they don't reference * a file but create a new virtual media model which groups other media models together in a new virtual - * path if so desired without deleting or moving the orginal media files. + * path if so desired without deleting or moving the original media files. * * @param string $virtualPath Virtual path * @param int $status Media status diff --git a/Theme/Backend/Components/Media/audio.tpl.php b/Theme/Backend/Components/Media/audio.tpl.php index 1893d4f..746d353 100755 --- a/Theme/Backend/Components/Media/audio.tpl.php +++ b/Theme/Backend/Components/Media/audio.tpl.php @@ -18,7 +18,7 @@ use phpOMS\Uri\UriFactory;
diff --git a/Theme/Backend/Components/Media/audio_raw.tpl.php b/Theme/Backend/Components/Media/audio_raw.tpl.php index ecdd6a6..8cdab30 100755 --- a/Theme/Backend/Components/Media/audio_raw.tpl.php +++ b/Theme/Backend/Components/Media/audio_raw.tpl.php @@ -18,6 +18,6 @@ use phpOMS\Uri\UriFactory; \ No newline at end of file diff --git a/Theme/Backend/Components/Media/image.tpl.php b/Theme/Backend/Components/Media/image.tpl.php index 1f1ac84..c1000e8 100755 --- a/Theme/Backend/Components/Media/image.tpl.php +++ b/Theme/Backend/Components/Media/image.tpl.php @@ -28,8 +28,8 @@ use phpOMS\Uri\UriFactory;
<?= $this->printHtml($this->media->name); ?>
media->content->content)) : ?> diff --git a/Theme/Backend/Components/Media/image_raw.tpl.php b/Theme/Backend/Components/Media/image_raw.tpl.php index 0b64211..07ff3d8 100755 --- a/Theme/Backend/Components/Media/image_raw.tpl.php +++ b/Theme/Backend/Components/Media/image_raw.tpl.php @@ -18,6 +18,6 @@ use phpOMS\Uri\UriFactory; <?= $this->printHtml($this->media->name); ?> diff --git a/Theme/Backend/Components/Media/pdf.tpl.php b/Theme/Backend/Components/Media/pdf.tpl.php index 71e4a9a..a1b2677 100755 --- a/Theme/Backend/Components/Media/pdf.tpl.php +++ b/Theme/Backend/Components/Media/pdf.tpl.php @@ -26,7 +26,7 @@ use \phpOMS\Uri\UriFactory;
- +
diff --git a/Theme/Backend/Components/Media/pdf_raw.tpl.php b/Theme/Backend/Components/Media/pdf_raw.tpl.php index e9ce032..d92bdb9 100755 --- a/Theme/Backend/Components/Media/pdf_raw.tpl.php +++ b/Theme/Backend/Components/Media/pdf_raw.tpl.php @@ -25,7 +25,7 @@ use \phpOMS\Uri\UriFactory; src="media->id === 0 - ? \urlencode(UriFactory::build('{/api}media/export?path=' . $this->media->getPath())) - : \urlencode(UriFactory::build('{/api}media/export?id=' . $this->media->id) + ? \urlencode(UriFactory::build('{/api}media/export?path=' . $this->media->getPath()) . '&csrf={$CSRF}') + : \urlencode(UriFactory::build('{/api}media/export?id=' . $this->media->id . '&csrf={$CSRF}') ))); ?>" allowfullscreen> \ No newline at end of file diff --git a/Theme/Backend/Components/Media/spreadsheet.tpl.php b/Theme/Backend/Components/Media/spreadsheet.tpl.php index f6edeef..371b14f 100755 --- a/Theme/Backend/Components/Media/spreadsheet.tpl.php +++ b/Theme/Backend/Components/Media/spreadsheet.tpl.php @@ -29,7 +29,7 @@ Autoloader::addPath(__DIR__ . '/../../../../../../Resources/');
- +
diff --git a/Theme/Backend/Components/Media/video.tpl.php b/Theme/Backend/Components/Media/video.tpl.php index 8585ac3..eac8ab0 100755 --- a/Theme/Backend/Components/Media/video.tpl.php +++ b/Theme/Backend/Components/Media/video.tpl.php @@ -18,7 +18,7 @@ use phpOMS\Uri\UriFactory;
diff --git a/Theme/Backend/Components/Media/video_raw.tpl.php b/Theme/Backend/Components/Media/video_raw.tpl.php index 7b2aff8..9ce4084 100755 --- a/Theme/Backend/Components/Media/video_raw.tpl.php +++ b/Theme/Backend/Components/Media/video_raw.tpl.php @@ -18,6 +18,6 @@ use phpOMS\Uri\UriFactory; diff --git a/Theme/Backend/Components/Media/word.tpl.php b/Theme/Backend/Components/Media/word.tpl.php index 942c2f5..9010476 100755 --- a/Theme/Backend/Components/Media/word.tpl.php +++ b/Theme/Backend/Components/Media/word.tpl.php @@ -17,6 +17,6 @@ use phpOMS\Uri\UriFactory; ?>
- +
diff --git a/Theme/Backend/Components/Upload/upload-list.tpl.php b/Theme/Backend/Components/Upload/upload-list.tpl.php index 1f850e9..bc8761c 100755 --- a/Theme/Backend/Components/Upload/upload-list.tpl.php +++ b/Theme/Backend/Components/Upload/upload-list.tpl.php @@ -76,7 +76,7 @@ use phpOMS\Uri\UriFactory; "}]}]' + data-action='[{"listener": "change", "action": [{"key": 1, "type": "dom.set", "selector": "#iMediaFile", "value": ""}]}]' data-limit="0" data-active="true" data-form="form; ?>" @@ -116,7 +116,7 @@ use phpOMS\Uri\UriFactory;
files)) : ?> - + diff --git a/Theme/Backend/media-list.tpl.php b/Theme/Backend/media-list.tpl.php index ed7bc4d..7bddb4f 100755 --- a/Theme/Backend/media-list.tpl.php +++ b/Theme/Backend/media-list.tpl.php @@ -209,6 +209,7 @@ $next = empty($media) ? '{/base}/media/list' : '{/base}/media/list?{?}&offse printHtml($value->name); ?> + printHtml($value->extension); ?> getHtml('Previous', '0', '0'); ?> getHtml('Next', '0', '0'); ?> - + getHtml('Download'); ?>
diff --git a/Theme/Backend/media-view.tpl.php b/Theme/Backend/media-view.tpl.php index 6d53eec..0ac14f8 100644 --- a/Theme/Backend/media-view.tpl.php +++ b/Theme/Backend/media-view.tpl.php @@ -121,7 +121,7 @@ echo $this->data['nav']->render(); data-uri=""> getHtml('Download'); ?> filePathFunction($media, $this->request->getData('sub') ?? '');