From 8482fb1ceafff505329634d0f115f32328f33b0b Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Thu, 25 May 2023 17:32:35 +0200 Subject: [PATCH] Fix initialisation of versions in the DB Broken after https://github.com/nextcloud/server/pull/36690 Signed-off-by: Louis Chemineau --- apps/files_versions/lib/Storage.php | 4 +++ .../lib/Versions/LegacyVersionsBackend.php | 31 ++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/apps/files_versions/lib/Storage.php b/apps/files_versions/lib/Storage.php index 927788b08788b..f0a82a5f98cab 100644 --- a/apps/files_versions/lib/Storage.php +++ b/apps/files_versions/lib/Storage.php @@ -588,6 +588,10 @@ public static function expireOlderThanMaxForUser($uid) { // Check that the version does not have a label. $path = $versionsRoot->getRelativePath($info->getPath()); + if ($path === null) { + throw new DoesNotExistException('Could not find relative path of (' . $info->getPath() . ')'); + } + $node = $userFolder->get(substr($path, 0, -strlen('.v'.$version))); try { $versionEntity = $versionsMapper->findVersionForFileId($node->getId(), $version); diff --git a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php index 0ad0eb8a439e9..a9ea09247c209 100644 --- a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php +++ b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php @@ -66,17 +66,35 @@ public function useBackendForStorage(IStorage $storage): bool { public function getVersionsForFile(IUser $user, FileInfo $file): array { $storage = $file->getStorage(); + if ($storage->instanceOfStorage(SharedStorage::class)) { $owner = $storage->getOwner(''); $user = $this->userManager->get($owner); + $fileId = $file->getId(); + if ($fileId === null) { + throw new NotFoundException("File not found ($fileId)"); + } + + if ($user === null) { + throw new NotFoundException("User $owner not found for $fileId"); + } + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); - $nodes = $userFolder->getById($file->getId()); + + $nodes = $userFolder->getById($fileId); $file = array_pop($nodes); if (!$file) { throw new NotFoundException("version file not found for share owner"); } + } else { + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); + } + + $fileId = $file->getId(); + if ($fileId === null) { + throw new NotFoundException("File not found ($fileId)"); } $versions = $this->getVersionsForFileFromDB($file, $user); @@ -87,7 +105,7 @@ public function getVersionsForFile(IUser $user, FileInfo $file): array { // Insert the entry in the DB for the current version. $versionEntity = new VersionEntity(); - $versionEntity->setFileId($file->getId()); + $versionEntity->setFileId($fileId); $versionEntity->setTimestamp($file->getMTime()); $versionEntity->setSize($file->getSize()); $versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype())); @@ -95,10 +113,15 @@ public function getVersionsForFile(IUser $user, FileInfo $file): array { $this->versionsMapper->insert($versionEntity); // Insert entries in the DB for existing versions. - $versionsOnFS = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file->getPath())); + $relativePath = $userFolder->getRelativePath($file->getPath()); + if ($relativePath === null) { + throw new NotFoundException("Relative path not found for file $fileId (" . $file->getPath() . ')'); + } + + $versionsOnFS = Storage::getVersions($user->getUID(), $relativePath); foreach ($versionsOnFS as $version) { $versionEntity = new VersionEntity(); - $versionEntity->setFileId($file->getId()); + $versionEntity->setFileId($fileId); $versionEntity->setTimestamp((int)$version['version']); $versionEntity->setSize((int)$version['size']); $versionEntity->setMimetype($this->mimeTypeLoader->getId($version['mimetype']));