From 9fe7f9e6667f67453c95e43f644fbcb8e9fbcc4f Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Thu, 31 Mar 2022 22:02:49 +0300 Subject: [PATCH] Fixed index updating on copyUploadedFile(), deleteFile() and renameFile() --- .../Common/Media/Traits/MediaUploadTrait.php | 18 +++++++++--------- .../Grav/Common/Page/Medium/AbstractMedia.php | 18 ++++++++++++------ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php b/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php index 3aad1186f..d3155f551 100644 --- a/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php +++ b/system/src/Grav/Common/Media/Traits/MediaUploadTrait.php @@ -312,9 +312,9 @@ trait MediaUploadTrait // TODO: This overrides existing media sizes if used with multiple retina image sizes. $this->doAddUploadedMedium($name, $filename, $path); - // Force media index update. - if (method_exists($this, 'saveIndex')) { - $this->saveIndex($this->index); + // Update media index. + if (method_exists($this, 'updateIndex')) { + $this->updateIndex(); } } catch (Exception $e) { @@ -364,9 +364,9 @@ trait MediaUploadTrait // Remove file and all all the associated metadata. $this->doRemove($name, $path); - // Force media index update. - if (method_exists($this, 'saveIndex')) { - $this->saveIndex($this->index); + // Update media index. + if (method_exists($this, 'updateIndex')) { + $this->updateIndex(); } // Finally clear media cache. @@ -409,9 +409,9 @@ trait MediaUploadTrait $this->doRename($from, $to, $path); - // Force media index update. - if (method_exists($this, 'saveIndex')) { - $this->saveIndex($this->index); + // Update media index. + if (method_exists($this, 'updateIndex')) { + $this->updateIndex(); } // Finally clear media cache. diff --git a/system/src/Grav/Common/Page/Medium/AbstractMedia.php b/system/src/Grav/Common/Page/Medium/AbstractMedia.php index e66471900..d6111e299 100644 --- a/system/src/Grav/Common/Page/Medium/AbstractMedia.php +++ b/system/src/Grav/Common/Page/Medium/AbstractMedia.php @@ -365,10 +365,10 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac } /** - * @param array $files + * @param array|null $files * @return void */ - public function updateIndex(array $files): void + public function updateIndex(array $files = null): void { $mediaIndex = $this->getIndex(); if (!$mediaIndex) { @@ -380,11 +380,17 @@ abstract class AbstractMedia implements ExportInterface, MediaCollectionInterfac $id = $this->getId(); $index = $mediaIndex->get($id, true); - // Add new files and remove the old ones. - $files += $index['files'] ?? []; - $files = array_filter($files, static function($val) { return $val !== null; } ); + if ($files === null) { + $files = $index['files'] ?? []; + $timestamp = 0; + } else { + // Add new files and remove the old ones. + $files += $index['files'] ?? []; + $files = array_filter($files, static function($val) { return $val !== null; } ); + $timestamp = time(); + } - $index = $this->generateIndex($files); + $index = $this->generateIndex($files, null, $timestamp); $mediaIndex->save($id, $index); }