diff options
author | Louis <louis@chmn.me> | 2024-03-05 11:39:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-05 11:39:00 +0100 |
commit | d81365711e43c2cc571ace51a656742faafde2e1 (patch) | |
tree | 7d82e346d5f4b9caaa45ea4453eb06ebc05482b0 /apps | |
parent | 96b88d3b654147a34c255d2a91e13dadf31fa41a (diff) | |
parent | 2957d8ad9b7562158993460fb40da2aa177e4cca (diff) | |
download | nextcloud-server-d81365711e43c2cc571ace51a656742faafde2e1.tar.gz nextcloud-server-d81365711e43c2cc571ace51a656742faafde2e1.zip |
Merge pull request #43980 from nextcloud/backport/43898/stable27
[stable27] Improve files version listing
Diffstat (limited to 'apps')
-rw-r--r-- | apps/files_versions/lib/Versions/LegacyVersionsBackend.php | 93 |
1 files changed, 50 insertions, 43 deletions
diff --git a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php index 8b2d2caf40e..74199dcccc6 100644 --- a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php +++ b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php @@ -24,6 +24,7 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OCA\Files_Versions\Versions; use OC\Files\View; @@ -103,64 +104,70 @@ class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend, throw new NotFoundException("File not found ($fileId)"); } - $versions = $this->getVersionsForFileFromDB($file, $user); - - // Early exit if we find any version in the database. - // Else we continue to populate the DB from what's on disk. - if (count($versions) > 0) { - return $versions; - } - - // Insert the entry in the DB for the current version. - $versionEntity = new VersionEntity(); - $versionEntity->setFileId($fileId); - $versionEntity->setTimestamp($file->getMTime()); - $versionEntity->setSize($file->getSize()); - $versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype())); - $versionEntity->setMetadata([]); - $this->versionsMapper->insert($versionEntity); - // Insert entries in the DB for existing versions. $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($fileId); - $versionEntity->setTimestamp((int)$version['version']); - $versionEntity->setSize((int)$version['size']); - $versionEntity->setMimetype($this->mimeTypeLoader->getId($version['mimetype'])); - $versionEntity->setMetadata([]); - $this->versionsMapper->insert($versionEntity); + $currentVersion = [ + 'version' => (string)$file->getMtime(), + 'size' => $file->getSize(), + 'mimetype' => $file->getMimetype(), + ]; + + $versionsInDB = $this->versionsMapper->findAllVersionsForFileId($file->getId()); + /** @var array<int, array> */ + $versionsInFS = array_values(Storage::getVersions($user->getUID(), $relativePath)); + + /** @var array<int, array{db: ?VersionEntity, fs: ?mixed}> */ + $groupedVersions = []; + $davVersions = []; + + foreach ($versionsInDB as $version) { + $revisionId = $version->getTimestamp(); + $groupedVersions[$revisionId] = $groupedVersions[$revisionId] ?? []; + $groupedVersions[$revisionId]['db'] = $version; } - return $this->getVersionsForFileFromDB($file, $user); - } + foreach ([$currentVersion, ...$versionsInFS] as $version) { + $revisionId = $version['version']; + $groupedVersions[$revisionId] = $groupedVersions[$revisionId] ?? []; + $groupedVersions[$revisionId]['fs'] = $version; + } - /** - * @return IVersion[] - */ - private function getVersionsForFileFromDB(FileInfo $file, IUser $user): array { - $userFolder = $this->rootFolder->getUserFolder($user->getUID()); + /** @var array<string, array{db: ?VersionEntity, fs: ?mixed}> $groupedVersions */ + foreach ($groupedVersions as $versions) { + if (empty($versions['db']) && !empty($versions['fs'])) { + $versions['db'] = new VersionEntity(); + $versions['db']->setFileId($fileId); + $versions['db']->setTimestamp((int)$versions['fs']['version']); + $versions['db']->setSize((int)$versions['fs']['size']); + $versions['db']->setMimetype($this->mimeTypeLoader->getId($versions['fs']['mimetype'])); + $versions['db']->setMetadata([]); + $this->versionsMapper->insert($versions['db']); + } elseif (!empty($versions['db']) && empty($versions['fs'])) { + $this->versionsMapper->delete($versions['db']); + continue; + } - return array_map( - fn (VersionEntity $versionEntity) => new Version( - $versionEntity->getTimestamp(), - $versionEntity->getTimestamp(), + $version = new Version( + $versions['db']->getTimestamp(), + $versions['db']->getTimestamp(), $file->getName(), - $versionEntity->getSize(), - $this->mimeTypeLoader->getMimetypeById($versionEntity->getMimetype()), + $versions['db']->getSize(), + $this->mimeTypeLoader->getMimetypeById($versions['db']->getMimetype()), $userFolder->getRelativePath($file->getPath()), $file, $this, $user, - $versionEntity->getLabel(), - ), - $this->versionsMapper->findAllVersionsForFileId($file->getId()) - ); + $versions['db']->getLabel(), + ); + + array_push($davVersions, $version); + } + + return $davVersions; } public function createVersion(IUser $user, FileInfo $file) { |