Skip to content

Commit f76bfc0

Browse files
committed
using IMountPointFileInfo
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 9b7f072 commit f76bfc0

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

lib/Db/FileRequest.php

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,53 @@
99

1010
namespace OCA\GlobalSiteSelector\Db;
1111

12+
use Exception;
1213
use OCA\GlobalSiteSelector\Model\FederatedShare;
1314
use OCA\GlobalSiteSelector\Model\LocalFile;
1415
use OCA\GlobalSiteSelector\Model\LocalMount;
1516
use OCP\DB\QueryBuilder\IQueryBuilder;
1617
use OCP\Federation\ICloudIdManager;
18+
use OCP\Files\Config\ICachedMountFileInfo;
19+
use OCP\Files\Config\IUserMountCache;
20+
use OCP\Files\IRootFolder;
1721
use OCP\IDBConnection;
22+
use Psr\Log\LoggerInterface;
1823

1924
class FileRequest {
2025
public function __construct(
2126
private readonly IDBConnection $connection,
27+
private readonly IUserMountCache $userMountCache,
28+
private readonly IRootFolder $rootFolder,
2229
private readonly ICloudIdManager $cloudIdManager,
30+
private readonly LoggerInterface $logger,
2331
) {
2432
}
2533

2634
/**
2735
* return details from a local file id
2836
*/
2937
public function getFileDetails(int $fileId): ?LocalFile {
30-
$qb = $this->connection->getQueryBuilder();
31-
$qb->select('parent', 'name', 'storage')
32-
->from('filecache')
33-
->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
38+
$cachedMount = $this->getCachedMountInfoFromNodeId($fileId);
39+
if ($cachedMount === null) {
40+
return null;
41+
}
3442

35-
$result = $qb->executeQuery();
36-
$row = $result->fetch();
37-
if ($row === false) {
43+
try {
44+
$rootFolder = $this->rootFolder->getUserFolder($cachedMount->getUser()->getUID());
45+
} catch (Exception $e) {
46+
$this->logger->warning('could not get root folder for user ' . $cachedMount->getUser()->getUID(), ['exception' => $e, 'fileId' => $fileId, 'userId' => $cachedMount->getUser()->getUID()]);
3847
return null;
3948
}
49+
$node = $rootFolder->getFirstNodeById($fileId);
50+
if ($node === null) {
51+
return null;
52+
}
53+
4054
$details = new LocalFile();
4155
$details->setId($fileId)
42-
->setName($row['name'] ?? '')
43-
->setStorageId($row['storage'] ?? -1)
44-
->setParent($row['parent'] ?? -1);
45-
$result->closeCursor();
56+
->setName($node->getName())
57+
->setStorageId($cachedMount->getStorageId())
58+
->setParent($node->getParentId());
4659

4760
return $details;
4861
}
@@ -51,28 +64,15 @@ public function getFileDetails(int $fileId): ?LocalFile {
5164
* return details about the mount point from a LocalFile
5265
*/
5366
public function getMountFromTarget(LocalFile $target): ?LocalMount {
54-
$qb = $this->connection->getQueryBuilder();
55-
$qb->select('mount_provider_class', 'mount_point', 'user_id')
56-
->from('mounts')
57-
->where(
58-
$qb->expr()->andX(
59-
$qb->expr()->eq('storage_id', $qb->createNamedParameter($target->getStorageId(), IQueryBuilder::PARAM_INT)),
60-
$qb->expr()->eq('root_id', $qb->createNamedParameter($target->getId(), IQueryBuilder::PARAM_INT)),
61-
)
62-
);
63-
64-
$result = $qb->executeQuery();
65-
$row = $result->fetch();
66-
if ($row === false) {
67+
$cachedMount = $this->getCachedMountInfoFromNodeId($target->getId());
68+
if ($cachedMount === null) {
6769
return null;
6870
}
6971

7072
$mount = new LocalMount();
71-
$mount->setProviderClass($row['mount_provider_class'])
72-
->setMountPoint(rtrim(explode('/files', $row['mount_point'], 2)[1] ?? '', '/'))
73-
->setUserId($row['user_id']);
74-
75-
$result->closeCursor();
73+
$mount->setProviderClass($cachedMount->getMountProvider())
74+
->setMountPoint(rtrim(explode('/files', $cachedMount->getMountPoint(), 2)[1] ?? '', '/'))
75+
->setUserId($cachedMount->getUser()->getUID());
7676

7777
return $mount;
7878
}
@@ -184,4 +184,13 @@ public function getTeamStorages(FederatedShare $federatedShare, string $instance
184184

185185
return $storage;
186186
}
187+
188+
private function getCachedMountInfoFromNodeId(int $nodeId): ?ICachedMountFileInfo {
189+
$mounts = $this->userMountCache->getMountsForFileId($nodeId);
190+
if (empty($mounts ?? [])) {
191+
$this->logger->warning('mount not found for node id ' . $nodeId);
192+
}
193+
194+
return reset($mounts);
195+
}
187196
}

0 commit comments

Comments
 (0)