Fix media paths/references

This commit is contained in:
Dennis Eichhorn 2022-02-13 14:36:26 +01:00
parent 3733ad68ea
commit d80cda27b1
13 changed files with 356 additions and 54 deletions

View File

@ -87,12 +87,6 @@
"default": null,
"null": true
},
"media_path": {
"name": "media_path",
"type": "VARCHAR(255)",
"default": null,
"null": true
},
"media_versioned": {
"name": "media_versioned",
"type": "TINYINT",
@ -136,8 +130,8 @@
"default": null,
"null": true
},
"media_collection": {
"name": "media_collection",
"media_class": {
"name": "media_class",
"type": "TINYINT",
"default": null,
"null": true
@ -148,6 +142,13 @@
"default": null,
"null": true
},
"media_source": {
"name": "media_source",
"type": "INT",
"null": true,
"foreignTable": "media",
"foreignKey": "media_id"
},
"media_created_by": {
"name": "media_created_by",
"type": "INT",

View File

@ -520,6 +520,53 @@ final class ApiController extends Controller
return $mediaCollection;
}
public function createRecursiveMediaCollection(string $basePath, string $path, int $account, string $physicalPath = '') : Collection
{
$status = false;
if (!empty($physicalPath)) {
$status = !\is_dir($physicalPath) ? \mkdir($physicalPath, 0755, true) : true;
}
$path = \trim($path, '/');
$paths = \explode('/', $path);
$tempPaths = $paths;
$length = \count($paths);
$temp = '';
/** @var Collection $parentCollection */
$parentCollection = null;
for ($i = $length; $i > 0; --$i) {
$temp = '/' . \implode('/', $tempPaths);
/** @var Collection $parentCollection */
$parentCollection = CollectionMapper::getParentCollection($temp)->execute();
if ($parentCollection->getId() > 0) {
break;
}
\array_pop($tempPaths);
}
for (; $i < $length; ++$i) {
/* Create collection */
$childCollection = new Collection();
$childCollection->name = $paths[$i];
$childCollection->createdBy = new NullAccount($account);
$childCollection->setVirtualPath('/'. \ltrim($temp, '/'));
$childCollection->setPath('/Modules/Media/Files' . $temp);
CollectionMapper::create()->execute($childCollection);
CollectionMapper::writer()->createRelationTable('sources', [$childCollection->getId()], $parentCollection->getId());
$parentCollection = $childCollection;
$temp .= '/' . $paths[$i];
}
return $parentCollection;
}
/**
* Api method to create media file.
*

View File

@ -34,6 +34,7 @@ use phpOMS\Account\PermissionType;
use phpOMS\Contract\RenderableInterface;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Utils\StringUtils;
use phpOMS\Views\View;
/**
@ -131,7 +132,16 @@ final class BackendController extends Controller
}
if ($collection instanceof Collection && !($collection instanceof NullCollection)) {
$media += $collection->getSources();
$collectionSources = $collection->getSources();
foreach ($collectionSources as $source) {
foreach ($media as $obj) {
if ($obj->getId() === $source->getId()) {
continue 2;
}
}
$media[] = $source;
}
/** @var string[] $glob */
$glob = $collection->isAbsolute
@ -148,6 +158,7 @@ final class BackendController extends Controller
foreach ($media as $obj) {
if ($obj->name === $basename
|| $obj->name . '.' . $obj->extension === $basename
|| StringUtils::endsWith(\realpath($file), $obj->getPath())
) {
continue 2;
}
@ -238,6 +249,7 @@ final class BackendController extends Controller
}
}
$view->addData('account', $this->app->accountManager->get($request->header->account));
$view->addData('media', $media);
return $view;

View File

