summaryrefslogtreecommitdiffstats
path: root/apps/files_versions
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2023-06-23 10:40:32 +0200
committerGitHub <noreply@github.com>2023-06-23 10:40:32 +0200
commit05f360c5ec76bf87f55bdbc0385aa7972ec11cbd (patch)
tree3f13e5235830b50f3e6ac199369aa169d53c52e0 /apps/files_versions
parent82bb7a8a2acc0bf44f4f8b02a7a26d5a3a66361a (diff)
parent7fec07196de645aefacc0cb7df94ecac1d518c67 (diff)
downloadnextcloud-server-05f360c5ec76bf87f55bdbc0385aa7972ec11cbd.tar.gz
nextcloud-server-05f360c5ec76bf87f55bdbc0385aa7972ec11cbd.zip
Merge pull request #38800 from nextcloud/bugfix/noid/version-update-or-insert
Diffstat (limited to 'apps/files_versions')
-rw-r--r--apps/files_versions/lib/Listener/FileEventsListener.php50
1 files changed, 34 insertions, 16 deletions
diff --git a/apps/files_versions/lib/Listener/FileEventsListener.php b/apps/files_versions/lib/Listener/FileEventsListener.php
index 76590733e26..fe638364b9d 100644
--- a/apps/files_versions/lib/Listener/FileEventsListener.php
+++ b/apps/files_versions/lib/Listener/FileEventsListener.php
@@ -40,6 +40,7 @@ use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Db\VersionsMapper;
use OCA\Files_Versions\Storage;
use OCP\AppFramework\Db\DoesNotExistException;
+use OCP\DB\Exception;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
@@ -57,6 +58,7 @@ use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
+use Psr\Log\LoggerInterface;
class FileEventsListener implements IEventListener {
private IRootFolder $rootFolder;
@@ -74,15 +76,18 @@ class FileEventsListener implements IEventListener {
*/
private array $versionsDeleted = [];
private IMimeTypeLoader $mimeTypeLoader;
+ private LoggerInterface $logger;
public function __construct(
IRootFolder $rootFolder,
VersionsMapper $versionsMapper,
- IMimeTypeLoader $mimeTypeLoader
+ IMimeTypeLoader $mimeTypeLoader,
+ LoggerInterface $logger,
) {
$this->rootFolder = $rootFolder;
$this->versionsMapper = $versionsMapper;
$this->mimeTypeLoader = $mimeTypeLoader;
+ $this->logger = $logger;
}
public function handle(Event $event): void {
@@ -226,24 +231,37 @@ class FileEventsListener implements IEventListener {
return;
}
- if ($writeHookInfo['versionCreated'] && $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()) {
+ if (
+ ($writeHookInfo['versionCreated'] || $writeHookInfo['previousNode']->getSize() === 0) &&
+ $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()
+ ) {
// 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.
- $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);
+ try {
+ // 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.
+ $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 (Exception $e) {
+ $this->logger->error('Failed to update existing version for ' . $node->getPath(), [
+ 'exception' => $e,
+ 'versionCreated' => $writeHookInfo['versionCreated'],
+ 'previousNode' => [
+ 'size' => $writeHookInfo['previousNode']->getSize(),
+ 'mtime' => $writeHookInfo['previousNode']->getMTime(),
+ ],
+ 'node' => [
+ 'size' => $node->getSize(),
+ 'mtime' => $node->getMTime(),
+ ]
+ ]);
+ throw $e;
+ }
}
unset($this->writeHookInfo[$node->getId()]);