Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken version preview in sidebar due to empty file #39791

Open
Tracked by #4663
juliushaertl opened this issue Aug 10, 2023 · 4 comments
Open
Tracked by #4663

Broken version preview in sidebar due to empty file #39791

juliushaertl opened this issue Aug 10, 2023 · 4 comments
Labels
1. to develop Accepted and waiting to be taken care of 28-feedback bug feature: file sidebar Related to the file sidebar component

Comments

@juliushaertl
Copy link
Member

When starting with an empty file there seems to be a scenario when there is a broken file version listed in the sidebar:

Screenshot 2023-08-10 at 12 23 16

#!/bin/bash
filename=$(date | md5sum | cut -d " " -f 1)
NC_URL=https://admin:admin@nextcloud.local/remote.php/webdav/

echo $filename
echo "" > 1/$filename.md
echo a2 > 2/$filename.md
echo a3 > 3/$filename.md

curl -T "1/$filename.md" "$NC_URL/$filename.md"
curl -T "2/$filename.md" "$NC_URL/$filename.md" -H 'X-OC-Mtime: 12345648'
curl -T "3/$filename.md" "$NC_URL/$filename.md"
@juliushaertl juliushaertl added bug 0. Needs triage Pending check for reproducibility or if it fits our roadmap labels Aug 10, 2023
@juliushaertl
Copy link
Member Author

Actually database content seems reasonable:

mysql> select * from oc_files_versions where file_id = 11970;
+------+---------+------------+------+----------+----------+
| id   | file_id | timestamp  | size | mimetype | metadata |
+------+---------+------------+------+----------+----------+
| 2945 |   11970 |   12345648 |    3 |       23 | []       |
| 2944 |   11970 | 1691662975 |    1 |       23 | []       |
| 2946 |   11970 | 1691662981 |    3 |       23 | []       |
+------+---------+------------+------+----------+----------+
3 rows in set (0.01 sec)

mysql> select path, mtime from oc_filecache where name like 'bbaf07%';
+----------------------------------------------------------------+------------+
| path                                                           | mtime      |
+----------------------------------------------------------------+------------+
| files/bbaf073b41fd7d439b1ea65da43f0b83.md                      | 1691662981 |
| files_versions/bbaf073b41fd7d439b1ea65da43f0b83.md.v1691662975 | 1691662975 |
| files_versions/bbaf073b41fd7d439b1ea65da43f0b83.md.v12345648   |   12345648 |
+----------------------------------------------------------------+------------+
3 rows in set (0.00 sec)

@juliushaertl
Copy link
Member Author

Ok, seems to be just the preview as the file also downloads fine, 1 byte due to the newline

@juliushaertl juliushaertl self-assigned this Aug 10, 2023
@juliushaertl juliushaertl transferred this issue from nextcloud/viewer Aug 10, 2023
@juliushaertl juliushaertl changed the title Broken version in sidebar due to empty file Broken version preview in sidebar due to empty file Aug 10, 2023
@joshtrichards joshtrichards added the feature: file sidebar Related to the file sidebar component label Aug 23, 2023
@juliushaertl
Copy link
Member Author

Root cause fix is in #39786 but there is no cleanup yet of existing broken version references

@juliushaertl
Copy link
Member Author

Finding orphaned db entries is quite tricky as we'd need to iterate over all entries to then

  • find the root file path
  • try to get the filecache entry for the path 'files_versions/$filepath.v$timestamp

One option could be to clean up on the fly when getting the versions, but a onetime cleanup would be preferred imo.

Non-tested patch for deleting orphaned db entry versions on the fly if they have no corresponding version file on the fs:

diff --git a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php
index a9ea09247c2..f41e17f0adc 100644
--- a/apps/files_versions/lib/Versions/LegacyVersionsBackend.php
+++ b/apps/files_versions/lib/Versions/LegacyVersionsBackend.php
@@ -99,6 +99,8 @@ class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend,

                $versions = $this->getVersionsForFileFromDB($file, $user);

+
+
                if (count($versions) > 0) {
                        return $versions;
                }
@@ -138,6 +140,14 @@ class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend,
        private function getVersionsForFileFromDB(FileInfo $file, IUser $user): array {
                $userFolder = $this->rootFolder->getUserFolder($user->getUID());

+               $versions = $this->versionsMapper->findAllVersionsForFileId($file->getId());
+               $relativePath = $userFolder->getRelativePath($file->getPath());
+               $versionsFS = array_map(fn($version) => $version['version'], Storage::getVersions($user->getUID(), $relativePath));
+               $orphanedVersions = array_filter($versions, fn($version) => !in_array($version->getTimestamp(), $versionsFS));
+               foreach ($orphanedVersions as $version) {
+                       $this->versionsMapper->delete($version);
+               }
+
                return array_map(
                        fn (VersionEntity $versionEntity) => new Version(
                                $versionEntity->getTimestamp(),
@@ -151,7 +161,7 @@ class LegacyVersionsBackend implements IVersionBackend, INameableVersionBackend,
                                $user,
                                $versionEntity->getLabel(),
                        ),
-                       $this->versionsMapper->findAllVersionsForFileId($file->getId())
+                       $versions
                );
        }

@juliushaertl juliushaertl removed their assignment Sep 11, 2023
@juliushaertl juliushaertl added 1. to develop Accepted and waiting to be taken care of and removed 0. Needs triage Pending check for reproducibility or if it fits our roadmap labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1. to develop Accepted and waiting to be taken care of 28-feedback bug feature: file sidebar Related to the file sidebar component
Projects
None yet
Development

No branches or pull requests

3 participants