RootMountProvider.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Files\Mount;
  8. use OC;
  9. use OC\Files\ObjectStore\ObjectStoreStorage;
  10. use OC\Files\Storage\LocalRootStorage;
  11. use OC_App;
  12. use OCP\Files\Config\IRootMountProvider;
  13. use OCP\Files\Storage\IStorageFactory;
  14. use OCP\IConfig;
  15. use Psr\Log\LoggerInterface;
  16. class RootMountProvider implements IRootMountProvider {
  17. private IConfig $config;
  18. private LoggerInterface $logger;
  19. public function __construct(IConfig $config, LoggerInterface $logger) {
  20. $this->config = $config;
  21. $this->logger = $logger;
  22. }
  23. public function getRootMounts(IStorageFactory $loader): array {
  24. $objectStore = $this->config->getSystemValue('objectstore', null);
  25. $objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
  26. if ($objectStoreMultiBucket) {
  27. return [$this->getMultiBucketStoreRootMount($loader, $objectStoreMultiBucket)];
  28. } elseif ($objectStore) {
  29. return [$this->getObjectStoreRootMount($loader, $objectStore)];
  30. } else {
  31. return [$this->getLocalRootMount($loader)];
  32. }
  33. }
  34. private function validateObjectStoreConfig(array &$config) {
  35. if (empty($config['class'])) {
  36. $this->logger->error('No class given for objectstore', ['app' => 'files']);
  37. }
  38. if (!isset($config['arguments'])) {
  39. $config['arguments'] = [];
  40. }
  41. // instantiate object store implementation
  42. $name = $config['class'];
  43. if (str_starts_with($name, 'OCA\\') && substr_count($name, '\\') >= 2) {
  44. $segments = explode('\\', $name);
  45. OC_App::loadApp(strtolower($segments[1]));
  46. }
  47. }
  48. private function getLocalRootMount(IStorageFactory $loader): MountPoint {
  49. $configDataDirectory = $this->config->getSystemValue("datadirectory", OC::$SERVERROOT . "/data");
  50. return new MountPoint(LocalRootStorage::class, '/', ['datadir' => $configDataDirectory], $loader, null, null, self::class);
  51. }
  52. private function getObjectStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
  53. $this->validateObjectStoreConfig($config);
  54. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  55. // mount with plain / root object store implementation
  56. $config['class'] = ObjectStoreStorage::class;
  57. return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
  58. }
  59. private function getMultiBucketStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
  60. $this->validateObjectStoreConfig($config);
  61. if (!isset($config['arguments']['bucket'])) {
  62. $config['arguments']['bucket'] = '';
  63. }
  64. // put the root FS always in first bucket for multibucket configuration
  65. $config['arguments']['bucket'] .= '0';
  66. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  67. // mount with plain / root object store implementation
  68. $config['class'] = ObjectStoreStorage::class;
  69. return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
  70. }
  71. }