@ -43,10 +43,10 @@ class Collection extends Media implements \Iterator
/**
* Is collection.
*
* @var bool
* @var int
* @since 1.0.0
*/
protected bool $collection = true;
public int $class = MediaClass::COLLECTION;
/**
* Set sources.

View File

@ -50,30 +50,6 @@ final class CollectionMapper extends MediaMapper
*/
public const MODEL = Collection::class;
/**
* Primary table.
*
* @var string
* @since 1.0.0
*/
public const TABLE = 'media';
/**
* Created at.
*
* @var string
* @since 1.0.0
*/
public const CREATED_AT = 'media_created_at';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
public const PRIMARYFIELD ='media_id';
/**
* Get media based on virtual path.
*
@ -102,7 +78,7 @@ final class CollectionMapper extends MediaMapper
->with('createdBy')
->with('tags')
->where('isHidden', $hidden)
->where('collection', true);
->where('class', MediaClass::COLLECTION);
}
/**

View File

@ -133,6 +133,14 @@ class Media implements \JsonSerializable
*/
public string $descriptionRaw = '';
/**
* Resource id.
*
* @var null|Media
* @since 1.0.0
*/
public ?Media $source = null;
/**
* Media encryption nonce.
*
@ -158,12 +166,12 @@ class Media implements \JsonSerializable
public bool $isHidden = false;
/**
* Is collection.
* Media class.
*
* @var bool
* @var int
* @since 1.0.0
*/
protected bool $collection = false;
public int $class = MediaClass::FILE;
/**
* Tags.

45
Models/MediaClass.php Normal file
View File

@ -0,0 +1,45 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\Media\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Media\Models;
/**
* Media class.
*
* @package Modules\Media\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
use phpOMS\Stdlib\Base\Enum;
/**
* Database type enum.
*
* Database types that are supported by the application
*
* @package phpOMS\DataStorage\Database
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
abstract class MediaClass extends Enum
{
public const FILE = 0;
public const COLLECTION = 1;
public const REFERENCE = 2;
}

View File

@ -50,7 +50,8 @@ class MediaMapper extends DataMapperFactory
'media_password' => ['name' => 'media_password', 'type' => 'string', 'internal' => 'password'],
'media_extension' => ['name' => 'media_extension', 'type' => 'string', 'internal' => 'extension'],
'media_size' => ['name' => 'media_size', 'type' => 'int', 'internal' => 'size'],
'media_collection' => ['name' => 'media_collection', 'type' => 'bool', 'internal' => 'collection'],
'media_source' => ['name' => 'media_source', 'type' => 'int', 'internal' => 'source'],
'media_class' => ['name' => 'media_class', 'type' => 'int', 'internal' => 'class'],
'media_created_by' => ['name' => 'media_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true],
'media_created_at' => ['name' => 'media_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true],
];
@ -79,6 +80,10 @@ class MediaMapper extends DataMapperFactory
'mapper' => MediaTypeMapper::class,
'external' => 'media_type',
],
'source' => [
'mapper' => self::class,
'external' => 'media_source',
],
];
/**
@ -154,6 +159,7 @@ class MediaMapper extends DataMapperFactory
{
return self::getAll()
->with('createdBy')
->with('source')
->with('tags')
->with('tags/title')
->where('virtualPath', $virtualPath)
@ -176,7 +182,9 @@ class MediaMapper extends DataMapperFactory
return self::get()
->with('sources')
->with('source')
->where('virtualPath', $virtualPath)
->where('class', MediaClass::COLLECTION)
->where('name', $name);
}
}

39
Models/NullReference.php Normal file
View File

@ -0,0 +1,39 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\Media\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Media\Models;
/**
* Reference class.
*
* @package Modules\Media\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class NullReference extends Reference
{
/**
* Constructor
*
* @param int $id Model id
*
* @since 1.0.0
*/
public function __construct(int $id = 0)
{
$this->id = $id;
parent::__construct();
}
}

144
Models/Reference.php Normal file
View File

@ -0,0 +1,144 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\Media\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Media\Models;
/**
* Reference class.
*
* @package Modules\Media\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class Reference extends Media
{
/**
* Extension name.
*
* @var string
* @since 1.0.0
*/
public string $extension = 'reference';
/**
* Is reference.
*
* @var int
* @since 1.0.0
*/
public int $class = MediaClass::REFERENCE;
/**
* Set sources.
*
* @param array $sources Source array
*
* @return void
*
* @since 1.0.0
*/
public function setSources(array $sources) : void
{
$this->sources = $sources;
}
/**
* Set sources.
*
* @param Media $source Source
*
* @return void
*
* @since 1.0.0
*/
public function addSource(Media $source) : void
{
$this->sources[] = $source;
}
/**
* Get sources.
*
* @return Media[]
*
* @since 1.0.0
*/
public function getSources() : array
{
return $this->sources;
}
/**
* Get media element by its name.
*
* @param string $name Name of the media element
*
* @return Media
*
* @since 1.0.0
*/
public function getSourceByName(string $name) : Media
{
foreach ($this->sources as $source) {
if ($source->name === $name) {
return $source;
}
}
return new NullMedia();
}
/**
* {@inheritdoc}
*/
public function rewind() : void
{
\reset($this->sources);
}
/**
* {@inheritdoc}
*/
public function current() : Media
{
$current = \current($this->sources);
return $current === false ? $this : $current;
}
/**
* {@inheritdoc}
*/
public function key() : ?int
{
return \key($this->sources);
}
/**
* {@inheritdoc}
*/
public function next() : void
{
\next($this->sources);
}
/**
* {@inheritdoc}
*/
public function valid() : bool
{
return \current($this->sources) !== false;
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules\Media\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace Modules\Media\Models;
use Modules\Admin\Models\Account;
use phpOMS\DataStorage\Database\Mapper\ReadMapper;
/**
* Mapper class.
*
* @package Modules\Media\Models
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class ReferenceMapper extends MediaMapper
{
}

View File

@ -16,17 +16,3 @@ require_once __DIR__ . '/../../../../../../phpOMS/Autoloader.php';
use phpOMS\Autoloader;
Autoloader::addPath(__DIR__ . '/../../../../../../Resources/');
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Writer\Html;
$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

@ -12,6 +12,8 @@
*/
declare(strict_types=1);
use Modules\Media\Models\MediaClass;
use Modules\Media\Models\Reference;
use phpOMS\System\File\FileUtils;
use phpOMS\Uri\UriFactory;
use phpOMS\Utils\Converter\FileSizeType;
@ -171,6 +173,10 @@ $next = empty($media) ? '{/prefix}media/list' : '{/prefix}media/list?{?}&id=
foreach ($media as $key => $value) :
++$count;
if ($value->class === MediaClass::REFERENCE) {
$value = $value->source;
}
$url = $value->extension === 'collection'
? UriFactory::build('{/prefix}media/list?path=' . \rtrim($value->getVirtualPath(), '/') . '/' . $value->name)
: UriFactory::build('{/prefix}media/single?id=' . $value->getId()