diff --git a/lib/Versions/GroupVersionsExpireManager.php b/lib/Versions/GroupVersionsExpireManager.php index 12b8f5595..05d452eaa 100644 --- a/lib/Versions/GroupVersionsExpireManager.php +++ b/lib/Versions/GroupVersionsExpireManager.php @@ -23,13 +23,14 @@ namespace OCA\GroupFolders\Versions; -use OC\Files\FileInfo; use OC\Files\View; use OC\Hooks\BasicEmitter; use OC\User\User; use OCA\GroupFolders\Folder\FolderManager; use OCP\AppFramework\Utility\ITimeFactory; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\Files\FileInfo; +use OCP\IUser; class GroupVersionsExpireManager extends BasicEmitter { private $folderManager; @@ -66,9 +67,17 @@ public function expireAll(): void { public function expireFolder(array $folder): void { $view = new View('/__groupfolders/versions/' . $folder['id']); $files = $this->versionsBackend->getAllVersionedFiles($folder); + /** @var IUser */ $dummyUser = new User('', null, $this->dispatcher); foreach ($files as $fileId => $file) { if ($file instanceof FileInfo) { + // Some versions could have been lost during move operations across storage. + // When this is the case, the fileinfo's path will not contains the name. + // When this is the case, we unlink the version's folder for the fileid, and continue to the next file. + if (!str_ends_with($file->getPath(), $file->getName())) { + $view->unlink('/' . $fileId); + continue; + } $versions = $this->versionsBackend->getVersionsForFile($dummyUser, $file); $expireVersions = $this->expireManager->getExpiredVersion($versions, $this->timeFactory->getTime(), false); foreach ($expireVersions as $version) { diff --git a/lib/Versions/VersionsBackend.php b/lib/Versions/VersionsBackend.php index 7da0b6990..670151dad 100644 --- a/lib/Versions/VersionsBackend.php +++ b/lib/Versions/VersionsBackend.php @@ -67,8 +67,15 @@ public function getVersionsForFile(IUser $user, FileInfo $fileInfo): array { try { $folderId = $mount->getFolderId(); - /** @var Folder $versionsFolder */ - $versionsFolder = $this->getVersionsFolder($mount->getFolderId())->get((string)$fileInfo->getId()); + + try { + $groupfoldersVersionsFolder = $this->getVersionsFolder($mount->getFolderId()); + /** @var Folder $versionsFolder */ + $versionsFolder = $groupfoldersVersionsFolder->get((string)$fileInfo->getId()); + } catch (NotFoundException $e) { + // The folder for the file's versions might not exists if no versions has been create yet. + $versionsFolder = $groupfoldersVersionsFolder->newFolder((string)$fileInfo->getId()); + } $versions = $this->getVersionsForFileFromDB($fileInfo, $user, $folderId); @@ -131,23 +138,42 @@ 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()); + $mappedVersions = array_map( + function (GroupVersionEntity $versionEntity) use ($versionsFolder, $mountPoint, $file, $fileInfo, $user, $folderId) { + if ($fileInfo->getMtime() === $versionEntity->getTimestamp()) { + $versionFile = $file; + } else { + try { + $versionFile = $versionsFolder->get((string)$versionEntity->getTimestamp()); + } catch (NotFoundException $e) { + // The version does not exists on disk anymore, so we can delete its entity in the DB. + // The reality is that the disk version might have been lost during a move operation between storages, + // and its not possible to recover it, so removing the entity makes sense. + $this->groupVersionsMapper->delete($versionEntity); + return null; + } + } + + return new GroupVersion( + $versionEntity->getTimestamp(), + $versionEntity->getTimestamp(), + $fileInfo->getName(), + $versionEntity->getSize(), + $this->mimeTypeLoader->getMimetypeById($versionEntity->getMimetype()), + $mountPoint->getInternalPath($fileInfo->getPath()), + $fileInfo, + $this, + $user, + $versionEntity->getLabel(), + $versionFile, + $folderId, + ); + }, + $versionEntities, ); + // Filter out null values. + return array_filter($mappedVersions); } /**