Skip to content

Commit

Permalink
fix: duplicate entry for key 'gf_versions_uniq_index'
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Jan 10, 2024
1 parent 4993587 commit 97dcb00
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions lib/Versions/VersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use OCA\Files_Versions\Versions\IVersionBackend;
use OCA\GroupFolders\Mount\GroupMountPoint;
use OCA\GroupFolders\Mount\MountProvider;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Constants;
use OCP\Files\File;
Expand Down Expand Up @@ -80,21 +79,13 @@ public function getVersionsForFile(IUser $user, FileInfo $file): array {
}

// Insert or update the entry in the DB for the current version.
try {
$versionEntity = $this->groupVersionsMapper->findVersionForFileId($file->getId(), $file->getMtime());
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setDecodedMetadata([]);
$this->groupVersionsMapper->update($versionEntity);
} catch (DoesNotExistException) {
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($file->getId());
$versionEntity->setTimestamp($file->getMTime());
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setDecodedMetadata([]);
$this->groupVersionsMapper->insert($versionEntity);
}
$versionEntity = new GroupVersionEntity();
$versionEntity->setFileId($file->getId());
$versionEntity->setTimestamp($file->getMTime());
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setDecodedMetadata([]);
$this->groupVersionsMapper->insert($versionEntity);

// Insert entries in the DB for existing versions.
$versionsOnFS = $versionsFolder->getDirectoryListing();
Expand Down Expand Up @@ -140,23 +131,36 @@ private function getVersionsForFileFromDB(FileInfo $fileInfo, IUser $user, int $
$folder = $this->appFolder->get((string)$folderId);
$file = $folder->get($fileInfo->getInternalPath());

return array_map(
fn (GroupVersionEntity $versionEntity) => new GroupVersion(
$versionEntity->getTimestamp(),
$versionEntity->getTimestamp(),
$file->getName(),
$versionEntity->getSize(),
$this->mimeTypeLoader->getMimetypeById($versionEntity->getMimetype()),
$mountPoint->getInternalPath($file->getPath()),
$file,
$this,
$user,
$versionEntity->getLabel(),
$file->getMtime() === $versionEntity->getTimestamp() ? $file : $versionsFolder->get((string)$versionEntity->getTimestamp()),
$folderId,
),
$this->groupVersionsMapper->findAllVersionsForFileId($file->getId())
);
$versionEntities = $this->groupVersionsMapper->findAllVersionsForFileId($fileInfo->getId());
$versions = [];

foreach ($versionEntities as $versionEntity) {
try {
/** @var GroupVersionEntity $versionEntity */
$versions[] = new GroupVersion(
$versionEntity->getTimestamp(),
$versionEntity->getTimestamp(),
$file->getName(),
$versionEntity->getSize(),
$this->mimeTypeLoader->getMimetypeById($versionEntity->getMimetype()),
$mountPoint->getInternalPath($file->getPath()),
$file,
$this,
$user,
$versionEntity->getLabel(),
$file->getMtime() === $versionEntity->getTimestamp() ? $file : $versionsFolder->get((string)$versionEntity->getTimestamp()),
$folderId,
);
} catch (NotFoundException $e) {
continue;
}
}

if (!count($versions) && count($versionEntities)) {
throw new NotFoundException();
}

return $versions;
}

/**
Expand Down

0 comments on commit 97dcb00

Please sign in to comment.