Skip to content

Commit 3ddc49b

Browse files
committed
feat(external-storage): Implement IPartialMountPoint
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent bd90e7c commit 3ddc49b

File tree

3 files changed

+219
-93
lines changed

3 files changed

+219
-93
lines changed

apps/files_external/lib/Config/ConfigAdapter.php

Lines changed: 94 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCA\Files_External\Service\UserStoragesService;
1919
use OCP\AppFramework\QueryException;
2020
use OCP\Files\Config\IMountProvider;
21+
use OCP\Files\Config\IPartialMountProvider;
2122
use OCP\Files\Mount\IMountPoint;
2223
use OCP\Files\ObjectStore\IObjectStore;
2324
use OCP\Files\Storage\IConstructableStorage;
@@ -26,13 +27,15 @@
2627
use OCP\Files\StorageNotAvailableException;
2728
use OCP\IUser;
2829
use OCP\Server;
30+
use Override;
2931
use Psr\Clock\ClockInterface;
32+
use Psr\Container\ContainerExceptionInterface;
3033
use Psr\Log\LoggerInterface;
3134

3235
/**
3336
* Make the old files_external config work with the new public mount config api
3437
*/
35-
class ConfigAdapter implements IMountProvider {
38+
class ConfigAdapter implements IMountProvider, IPartialMountProvider {
3639
public function __construct(
3740
private UserStoragesService $userStoragesService,
3841
private UserGlobalStoragesService $userGlobalStoragesService,
@@ -56,7 +59,7 @@ private function validateObjectStoreClassString(string $class): string {
5659
/**
5760
* Process storage ready for mounting
5861
*
59-
* @throws QueryException
62+
* @throws ContainerExceptionInterface
6063
*/
6164
private function prepareStorageConfig(StorageConfig &$storage, IUser $user): void {
6265
foreach ($storage->getBackendOptions() as $option => $value) {
@@ -75,8 +78,6 @@ private function prepareStorageConfig(StorageConfig &$storage, IUser $user): voi
7578

7679
/**
7780
* Construct the storage implementation
78-
*
79-
* @param StorageConfig $storageConfig
8081
*/
8182
private function constructStorage(StorageConfig $storageConfig): IStorage {
8283
$class = $storageConfig->getBackend()->getStorageClass();
@@ -93,17 +94,12 @@ private function constructStorage(StorageConfig $storageConfig): IStorage {
9394
}
9495

9596
/**
96-
* Get all mountpoints applicable for the user
97-
*
98-
* @return IMountPoint[]
97+
* @param list<StorageConfig> $storageConfigs
98+
* @return array
99+
* @throws ContainerExceptionInterface
99100
*/
100-
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
101-
$this->userStoragesService->setUser($user);
102-
$this->userGlobalStoragesService->setUser($user);
103-
104-
$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
105-
106-
$storages = array_map(function (StorageConfig $storageConfig) use ($user) {
101+
private function getAvailableStorages(array $storageConfigs, IUser $user): array {
102+
$storages = array_map(function (StorageConfig $storageConfig) use ($user): IStorage {
107103
try {
108104
$this->prepareStorageConfig($storageConfig, $user);
109105
return $this->constructStorage($storageConfig);
@@ -118,7 +114,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
118114
return $storage->getId();
119115
}, $storages));
120116

121-
$availableStorages = array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage {
117+
return array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage {
122118
try {
123119
$availability = $storage->getAvailability();
124120
if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
@@ -132,6 +128,19 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
132128
}
133129
return $storage;
134130
}, $storages, $storageConfigs);
131+
}
132+
133+
/**
134+
* Get all mountpoints applicable for the user
135+
*
136+
* @return IMountPoint[]
137+
*/
138+
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
139+
$this->userStoragesService->setUser($user);
140+
$this->userGlobalStoragesService->setUser($user);
141+
142+
$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
143+
$availableStorages = $this->getAvailableStorages($storageConfigs, $user);
135144

136145
$mounts = array_map(function (StorageConfig $storageConfig, IStorage $storage) use ($user, $loader) {
137146
$storage->setOwner($user->getUID());
@@ -168,4 +177,74 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
168177

169178
return $mounts;
170179
}
180+
181+
#[Override]
182+
public function getMountsForPath(string $path, bool $forChildren, array $mountProviderArgs, IStorageFactory $loader): array {
183+
if (empty($mountProviderArgs)) {
184+
return [];
185+
}
186+
187+
$userId = null;
188+
$user = null;
189+
foreach ($mountProviderArgs as $mountProviderArg) {
190+
if ($userId === null) {
191+
$user = $mountProviderArg->mountInfo->getUser();
192+
$userId = $user->getUID();
193+
} elseif ($userId !== $mountProviderArg->mountInfo->getUser()->getUID()) {
194+
throw new \LogicException('Mounts must belong to the same user!');
195+
}
196+
}
197+
198+
if (!$forChildren) {
199+
// override path with mount point when fetching without children
200+
$path = $mountProviderArgs[0]->mountInfo->getMountPoint(); // TODO: not sure what this is doing, is this getting the parent path?
201+
}
202+
203+
$this->userStoragesService->setUser($user);
204+
$this->userGlobalStoragesService->setUser($user);
205+
206+
$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUserWithPath($path, $forChildren);
207+
$availableStorages = $this->getAvailableStorages($storageConfigs, $user);
208+
209+
$mounts = [];
210+
211+
$i = 0;
212+
foreach ($storageConfigs as $storageConfig) {
213+
$storage = $availableStorages[$i];
214+
$i++;
215+
$storage->setOwner($user->getUID());
216+
$mountPoint = '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint();
217+
if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
218+
$mounts[$mountPoint] = new PersonalMount(
219+
$this->userStoragesService,
220+
$storageConfig,
221+
$storageConfig->getId(),
222+
new KnownMtime([
223+
'storage' => $storage,
224+
'clock' => $this->clock,
225+
]),
226+
$mountPoint,
227+
null,
228+
$loader,
229+
$storageConfig->getMountOptions(),
230+
$storageConfig->getId()
231+
);
232+
} else {
233+
$mounts[$mountPoint] = new SystemMountPoint(
234+
$storageConfig,
235+
$storage,
236+
$mountPoint,
237+
null,
238+
$loader,
239+
$storageConfig->getMountOptions(),
240+
$storageConfig->getId()
241+
);
242+
}
243+
}
244+
245+
$this->userStoragesService->resetUser();
246+
$this->userGlobalStoragesService->resetUser();
247+
248+
return $mounts;
249+
}
171250
}

0 commit comments

Comments
 (0)