ObjectHomeMountProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Mount;
  8. use OCP\Files\Config\IHomeMountProvider;
  9. use OCP\Files\Storage\IStorageFactory;
  10. use OCP\IConfig;
  11. use OCP\IUser;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Mount provider for object store home storages
  15. */
  16. class ObjectHomeMountProvider implements IHomeMountProvider {
  17. /**
  18. * @var IConfig
  19. */
  20. private $config;
  21. /**
  22. * ObjectStoreHomeMountProvider constructor.
  23. *
  24. * @param IConfig $config
  25. */
  26. public function __construct(IConfig $config) {
  27. $this->config = $config;
  28. }
  29. /**
  30. * Get the cache mount for a user
  31. *
  32. * @param IUser $user
  33. * @param IStorageFactory $loader
  34. * @return \OCP\Files\Mount\IMountPoint
  35. */
  36. public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
  37. $config = $this->getMultiBucketObjectStoreConfig($user);
  38. if ($config === null) {
  39. $config = $this->getSingleBucketObjectStoreConfig($user);
  40. }
  41. if ($config === null) {
  42. return null;
  43. }
  44. return new HomeMountPoint($user, '\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader, null, null, self::class);
  45. }
  46. /**
  47. * @param IUser $user
  48. * @return array|null
  49. */
  50. private function getSingleBucketObjectStoreConfig(IUser $user) {
  51. $config = $this->config->getSystemValue('objectstore');
  52. if (!is_array($config)) {
  53. return null;
  54. }
  55. // sanity checks
  56. if (empty($config['class'])) {
  57. \OC::$server->get(LoggerInterface::class)->error('No class given for objectstore', ['app' => 'files']);
  58. }
  59. if (!isset($config['arguments'])) {
  60. $config['arguments'] = [];
  61. }
  62. // instantiate object store implementation
  63. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  64. $config['arguments']['user'] = $user;
  65. return $config;
  66. }
  67. /**
  68. * @param IUser $user
  69. * @return array|null
  70. */
  71. private function getMultiBucketObjectStoreConfig(IUser $user) {
  72. $config = $this->config->getSystemValue('objectstore_multibucket');
  73. if (!is_array($config)) {
  74. return null;
  75. }
  76. // sanity checks
  77. if (empty($config['class'])) {
  78. \OC::$server->get(LoggerInterface::class)->error('No class given for objectstore', ['app' => 'files']);
  79. }
  80. if (!isset($config['arguments'])) {
  81. $config['arguments'] = [];
  82. }
  83. $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
  84. if ($bucket === null) {
  85. /*
  86. * Use any provided bucket argument as prefix
  87. * and add the mapping from username => bucket
  88. */
  89. if (!isset($config['arguments']['bucket'])) {
  90. $config['arguments']['bucket'] = '';
  91. }
  92. $mapper = new \OC\Files\ObjectStore\Mapper($user, $this->config);
  93. $numBuckets = $config['arguments']['num_buckets'] ?? 64;
  94. $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
  95. $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
  96. } else {
  97. $config['arguments']['bucket'] = $bucket;
  98. }
  99. // instantiate object store implementation
  100. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  101. $config['arguments']['user'] = $user;
  102. return $config;
  103. }
  104. }