diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2023-07-13 13:29:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-13 13:29:22 +0200 |
commit | a06ef707bb34e8e32a0c26db8a5ac7b32fb0a513 (patch) | |
tree | 1820d5a24042f4e498cc0743a18e5b19e21fbeb8 /apps | |
parent | 9b1bb6a61da3546cc39196fa63f6197ba04302dd (diff) | |
parent | 4742901124ad6cf6e98b65cba6e14259c2b5b3cd (diff) | |
download | nextcloud-server-a06ef707bb34e8e32a0c26db8a5ac7b32fb0a513.tar.gz nextcloud-server-a06ef707bb34e8e32a0c26db8a5ac7b32fb0a513.zip |
Merge pull request #39003 from nextcloud/backport/38800/stable26
[stable26] fix: Avoid failing to update the current version entry if there is none
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_versions/lib/Listener/FileEventsListener.php | 50 |
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()]); |