From 695565fb83de83e3e972f313ff02ad01c398da9c Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Wed, 21 Jun 2023 09:51:36 +0200 Subject: [PATCH] Handle previousNode size === 0 on version creation Signed-off-by: Louis Chemineau --- .../lib/Listener/FileEventsListener.php | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/apps/files_versions/lib/Listener/FileEventsListener.php b/apps/files_versions/lib/Listener/FileEventsListener.php index 082c88295cb..69fb0f1fa54 100644 --- a/apps/files_versions/lib/Listener/FileEventsListener.php +++ b/apps/files_versions/lib/Listener/FileEventsListener.php @@ -226,31 +226,21 @@ class FileEventsListener implements IEventListener { return; } - if ($writeHookInfo['versionCreated'] && $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()) { + if ( + ($writeHookInfo['versionCreated'] && $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()) || + $writeHookInfo['previousNode']->getSize() === 0 + ) { // If a new version was created, insert a version in the DB for the current content. // Unless both versions have the same mtime. - $versionEntity = new VersionEntity(); - $versionEntity->setFileId($node->getId()); - $versionEntity->setTimestamp($node->getMTime()); - $versionEntity->setSize($node->getSize()); - $versionEntity->setMimetype($this->mimeTypeLoader->getId($node->getMimetype())); - $versionEntity->setMetadata([]); - $this->versionsMapper->insert($versionEntity); + $this->created($node); } else { // If no new version was stored in the FS, no new version should be added in the DB. // So we simply update the associated version. - try { - $currentVersionEntity = $this->versionsMapper->findVersionForFileId($node->getId(), $writeHookInfo['previousNode']->getMtime()); - $currentVersionEntity->setTimestamp($node->getMTime()); - $currentVersionEntity->setSize($node->getSize()); - $currentVersionEntity->setMimetype($this->mimeTypeLoader->getId($node->getMimetype())); - $this->versionsMapper->update($currentVersionEntity); - } catch (DoesNotExistException) { - // There might be cases where the current version entry doesn't exist, - // e.g. if none was written due to an empty file or from before the versions table was introduced - // We just create the initial version entry then for the current entity - $this->created($node); - } + $currentVersionEntity = $this->versionsMapper->findVersionForFileId($node->getId(), $writeHookInfo['previousNode']->getMtime()); + $currentVersionEntity->setTimestamp($node->getMTime()); + $currentVersionEntity->setSize($node->getSize()); + $currentVersionEntity->setMimetype($this->mimeTypeLoader->getId($node->getMimetype())); + $this->versionsMapper->update($currentVersionEntity); } unset($this->writeHookInfo[$node->getId()]); -- 2.39.5