2016-05-17 19:40:55 +00:00
|
|
|
<?php
|
2024-05-23 07:26:56 +00:00
|
|
|
|
2016-05-17 19:40:55 +00:00
|
|
|
/**
|
2024-05-23 07:26:56 +00:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2016-05-17 19:40:55 +00:00
|
|
|
*/
|
|
|
|
namespace OC\Files\Mount;
|
|
|
|
|
|
|
|
use OCP\Files\Config\IHomeMountProvider;
|
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IUser;
|
2022-03-30 08:55:41 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-05-17 19:40:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the cache mount for a user
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @param IStorageFactory $loader
|
2016-10-27 12:26:13 +00:00
|
|
|
* @return \OCP\Files\Mount\IMountPoint
|
2016-05-17 19:40:55 +00:00
|
|
|
*/
|
|
|
|
public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
|
2016-05-23 18:41:51 +00:00
|
|
|
$config = $this->getMultiBucketObjectStoreConfig($user);
|
2016-05-22 18:44:06 +00:00
|
|
|
if ($config === null) {
|
2016-05-23 18:41:51 +00:00
|
|
|
$config = $this->getSingleBucketObjectStoreConfig($user);
|
2016-05-22 18:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($config === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-10-25 16:04:34 +00:00
|
|
|
return new HomeMountPoint($user, '\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader, null, null, self::class);
|
2016-05-22 18:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array|null
|
|
|
|
*/
|
2016-05-23 18:41:51 +00:00
|
|
|
private function getSingleBucketObjectStoreConfig(IUser $user) {
|
2016-05-17 19:40:55 +00:00
|
|
|
$config = $this->config->getSystemValue('objectstore');
|
|
|
|
if (!is_array($config)) {
|
2016-05-22 18:44:06 +00:00
|
|
|
return null;
|
2016-05-17 19:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sanity checks
|
|
|
|
if (empty($config['class'])) {
|
2022-03-30 08:55:41 +00:00
|
|
|
\OC::$server->get(LoggerInterface::class)->error('No class given for objectstore', ['app' => 'files']);
|
2016-05-17 19:40:55 +00:00
|
|
|
}
|
|
|
|
if (!isset($config['arguments'])) {
|
|
|
|
$config['arguments'] = [];
|
|
|
|
}
|
|
|
|
// instantiate object store implementation
|
|
|
|
$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
|
|
|
|
|
2018-02-07 16:26:42 +00:00
|
|
|
$config['arguments']['user'] = $user;
|
|
|
|
|
2016-05-22 18:44:06 +00:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array|null
|
|
|
|
*/
|
2016-05-23 18:41:51 +00:00
|
|
|
private function getMultiBucketObjectStoreConfig(IUser $user) {
|
2016-05-22 18:44:06 +00:00
|
|
|
$config = $this->config->getSystemValue('objectstore_multibucket');
|
|
|
|
if (!is_array($config)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity checks
|
|
|
|
if (empty($config['class'])) {
|
2022-03-30 08:55:41 +00:00
|
|
|
\OC::$server->get(LoggerInterface::class)->error('No class given for objectstore', ['app' => 'files']);
|
2016-05-22 18:44:06 +00:00
|
|
|
}
|
|
|
|
if (!isset($config['arguments'])) {
|
|
|
|
$config['arguments'] = [];
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:03:25 +00:00
|
|
|
$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'] = '';
|
|
|
|
}
|
2021-11-10 11:21:01 +00:00
|
|
|
$mapper = new \OC\Files\ObjectStore\Mapper($user, $this->config);
|
2023-07-07 10:13:21 +00:00
|
|
|
$numBuckets = $config['arguments']['num_buckets'] ?? 64;
|
2016-11-02 13:42:36 +00:00
|
|
|
$config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
|
2016-05-23 19:03:25 +00:00
|
|
|
|
|
|
|
$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
|
|
|
|
} else {
|
|
|
|
$config['arguments']['bucket'] = $bucket;
|
2016-05-22 18:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// instantiate object store implementation
|
|
|
|
$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
|
|
|
|
|
2019-02-06 10:36:08 +00:00
|
|
|
$config['arguments']['user'] = $user;
|
|
|
|
|
2016-05-22 18:44:06 +00:00
|
|
|
return $config;
|
2016-05-17 19:40:55 +00:00
|
|
|
}
|
|
|
|
}
|