]> source.dussan.org Git - nextcloud-server.git/commitdiff
Handle previousNode size === 0 on version creation
authorLouis Chemineau <louis@chmn.me>
Wed, 21 Jun 2023 07:51:36 +0000 (09:51 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Wed, 12 Jul 2023 17:55:30 +0000 (19:55 +0200)
Signed-off-by: Louis Chemineau <louis@chmn.me>
apps/files_versions/lib/Listener/FileEventsListener.php

index 082c88295cb4369fddf07068cb9a2ec7dfbd3fe2..69fb0f1fa54fede42b8725bc1b842c54e46f0907 100644 (file)
@@ -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()]);