Skip to content

Commit 0051db7

Browse files
authored
Merge pull request #52373 from nextcloud/fix/files-version-creation
fix(files_versions): create version if previous does not exist
2 parents 4114fbf + 5e03c6f commit 0051db7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

apps/files_versions/lib/Listener/FileEventsListener.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ public function post_write_hook(Node $node): void {
196196
}
197197

198198
if (
199-
$writeHookInfo['versionCreated'] &&
200-
$node->getMTime() !== $writeHookInfo['previousNode']->getMTime()
199+
$writeHookInfo['versionCreated']
200+
&& $node->getMTime() !== $writeHookInfo['previousNode']->getMTime()
201201
) {
202202
// If a new version was created, insert a version in the DB for the current content.
203203
// If both versions have the same mtime, it means the latest version file simply got overrode,
@@ -218,6 +218,15 @@ public function post_write_hook(Node $node): void {
218218
],
219219
);
220220
}
221+
} catch (DoesNotExistException $e) {
222+
// This happens if the versions app was not enabled while the file was created or updated the last time.
223+
// meaning there is no such revision and we need to create this file.
224+
if ($writeHookInfo['versionCreated']) {
225+
$this->created($node);
226+
} else {
227+
// Normally this should not happen so we re-throw the exception to not hide any potential issues.
228+
throw $e;
229+
}
221230
} catch (Exception $e) {
222231
$this->logger->error('Failed to update existing version for ' . $node->getPath(), [
223232
'exception' => $e,

0 commit comments

Comments
 (0)