aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2025-04-23 16:03:20 +0200
committerFerdinand Thiessen <opensource@fthiessen.de>2025-04-23 16:03:20 +0200
commit5e03c6fb5824018a1623b025054f1c656cd9d8a9 (patch)
treebbd3cb11c22f9f0046a7b843892bec00910f745f
parent9bfa1e7bd995161db1ab19dab45776b5ff1a7295 (diff)
downloadnextcloud-server-fix/files-version-creation.tar.gz
nextcloud-server-fix/files-version-creation.zip
fix(files_versions): create version if previous does not existfix/files-version-creation
This issue happens reproducible if: - Versions is disabled - Upload a file - Enable versions - Upload same file unchanged - Now the error happens. Problem is that the mtime is unchanged so no version will be created on the upload, but it tries to update the last version which does not exists. Instead of "upload same file unchanged" you can also - like in the example stack trace above - use Android with an SD card with invalid mtime -> the mtime will be stripped so its always the same. Instead of disable versions the same also happens if e.g. the versions creation failed due to other issues. The solution now is to catch the exception and create if not exists. A cleaner solution would be to have a method on the versions backend like `hasVersionEntity(File $file, int $revision): bool` but this would be a breaking change or at least a feature that apps need to implement. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
-rw-r--r--apps/files_versions/lib/Listener/FileEventsListener.php13
1 files changed, 11 insertions, 2 deletions
diff --git a/apps/files_versions/lib/Listener/FileEventsListener.php b/apps/files_versions/lib/Listener/FileEventsListener.php
index 49150f4e726..c581c61b4ae 100644
--- a/apps/files_versions/lib/Listener/FileEventsListener.php
+++ b/apps/files_versions/lib/Listener/FileEventsListener.php
@@ -196,8 +196,8 @@ class FileEventsListener implements IEventListener {
}
if (
- $writeHookInfo['versionCreated'] &&
- $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()
+ $writeHookInfo['versionCreated']
+ && $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()
) {
// If a new version was created, insert a version in the DB for the current content.
// If both versions have the same mtime, it means the latest version file simply got overrode,
@@ -218,6 +218,15 @@ class FileEventsListener implements IEventListener {
],
);
}
+ } catch (DoesNotExistException $e) {
+ // This happens if the versions app was not enabled while the file was created or updated the last time.
+ // meaning there is no such revision and we need to create this file.
+ if ($writeHookInfo['versionCreated']) {
+ $this->created($node);
+ } else {
+ // Normally this should not happen so we re-throw the exception to not hide any potential issues.
+ throw $e;
+ }
} catch (Exception $e) {
$this->logger->error('Failed to update existing version for ' . $node->getPath(), [
'exception' => $e,