Fixed index updating on copyUploadedFile(), deleteFile() and renameFile()

This commit is contained in:
Matias Griese
2022-03-31 22:02:49 +03:00
parent 77d675f167
commit 9fe7f9e666
2 changed files with 21 additions and 15 deletions

View File

@@ -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.

View File

@@ -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);
}