From da0d04a293df91bf882129fb4f2b53390d3110f3 Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Tue, 24 Sep 2024 13:09:58 +0000 Subject: [PATCH] Merge tag v2.3.28 into develop --- src/Events/DocumentLifeCycleSubscriber.php | 44 ++++++++++++++-------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/Events/DocumentLifeCycleSubscriber.php b/src/Events/DocumentLifeCycleSubscriber.php index db94161..8865ff5 100644 --- a/src/Events/DocumentLifeCycleSubscriber.php +++ b/src/Events/DocumentLifeCycleSubscriber.php @@ -10,7 +10,6 @@ use Doctrine\ORM\Events; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemOperator; -use League\Flysystem\UnableToMoveFile; use League\Flysystem\Visibility; use RZ\Roadiz\Documents\Exceptions\DocumentWithoutFileException; use RZ\Roadiz\Documents\Models\DocumentInterface; @@ -20,9 +19,9 @@ */ #[AsDoctrineListener(event: Events::postRemove)] #[AsDoctrineListener(event: Events::preUpdate)] -final class DocumentLifeCycleSubscriber +final readonly class DocumentLifeCycleSubscriber { - public function __construct(private readonly FilesystemOperator $documentsStorage) + public function __construct(private FilesystemOperator $documentsStorage) { } @@ -43,19 +42,9 @@ public function preUpdate(PreUpdateEventArgs $args): void && is_string($args->getNewValue('filename')) && $args->getOldValue('filename') !== '' ) { - $oldPath = $this->getDocumentMountPathForFilename($document, $args->getOldValue('filename')); - $newPath = $this->getDocumentMountPathForFilename($document, $args->getNewValue('filename')); - - if ($oldPath !== $newPath) { - if ($this->documentsStorage->fileExists($oldPath) && !$this->documentsStorage->fileExists($newPath)) { - /* - * Only perform IO rename if old file exists and new path is free. - */ - $this->documentsStorage->move($oldPath, $newPath); - } else { - throw new UnableToMoveFile('Cannot rename file from ' . $oldPath . ' to ' . $newPath); - } - } + // This method must not throw any exception + // because filename WILL change if document file is updated too. + $this->renameDocumentFilename($document, $args); } if ($args->hasChangedField('private')) { if ($document->isPrivate() === true) { @@ -66,6 +55,29 @@ public function preUpdate(PreUpdateEventArgs $args): void } } + private function renameDocumentFilename(DocumentInterface $document, PreUpdateEventArgs $args): void + { + $oldPath = $this->getDocumentMountPathForFilename($document, $args->getOldValue('filename')); + $newPath = $this->getDocumentMountPathForFilename($document, $args->getNewValue('filename')); + + if ($oldPath === $newPath) { + return; + } + + if (!$this->documentsStorage->fileExists($oldPath)) { + // Do not throw, just return + return; + } + if ($this->documentsStorage->fileExists($newPath)) { + // Do not throw, just return + return; + } + /* + * Only perform IO rename if old file exists and new path is free. + */ + $this->documentsStorage->move($oldPath, $newPath); + } + private function makePublic(DocumentInterface $document): void { $this->validateDocument($document);