Skip to content

Commit 67fcb93

Browse files
committed
feat(external-storage): Implement IPartialMountPoint
Signed-off-by: Carl Schwan <[email protected]>
1 parent bd90e7c commit 67fcb93

File tree

3 files changed

+222
-79
lines changed

3 files changed

+222
-79
lines changed

apps/files_external/lib/Config/ConfigAdapter.php

Lines changed: 97 additions & 1 deletion
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,14 @@
2627
use OCP\Files\StorageNotAvailableException;
2728
use OCP\IUser;
2829
use OCP\Server;
30+
use Override;
2931
use Psr\Clock\ClockInterface;
3032
use Psr\Log\LoggerInterface;
3133

3234
/**
3335
* Make the old files_external config work with the new public mount config api
3436
*/
35-
class ConfigAdapter implements IMountProvider {
37+
class ConfigAdapter implements IMountProvider, IPartialMountProvider {
3638
public function __construct(
3739
private UserStoragesService $userStoragesService,
3840
private UserGlobalStoragesService $userGlobalStoragesService,
@@ -168,4 +170,98 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
168170

169171
return $mounts;
170172
}
173+
174+
#[Override]
175+
public function getMountsForPath(string $path, bool $forChildren, array $mountProviderArgs, IStorageFactory $loader): array {
176+
if (empty($mountProviderArgs)) {
177+
return [];
178+
}
179+
180+
$userId = null;
181+
$user = null;
182+
foreach ($mountProviderArgs as $mountProviderArg) {
183+
if ($userId === null) {
184+
$user = $mountProviderArg->mountInfo->getUser();
185+
$userId = $user->getUID();
186+
} elseif ($userId !== $mountProviderArg->mountInfo->getUser()->getUID()) {
187+
throw new \LogicException('Mounts must belong to the same user!');
188+
}
189+
}
190+
191+
$this->userStoragesService->setUser($user);
192+
$this->userGlobalStoragesService->setUser($user);
193+
194+
$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUserWithPath($path, $forChildren);
195+
196+
$storages = array_map(function (StorageConfig $storageConfig) use ($user) {
197+
try {
198+
$this->prepareStorageConfig($storageConfig, $user);
199+
return $this->constructStorage($storageConfig);
200+
} catch (\Exception $e) {
201+
// propagate exception into filesystem
202+
return new FailedStorage(['exception' => $e]);
203+
}
204+
}, $storageConfigs);
205+
206+
207+
Storage::getGlobalCache()->loadForStorageIds(array_map(function (IStorage $storage) {
208+
return $storage->getId();
209+
}, $storages));
210+
211+
$availableStorages = array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage {
212+
try {
213+
$availability = $storage->getAvailability();
214+
if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
215+
$storage = new FailedStorage([
216+
'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
217+
]);
218+
}
219+
} catch (\Exception $e) {
220+
// propagate exception into filesystem
221+
$storage = new FailedStorage(['exception' => $e]);
222+
}
223+
return $storage;
224+
}, $storages, $storageConfigs);
225+
226+
$mounts = [];
227+
228+
$i = 0;
229+
foreach ($storageConfigs as $storageConfig) {
230+
$storage = $availableStorages[$i];
231+
$i++;
232+
$storage->setOwner($user->getUID());
233+
$mountPoint = '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint();
234+
if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
235+
$mounts[$mountPoint] = new PersonalMount(
236+
$this->userStoragesService,
237+
$storageConfig,
238+
$storageConfig->getId(),
239+
new KnownMtime([
240+
'storage' => $storage,
241+
'clock' => $this->clock,
242+
]),
243+
$mountPoint,
244+
null,
245+
$loader,
246+
$storageConfig->getMountOptions(),
247+
$storageConfig->getId()
248+
);
249+
} else {
250+
$mounts[$mountPoint] = new SystemMountPoint(
251+
$storageConfig,
252+
$storage,
253+
$mountPoint,
254+
null,
255+
$loader,
256+
$storageConfig->getMountOptions(),
257+
$storageConfig->getId()
258+
);
259+
}
260+
}
261+
262+
$this->userStoragesService->resetUser();
263+
$this->userGlobalStoragesService->resetUser();
264+
265+
return $mounts;
266+
}
171267
}

0 commit comments

Comments
 (0)