RootMountProvider.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\Files\Mount;
  23. use OC;
  24. use OC\Files\ObjectStore\ObjectStoreStorage;
  25. use OC\Files\Storage\LocalRootStorage;
  26. use OC_App;
  27. use OCP\Files\Config\IRootMountProvider;
  28. use OCP\Files\Storage\IStorageFactory;
  29. use OCP\IConfig;
  30. use Psr\Log\LoggerInterface;
  31. class RootMountProvider implements IRootMountProvider {
  32. private IConfig $config;
  33. private LoggerInterface $logger;
  34. public function __construct(IConfig $config, LoggerInterface $logger) {
  35. $this->config = $config;
  36. $this->logger = $logger;
  37. }
  38. public function getRootMounts(IStorageFactory $loader): array {
  39. $objectStore = $this->config->getSystemValue('objectstore', null);
  40. $objectStoreMultiBucket = $this->config->getSystemValue('objectstore_multibucket', null);
  41. if ($objectStoreMultiBucket) {
  42. return [$this->getMultiBucketStoreRootMount($loader, $objectStoreMultiBucket)];
  43. } elseif ($objectStore) {
  44. return [$this->getObjectStoreRootMount($loader, $objectStore)];
  45. } else {
  46. return [$this->getLocalRootMount($loader)];
  47. }
  48. }
  49. private function validateObjectStoreConfig(array &$config) {
  50. if (empty($config['class'])) {
  51. $this->logger->error('No class given for objectstore', ['app' => 'files']);
  52. }
  53. if (!isset($config['arguments'])) {
  54. $config['arguments'] = [];
  55. }
  56. // instantiate object store implementation
  57. $name = $config['class'];
  58. if (strpos($name, 'OCA\\') === 0 && substr_count($name, '\\') >= 2) {
  59. $segments = explode('\\', $name);
  60. OC_App::loadApp(strtolower($segments[1]));
  61. }
  62. }
  63. private function getLocalRootMount(IStorageFactory $loader): MountPoint {
  64. $configDataDirectory = $this->config->getSystemValue("datadirectory", OC::$SERVERROOT . "/data");
  65. return new MountPoint(LocalRootStorage::class, '/', ['datadir' => $configDataDirectory], $loader, null, null, self::class);
  66. }
  67. private function getObjectStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
  68. $this->validateObjectStoreConfig($config);
  69. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  70. // mount with plain / root object store implementation
  71. $config['class'] = ObjectStoreStorage::class;
  72. return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
  73. }
  74. private function getMultiBucketStoreRootMount(IStorageFactory $loader, array $config): MountPoint {
  75. $this->validateObjectStoreConfig($config);
  76. if (!isset($config['arguments']['bucket'])) {
  77. $config['arguments']['bucket'] = '';
  78. }
  79. // put the root FS always in first bucket for multibucket configuration
  80. $config['arguments']['bucket'] .= '0';
  81. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  82. // mount with plain / root object store implementation
  83. $config['class'] = ObjectStoreStorage::class;
  84. return new MountPoint($config['class'], '/', $config['arguments'], $loader, null, null, self::class);
  85. }
  86. }