Skip to content

Commit f8cd96b

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

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

lib/Command/GlobalScaleDiscovery.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,21 @@ public function __construct(
2525
parent::__construct();
2626
}
2727

28-
29-
/**
30-
*
31-
*/
32-
protected function configure() {
28+
protected function configure(): void {
3329
parent::configure();
3430
$this->setName('globalsiteselector:discovery')
3531
->addOption('current', '', InputOption::VALUE_NONE, 'display current data')
3632
->setDescription('run a discovery request over Global Scale to get details about each instances');
3733
}
3834

39-
/**
40-
* @param InputInterface $input
41-
* @param OutputInterface $output
42-
*
43-
* @return int
44-
*/
4535
protected function execute(InputInterface $input, OutputInterface $output): int {
4636
if ($input->getOption('current')) {
4737
$output->writeln(json_encode($this->appConfig->getValueArray(Application::APP_ID, ConfigLexicon::GS_TOKENS), JSON_PRETTY_PRINT));
48-
return 0;
38+
return self::SUCCESS;
4939
}
5040

5141
// currently, the only available data is a unique token that helps identify each instance
5242
$this->globalScaleService->refreshTokenFromGlobalScale();
53-
return 0;
43+
return self::SUCCESS;
5444
}
5545
}

lib/Db/FileRequest.php

Lines changed: 42 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()]);
47+
return null;
48+
}
49+
$node = $rootFolder->getFirstNodeById($fileId);
50+
if ($node === null) {
3851
return null;
3952
}
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,17 @@ public function getTeamStorages(FederatedShare $federatedShare, string $instance
184184

185185
return $storage;
186186
}
187+
188+
/**
189+
* returns the mount using the id of a node,
190+
* userid can then be extracted and used to retrieve the file's root folder
191+
*/
192+
private function getCachedMountInfoFromNodeId(int $nodeId): ?ICachedMountFileInfo {
193+
$mounts = $this->userMountCache->getMountsForFileId($nodeId);
194+
if (empty($mounts ?? [])) {
195+
$this->logger->warning('mount not found for node id ' . $nodeId);
196+
}
197+
198+
return reset($mounts);
199+
}
187200
}

0 commit comments

Comments
 (0)