Media improvements

This commit is contained in:
Matias Griese
2022-02-03 19:47:46 +02:00
parent 81e2751849
commit d4cb9d3f98
4 changed files with 25 additions and 26 deletions

View File

@@ -9,7 +9,7 @@
namespace Grav\Common\Media\Events;
use Grav\Common\Media\Factories\FolderMediaFactory;
use Grav\Common\Media\Factories\LocalMediaFactory;
use Grav\Common\Media\Factories\MediaFactory;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -34,6 +34,6 @@ class MediaEventSubscriber implements EventSubscriberInterface
*/
public function onMediaFactoryInit(MediaFactory $factory): void
{
$factory->register(new FolderMediaFactory());
$factory->register(new LocalMediaFactory());
}
}

View File

@@ -16,14 +16,14 @@ use Grav\Common\Page\Media;
/**
*
*/
class FolderMediaFactory implements MediaFactoryInterface
class LocalMediaFactory implements MediaFactoryInterface
{
/**
* @return string[]
*/
public function getCollectionTypes(): array
{
return ['folder'];
return ['local'];
}
/**

View File

@@ -88,7 +88,7 @@ final class MediaFactory implements MediaFactoryInterface
*/
public function createCollection(array $settings): ?MediaCollectionInterface
{
$type = $settings['type'] ?? 'folder';
$type = $settings['type'] ?? 'local';
$factory = $this->collectionTypes[$type] ?? null;
if ($factory) {
return $factory->createCollection($settings);

View File

@@ -50,12 +50,18 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
/** @var string */
protected const VERSION = '1';
/** @var string|null */
protected $path;
/** @var array */
protected $index = [];
/** @var array */
protected $items = [];
/** @var string|null */
protected $path;
/** @var array|null */
protected $media_order;
/** @var array */
protected $standard_exif = ['FileSize', 'MimeType', 'height', 'width'];
/** @var int */
protected $indexTimeout = 0;
/** @var array */
protected $images = [];
/** @var array */
@@ -64,12 +70,6 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
protected $audios = [];
/** @var array */
protected $files = [];
/** @var array|null */
protected $media_order;
/** @var array */
protected $standard_exif = ['FileSize', 'MimeType', 'height', 'width'];
/** @var int */
protected $indexTimeout = 0;
/**
* Return media path.
@@ -382,30 +382,25 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
//$exifReader = $this->getExifReader();
$list = [];
foreach ($files as $info) {
foreach ($files as $filename => $info) {
// Ignore markdown, frontmatter and dot files. Also ignore all files which are not listed in media types.
$basename = $info['basename'];
$extension = $info['extension'] ?? '';
$params = $media_types[strtolower($extension)] ?? [];
if (!$params || $extension === 'md' || str_starts_with($basename, '.') || \in_array($basename, static::$ignore, true)) {
if (!$params || $extension === 'md' || str_starts_with($filename, '.') || \in_array($filename, static::$ignore, true)) {
continue;
}
$filepath = $info['filepath'] ?? (($info['dirname'] ? $info['dirname'] . '/' : '') . $basename);
$filename = $info['filename'];
$type = $params['type'] ?? 'file';
$info['type'] = $type;
$info['mime'] = $params['mime'];
if ($info['dirname'] === '.') {
$info['dirname'] = '';
}
$info['filepath'] = $filepath;
$info['basename'] = $filename;
$info['filename'] = $basename;
$info['basename'] = $info['filename'];
$info['filename'] = $filename;
if (null !== $cached) {
$existing = $cached[$filepath] ?? null;
$existing = $cached[$filename] ?? null;
if ($existing && $existing['size'] === $info['size'] && $existing['modified'] === $info['modified']) {
// Append cached data.
$info += $existing;
@@ -433,7 +428,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
}
}
$list[$basename] = $info;
$list[$filename] = $info;
}
return $list;
@@ -451,6 +446,10 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
return;
}
if (!isset($info['filepath'])) {
$info['filepath'] = ($info['dirname'] ? $info['dirname'] . '/' : '') . $info['filename'];
}
$config = $this->getConfig();
$ext = $info['extension'] ?? '';
$media_params = $ext ? $config->get('media.types.' . strtolower($ext)) : null;
@@ -514,7 +513,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
// Find out what type we're dealing with
[$basename, $extension, $type, $extra] = $this->getFileParts($info['filename']);
$info['file'] = $info['filepath']; // TODO: use filename
$info['file'] = $info['filepath'] ?? (($info['dirname'] ? $info['dirname'] . '/' : '') . $info['filename']); // TODO: use filename
$filename = "{$basename}.{$extension}";
if ($type === 'alternative') {
$media[$filename][$type][$extra] = $info;
@@ -659,7 +658,7 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac
*/
protected function readImageSize(array $info): array
{
$path = $info['filepath'];
$path = $info['filepath'] ?? (($info['dirname'] ? $info['dirname'] . '/' : '') . $info['filename']);
return getimagesize($path);
}