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
8 changes: 0 additions & 8 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1975,14 +1975,6 @@
<code><![CDATA[wrap]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/Files/Mount/ObjectHomeMountProvider.php">
<InvalidNullableReturnType>
<code><![CDATA[\OCP\Files\Mount\IMountPoint]]></code>
</InvalidNullableReturnType>
<NullableReturnStatement>
<code><![CDATA[null]]></code>
</NullableReturnStatement>
</file>
<file src="lib/private/Files/Node/File.php">
<InvalidReturnStatement>
<code><![CDATA[$this->view->hash($type, $this->path, $raw)]]></code>
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,7 @@
'OC\\Files\\ObjectStore\\Mapper' => $baseDir . '/lib/private/Files/ObjectStore/Mapper.php',
'OC\\Files\\ObjectStore\\ObjectStoreScanner' => $baseDir . '/lib/private/Files/ObjectStore/ObjectStoreScanner.php',
'OC\\Files\\ObjectStore\\ObjectStoreStorage' => $baseDir . '/lib/private/Files/ObjectStore/ObjectStoreStorage.php',
'OC\\Files\\ObjectStore\\PrimaryObjectStoreConfig' => $baseDir . '/lib/private/Files/ObjectStore/PrimaryObjectStoreConfig.php',
'OC\\Files\\ObjectStore\\S3' => $baseDir . '/lib/private/Files/ObjectStore/S3.php',
'OC\\Files\\ObjectStore\\S3ConfigTrait' => $baseDir . '/lib/private/Files/ObjectStore/S3ConfigTrait.php',
'OC\\Files\\ObjectStore\\S3ConnectionTrait' => $baseDir . '/lib/private/Files/ObjectStore/S3ConnectionTrait.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Files\\ObjectStore\\Mapper' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/Mapper.php',
'OC\\Files\\ObjectStore\\ObjectStoreScanner' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/ObjectStoreScanner.php',
'OC\\Files\\ObjectStore\\ObjectStoreStorage' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/ObjectStoreStorage.php',
'OC\\Files\\ObjectStore\\PrimaryObjectStoreConfig' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/PrimaryObjectStoreConfig.php',
'OC\\Files\\ObjectStore\\S3' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/S3.php',
'OC\\Files\\ObjectStore\\S3ConfigTrait' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/S3ConfigTrait.php',
'OC\\Files\\ObjectStore\\S3ConnectionTrait' => __DIR__ . '/../../..' . '/lib/private/Files/ObjectStore/S3ConnectionTrait.php',
Expand Down
110 changes: 16 additions & 94 deletions lib/private/Files/Mount/ObjectHomeMountProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,117 +7,39 @@
*/
namespace OC\Files\Mount;

use OC\Files\ObjectStore\HomeObjectStoreStorage;
use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
use OCP\Files\Config\IHomeMountProvider;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IUser;
use Psr\Log\LoggerInterface;

/**
* Mount provider for object store home storages
*/
class ObjectHomeMountProvider implements IHomeMountProvider {
/**
* @var IConfig
*/
private $config;

/**
* ObjectStoreHomeMountProvider constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
public function __construct(
private PrimaryObjectStoreConfig $objectStoreConfig,
) {
}

/**
* Get the cache mount for a user
* Get the home mount for a user
*
* @param IUser $user
* @param IStorageFactory $loader
* @return \OCP\Files\Mount\IMountPoint
* @return ?IMountPoint
*/
public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
$config = $this->getMultiBucketObjectStoreConfig($user);
if ($config === null) {
$config = $this->getSingleBucketObjectStoreConfig($user);
}

if ($config === null) {
public function getHomeMountForUser(IUser $user, IStorageFactory $loader): ?IMountPoint {
$objectStoreConfig = $this->objectStoreConfig->getObjectStoreConfigForUser($user);
if ($objectStoreConfig === null) {
return null;
}
$arguments = array_merge($objectStoreConfig['arguments'], [
'objectstore' => $this->objectStoreConfig->buildObjectStore($objectStoreConfig),
'user' => $user,
]);

return new HomeMountPoint($user, '\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader, null, null, self::class);
}

/**
* @param IUser $user
* @return array|null
*/
private function getSingleBucketObjectStoreConfig(IUser $user) {
$config = $this->config->getSystemValue('objectstore');
if (!is_array($config)) {
return null;
}

// sanity checks
if (empty($config['class'])) {
\OC::$server->get(LoggerInterface::class)->error('No class given for objectstore', ['app' => 'files']);
}
if (!isset($config['arguments'])) {
$config['arguments'] = [];
}
// instantiate object store implementation
$config['arguments']['objectstore'] = new $config['class']($config['arguments']);

$config['arguments']['user'] = $user;

return $config;
}

/**
* @param IUser $user
* @return array|null
*/
private function getMultiBucketObjectStoreConfig(IUser $user) {
$config = $this->config->getSystemValue('objectstore_multibucket');
if (!is_array($config)) {
return null;
}

// sanity checks
if (empty($config['class'])) {
\OC::$server->get(LoggerInterface::class)->error('No class given for objectstore', ['app' => 'files']);
}
if (!isset($config['arguments'])) {
$config['arguments'] = [];
}

$bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);

if ($bucket === null) {
/*
* Use any provided bucket argument as prefix
* and add the mapping from username => bucket
*/
if (!isset($config['arguments']['bucket'])) {
$config['arguments']['bucket'] = '';
}
$mapper = new \OC\Files\ObjectStore\Mapper($user, $this->config);
$numBuckets = $config['arguments']['num_buckets'] ?? 64;
$config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);

$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
} else {
$config['arguments']['bucket'] = $bucket;
}

// instantiate object store implementation
$config['arguments']['objectstore'] = new $config['class']($config['arguments']);

$config['arguments']['user'] = $user;

return $config;
return new HomeMountPoint($user, HomeObjectStoreStorage::class, '/' . $user->getUID(), $arguments, $loader, null, null, self::class);
}
}
62 changes: 12 additions & 50 deletions lib/private/Files/Mount/RootMountProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,41 @@

use OC;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
use OC\Files\Storage\LocalRootStorage;
use OC_App;
use OCP\Files\Config\IRootMountProvider;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use Psr\Log\LoggerInterface;

class RootMountProvider implements IRootMountProvider {
private PrimaryObjectStoreConfig $objectStoreConfig;
private IConfig $config;
private LoggerInterface $logger;

public function __construct(IConfig $config, LoggerInterface $logger) {
public function __construct(PrimaryObjectStoreConfig $objectStoreConfig, IConfig $config) {
$this->objectStoreConfig = $objectStoreConfig;
$this->config = $config;
$this->logger = $logger;
}

public function getRootMounts(IStorageFactory $loader): array {
$objectStore = $this->config->getSystemValue('objectstore', null);
$objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
$objectStoreConfig = $this->objectStoreConfig->getObjectStoreConfigForRoot();

if ($objectStoreMultiBucket) {
return [$this->getMultiBucketStoreRootMount($loader, $objectStoreMultiBucket)];
} elseif ($objectStore) {
return [$this->getObjectStoreRootMount($loader, $objectStore)];
if ($objectStoreConfig) {
return [$this->getObjectStoreRootMount($loader, $objectStoreConfig)];
} else {
return [$this->getLocalRootMount($loader)];
}
}

private function validateObjectStoreConfig(array &$config) {
if (empty($config['class'])) {
$this->logger->error('No class given for objectstore', ['app' => 'files']);
}
if (!isset($config['arguments'])) {
$config['arguments'] = [];
}

// instantiate object store implementation
$name = $config['class'];
if (str_starts_with($name, 'OCA\\') && substr_count($name, '\\') >= 2) {
$segments = explode('\\', $name);
OC_App::loadApp(strtolower($segments[1]));
}
}

private function getLocalRootMount(IStorageFactory $loader): MountPoint {
$configDataDirectory = $this->config->getSystemValue('datadirectory', OC::$SERVERROOT . '/data');
return new MountPoint(LocalRootStorage::class, '/', ['datadir' => $configDataDirectory], $loader, null, null, self::class);
}

private function getObjectStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
$this->validateObjectStoreConfig($config);

$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
// mount with plain / root object store implementation
$config['class'] = ObjectStoreStorage::class;

return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
}

private function getMultiBucketStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
$this->validateObjectStoreConfig($config);

if (!isset($config['arguments']['bucket'])) {
$config['arguments']['bucket'] = '';
}
// put the root FS always in first bucket for multibucket configuration
$config['arguments']['bucket'] .= '0';

$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
// mount with plain / root object store implementation
$config['class'] = ObjectStoreStorage::class;
private function getObjectStoreRootMount(IStorageFactory $loader, array $objectStoreConfig): MountPoint {
$arguments = array_merge($objectStoreConfig['arguments'], [
'objectstore' => $this->objectStoreConfig->buildObjectStore($objectStoreConfig),
]);

return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
return new MountPoint(ObjectStoreStorage::class, '/', $arguments, $loader, null, null, self::class);
}
}
140 changes: 140 additions & 0 deletions lib/private/Files/ObjectStore/PrimaryObjectStoreConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OC\Files\ObjectStore;

use OCP\App\IAppManager;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\IConfig;
use OCP\IUser;

/**
* @psalm-type ObjectStoreConfig array{class: class-string<IObjectStore>, arguments: array{multibucket: bool, ...}}
*/
class PrimaryObjectStoreConfig {
public function __construct(
private readonly IConfig $config,
private readonly IAppManager $appManager,
) {
}

/**
* @param ObjectStoreConfig $config
*/
public function buildObjectStore(array $config): IObjectStore {
return new $config['class']($config['arguments']);
}

/**
* @return ?ObjectStoreConfig
*/
public function getObjectStoreConfigForRoot(): ?array {
$config = $this->getObjectStoreConfig();

if ($config && $config['arguments']['multibucket']) {
if (!isset($config['arguments']['bucket'])) {
$config['arguments']['bucket'] = '';
}

// put the root FS always in first bucket for multibucket configuration
$config['arguments']['bucket'] .= '0';
}
return $config;
}

/**
* @return ?ObjectStoreConfig
*/
public function getObjectStoreConfigForUser(IUser $user): ?array {
$config = $this->getObjectStoreConfig();

if ($config && $config['arguments']['multibucket']) {
$config['arguments']['bucket'] = $this->getBucketForUser($user, $config);
}
return $config;
}

/**
* @return ?ObjectStoreConfig
*/
private function getObjectStoreConfig(): ?array {
$objectStore = $this->config->getSystemValue('objectstore', null);
$objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);

// new-style multibucket config uses the same 'objectstore' key but sets `'multibucket' => true`, transparently upgrade older style config
if ($objectStoreMultiBucket) {
$objectStoreMultiBucket['arguments']['multibucket'] = true;
return $this->validateObjectStoreConfig($objectStoreMultiBucket);
} elseif ($objectStore) {
return $this->validateObjectStoreConfig($objectStore);
} else {
return null;
}
}

/**
* @return ObjectStoreConfig
*/
private function validateObjectStoreConfig(array $config) {
if (!isset($config['class'])) {
throw new \Exception('No class configured for object store');
}
if (!isset($config['arguments'])) {
$config['arguments'] = [];
}
$class = $config['class'];
$arguments = $config['arguments'];
if (!is_array($arguments)) {
throw new \Exception('Configured object store arguments are not an array');
}
if (!isset($arguments['multibucket'])) {
$arguments['multibucket'] = false;
}
if (!is_bool($arguments['multibucket'])) {
throw new \Exception('arguments.multibucket must be a boolean in object store configuration');
}

if (!is_string($class)) {
throw new \Exception('Configured class for object store is not a string');
}

if (str_starts_with($class, 'OCA\\') && substr_count($class, '\\') >= 2) {
[$appId] = explode('\\', $class);
$this->appManager->loadApp(strtolower($appId));
}

if (!is_a($class, IObjectStore::class, true)) {
throw new \Exception('Configured class for object store is not an object store');
}
return [
'class' => $class,
'arguments' => $arguments,
];
}

private function getBucketForUser(IUser $user, array $config): string {
$bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);

if ($bucket === null) {
/*
* Use any provided bucket argument as prefix
* and add the mapping from username => bucket
*/
if (!isset($config['arguments']['bucket'])) {
$config['arguments']['bucket'] = '';
}
$mapper = new Mapper($user, $this->config);
$numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
$bucket = $config['arguments']['bucket'] . $mapper->getBucket($numBuckets);

$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $bucket);
}

return $bucket;
}
}
Loading
Loading