From a8a1791fccdaca575323fbeef5d1978f44ed7b0f Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 18 Feb 2022 10:15:35 +0200 Subject: [PATCH] Move media methods around --- system/src/Grav/Common/Page/Media.php | 10 ++- .../Grav/Common/Page/Medium/AbstractMedia.php | 77 ++++++++++++++++++- .../Grav/Common/Page/Medium/LocalMedia.php | 68 ---------------- 3 files changed, 82 insertions(+), 73 deletions(-) diff --git a/system/src/Grav/Common/Page/Media.php b/system/src/Grav/Common/Page/Media.php index f1ebe3617..59ca7da05 100644 --- a/system/src/Grav/Common/Page/Media.php +++ b/system/src/Grav/Common/Page/Media.php @@ -23,14 +23,18 @@ class Media extends LocalMedia /** * @param string|null $path - * @param array|null $media_order + * @param array|null $mediaOrder * @param bool $load */ - public function __construct(?string $path, array $media_order = null, bool $load = true) + public function __construct(?string $path, array $mediaOrder = null, bool $load = true) { $this->setPath($path); + $this->indexFolder = $this->getPath(); $this->indexTimeout = 60; - $this->media_order = $media_order; + $this->media_order = $mediaOrder; + + $path = $this->getPath(); + $this->exists = null !== $path && is_dir($path); if ($load) { $this->init(); diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index 487f643eb..5b3ef6f4e 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -21,6 +21,8 @@ use Grav\Common\Media\Traits\MediaUploadTrait; use Grav\Common\Page\Pages; use Grav\Common\Utils; use Grav\Framework\Compat\Serializable; +use Grav\Framework\File\Formatter\JsonFormatter; +use Grav\Framework\File\JsonFile; use InvalidArgumentException; use PHPExif\Reader\Reader; use RocketTheme\Toolbox\ArrayTraits\Export; @@ -51,6 +53,8 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac protected $path; /** @var string|null */ protected $url; + /** @var bool */ + protected $exists = false; /** @var array|null */ protected $index; /** @var array|null */ @@ -63,6 +67,10 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac protected $config = []; /** @var array */ protected $standard_exif = ['FileSize', 'MimeType', 'height', 'width']; + /** @var string|null */ + protected $indexFolder; + /** @var string|null */ + protected $indexFile = 'media.json'; /** @var int */ protected $indexTimeout = 0; /** @var string|int|null */ @@ -87,7 +95,10 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac /** * @return bool */ - abstract public function exists(): bool; + public function exists(): bool + { + return $this->exists; + } /** * @return int @@ -380,8 +391,11 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac 'grouped' => $this->grouped, 'path' => $this->path, 'url' => $this->url, + 'exists' => $this->exists, 'media_order' => $this->media_order, 'standard_exif' => $this->standard_exif, + 'indexFolder' => $this->indexFolder, + 'indexFile' => $this->indexFile, 'indexTimeout' => $this->indexTimeout ]; } @@ -401,8 +415,11 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac $this->grouped = $data['grouped']; $this->path = $data['path']; $this->url = $data['url']; + $this->exists = $data['exists']; $this->media_order = $data['media_order']; $this->standard_exif = $data['standard_exif']; + $this->indexFolder = $data['indexFolder']; + $this->indexFile = $data['indexFile']; $this->indexTimeout = $data['indexTimeout']; // Initialize items. @@ -763,12 +780,53 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac return $medium; } + /** + * Get index file, which stores media file index. + * + * @return JsonFile|null + */ + protected function getIndexFile(): ?JsonFile + { + if (null === $this->indexFolder || null === $this->indexFile) { + return null; + } + + return new JsonFile($this->indexFolder . '/' . $this->indexFile, new JsonFormatter(['encode_options' => JSON_PRETTY_PRINT])); + } + /** * @return array */ protected function loadIndex(): array { - return [[], 0]; + // Read media index file. + $indexFile = $this->getIndexFile(); + if (!($indexFile && $indexFile->exists())) { + return [[], 0, 0]; + } + + $index = $indexFile->load(); + $version = $index['version'] ?? null; + $folder = $index['folder'] ?? null; + $type = $index['type'] ?? null; + if ($version !== static::VERSION || $folder !== $this->path || $type !== ($this->config['type'] ?? 'local')) { + return [[], 0, 0]; + } + + return [$index['files'] ?? [], $index['timestamp'] ?? 0, $indexFile->getModificationTime()]; + } + + /** + * @return void + */ + protected function touchIndex(): void + { + $index = $this->getIndexFile(); + if (!$index || !$this->exists) { + return; + } + + $index->touch(); } /** @@ -778,6 +836,21 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac */ protected function saveIndex(array $files, ?int $timestamp = null): void { + $index = $this->getIndexFile(); + if (!$index || !$this->exists) { + return; + } + + $data = [ + 'type' => $this->config['type'] ?? 'local', + 'version' => static::VERSION, + 'timestamp' => $timestamp ?? time(), + 'folder' => $this->path, + 'url' => $this->url, + 'files' => $files, + ]; + + $index->save($data); } /** diff --git a/system/src/Grav/Common/Page/Medium/LocalMedia.php b/system/src/Grav/Common/Page/Medium/LocalMedia.php index 67811e943..8443d1e46 100644 --- a/system/src/Grav/Common/Page/Medium/LocalMedia.php +++ b/system/src/Grav/Common/Page/Medium/LocalMedia.php @@ -55,16 +55,6 @@ abstract class LocalMedia extends AbstractMedia $this->path = $path; } - /** - * @return bool - */ - public function exists(): bool - { - $path = $this->getPath(); - - return null !== $path && is_dir($path); - } - /** * Create Medium from a file. * @@ -223,62 +213,4 @@ abstract class LocalMedia extends AbstractMedia return $media; } - - /** - * Get index file, which stores media file index. - * - * @return JsonFile|null - */ - protected function getIndexFile(): ?JsonFile - { - $indexFolder = $this->getPath(); - $indexFile = 'media.json'; - if (null === $indexFolder || null === $indexFile) { - return null; - } - - return new JsonFile($indexFolder . '/' . $indexFile, new JsonFormatter(['encode_options' => JSON_PRETTY_PRINT])); - } - - /** - * @return array - */ - protected function loadIndex(): array - { - // Read media index file. - $indexFile = $this->getIndexFile(); - - $index = $indexFile && $indexFile->exists() ? $indexFile->load() : []; - $version = $index['version'] ?? null; - $type = $index['type'] ?? null; - $folder = $index['folder'] ?? null; - if ($version !== static::VERSION || $folder !== $this->path || $type !== ($this->config['type'] ?? 'local')) { - $index = []; - } - - return [$index['files'] ?? [], $index['timestamp'] ?? 0]; - } - - /** - * @param array $files - * @param int|null $timestamp - * @return void - */ - protected function saveIndex(array $files, ?int $timestamp = null): void - { - $index = $this->getIndexFile(); - if (!$index || !$this->exists()) { - return; - } - - $index->save( - [ - 'timestamp' => $timestamp ?? time(), - 'type' => $this->config['type'] ?? 'local', - 'version' => static::VERSION, - 'folder' => $this->path, - 'files' => $files, - ] - ); - } }