Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 85 additions & 41 deletions apps/files_external/lib/Config/ConfigAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCA\Files_External\Service\UserStoragesService;
use OCP\Files\Config\IAuthoritativeMountProvider;
use OCP\Files\Config\IMountProvider;
use OCP\Files\Config\IPartialMountProvider;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\Storage\IConstructableStorage;
Expand All @@ -26,14 +27,15 @@
use OCP\Files\StorageNotAvailableException;
use OCP\IUser;
use OCP\Server;
use Override;
use Psr\Clock\ClockInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerInterface;

/**
* Make the old files_external config work with the new public mount config api
*/
class ConfigAdapter implements IMountProvider, IAuthoritativeMountProvider {
class ConfigAdapter implements IMountProvider, IAuthoritativeMountProvider, IPartialMountProvider {
public function __construct(
private UserStoragesService $userStoragesService,
private UserGlobalStoragesService $userGlobalStoragesService,
Expand Down Expand Up @@ -81,8 +83,6 @@ public function constructStorageForUser(IUser $user, StorageConfig $storage) {

/**
* Construct the storage implementation
*
* @param StorageConfig $storageConfig
*/
private function constructStorage(StorageConfig $storageConfig): IStorage {
$class = $storageConfig->getBackend()->getStorageClass();
Expand All @@ -99,17 +99,12 @@ private function constructStorage(StorageConfig $storageConfig): IStorage {
}

/**
* Get all mountpoints applicable for the user
*
* @return IMountPoint[]
* @param list<StorageConfig> $storageConfigs
* @return array
* @throws ContainerExceptionInterface
*/
public function getMountsForUser(IUser $user, IStorageFactory $loader) {
$this->userStoragesService->setUser($user);
$this->userGlobalStoragesService->setUser($user);

$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();

$storages = array_map(function (StorageConfig $storageConfig) use ($user) {
private function getAvailableStorages(array $storageConfigs, IUser $user): array {
$storages = array_map(function (StorageConfig $storageConfig) use ($user): IStorage {
try {
return $this->constructStorageForUser($user, $storageConfig);
} catch (\Exception $e) {
Expand All @@ -123,7 +118,7 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
return $storage->getId();
}, $storages));

$availableStorages = array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage {
return array_map(function (IStorage $storage, StorageConfig $storageConfig): IStorage {
try {
$availability = $storage->getAvailability();
if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
Expand All @@ -137,40 +132,89 @@ public function getMountsForUser(IUser $user, IStorageFactory $loader) {
}
return $storage;
}, $storages, $storageConfigs);
}

/**
* Get all mountpoints applicable for the user
*
* @return IMountPoint[]
*/
public function getMountsForUser(IUser $user, IStorageFactory $loader): array {
$this->userStoragesService->setUser($user);
$this->userGlobalStoragesService->setUser($user);

$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
$availableStorages = $this->getAvailableStorages($storageConfigs, $user);

$mounts = array_map(function (StorageConfig $storageConfig, IStorage $storage) use ($user, $loader) {
$storage->setOwner($user->getUID());
if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
return new PersonalMount(
$this->userStoragesService,
$storageConfig,
$storageConfig->getId(),
new KnownMtime([
'storage' => $storage,
'clock' => $this->clock,
]),
'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
null,
$loader,
$storageConfig->getMountOptions(),
$storageConfig->getId(),
);
} else {
return new SystemMountPoint(
$storageConfig,
$storage,
'/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
null,
$loader,
$storageConfig->getMountOptions(),
$storageConfig->getId(),
);
}
$mountpoint = '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint();
return $this->storageConfigToMount($user, $mountpoint, $loader, $storage, $storageConfig);
}, $storageConfigs, $availableStorages);

$this->userStoragesService->resetUser();
$this->userGlobalStoragesService->resetUser();

return $mounts;
}

#[Override]
public function getMountsForPath(string $setupPathHint, bool $forChildren, array $mountProviderArgs, IStorageFactory $loader): array {
$user = $mountProviderArgs[0]->mountInfo->getUser();

if (!$forChildren) {
// override path with mount point when fetching without children
$setupPathHint = $mountProviderArgs[0]->mountInfo->getMountPoint();
}

$this->userStoragesService->setUser($user);
$this->userGlobalStoragesService->setUser($user);

$storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUserWithPath($setupPathHint, $forChildren);
$availableStorages = $this->getAvailableStorages($storageConfigs, $user);

$mounts = [];

$i = 0;
foreach ($storageConfigs as $storageConfig) {
$storage = $availableStorages[$i];
$i++;
$mountPoint = '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint();
$mounts[$mountPoint] = $this->storageConfigToMount($user, $mountPoint, $loader, $storage, $storageConfig);
}

$this->userStoragesService->resetUser();
$this->userGlobalStoragesService->resetUser();

return $mounts;
}

private function storageConfigToMount(IUser $user, string $mountPoint, IStorageFactory $loader, IStorage $storage, StorageConfig $storageConfig): IMountPoint {
$storage->setOwner($user->getUID());
if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
return new PersonalMount(
$this->userStoragesService,
$storageConfig,
$storageConfig->getId(),
new KnownMtime([
'storage' => $storage,
'clock' => $this->clock,
]),
$mountPoint,
null,
$loader,
$storageConfig->getMountOptions(),
$storageConfig->getId()
);
} else {
return new SystemMountPoint(
$storageConfig,
$storage,
$mountPoint,
null,
$loader,
$storageConfig->getMountOptions(),
$storageConfig->getId()
);
}
}
}
14 changes: 5 additions & 9 deletions apps/files_external/lib/Lib/StorageConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ class StorageConfig implements \JsonSerializable {

/**
* Priority
*
* @var int
*/
private $priority;
private int $priority = 100;

/**
* List of users who have access to this storage
Expand Down Expand Up @@ -242,7 +240,7 @@ public function setBackendOption($key, $value) {
*
* @return int priority
*/
public function getPriority() {
public function getPriority(): int {
return $this->priority;
}

Expand All @@ -251,7 +249,7 @@ public function getPriority() {
*
* @param int $priority priority
*/
public function setPriority($priority) {
public function setPriority(int $priority): void {
$this->priority = $priority;
}

Expand All @@ -260,7 +258,7 @@ public function setPriority($priority) {
*
* @return list<string> applicable users
*/
public function getApplicableUsers() {
public function getApplicableUsers(): array {
return $this->applicableUsers;
}

Expand Down Expand Up @@ -399,9 +397,7 @@ public function jsonSerialize(bool $obfuscate = false): array {
$result['backend'] = $this->backend->getIdentifier();
$result['authMechanism'] = $this->authMechanism->getIdentifier();
$result['backendOptions'] = $this->backendOptions;
if (!is_null($this->priority)) {
$result['priority'] = $this->priority;
}
$result['priority'] = $this->priority;
if (!empty($this->applicableUsers)) {
$result['applicableUsers'] = $this->applicableUsers;
}
Expand Down
Loading
Loading