You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.9 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Vlastimil Pecinka <pecinka@email.cz>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Mount;
  26. use OCP\Files\Config\IHomeMountProvider;
  27. use OCP\Files\Storage\IStorageFactory;
  28. use OCP\IConfig;
  29. use OCP\ILogger;
  30. use OCP\IUser;
  31. /**
  32. * Mount provider for object store home storages
  33. */
  34. class ObjectHomeMountProvider implements IHomeMountProvider {
  35. /**
  36. * @var IConfig
  37. */
  38. private $config;
  39. /**
  40. * ObjectStoreHomeMountProvider constructor.
  41. *
  42. * @param IConfig $config
  43. */
  44. public function __construct(IConfig $config) {
  45. $this->config = $config;
  46. }
  47. /**
  48. * Get the cache mount for a user
  49. *
  50. * @param IUser $user
  51. * @param IStorageFactory $loader
  52. * @return \OCP\Files\Mount\IMountPoint
  53. */
  54. public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
  55. $config = $this->getMultiBucketObjectStoreConfig($user);
  56. if ($config === null) {
  57. $config = $this->getSingleBucketObjectStoreConfig($user);
  58. }
  59. if ($config === null) {
  60. return null;
  61. }
  62. return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader);
  63. }
  64. /**
  65. * @param IUser $user
  66. * @return array|null
  67. */
  68. private function getSingleBucketObjectStoreConfig(IUser $user) {
  69. $config = $this->config->getSystemValue('objectstore');
  70. if (!is_array($config)) {
  71. return null;
  72. }
  73. // sanity checks
  74. if (empty($config['class'])) {
  75. \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR);
  76. }
  77. if (!isset($config['arguments'])) {
  78. $config['arguments'] = [];
  79. }
  80. // instantiate object store implementation
  81. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  82. $config['arguments']['user'] = $user;
  83. return $config;
  84. }
  85. /**
  86. * @param IUser $user
  87. * @return array|null
  88. */
  89. private function getMultiBucketObjectStoreConfig(IUser $user) {
  90. $config = $this->config->getSystemValue('objectstore_multibucket');
  91. if (!is_array($config)) {
  92. return null;
  93. }
  94. // sanity checks
  95. if (empty($config['class'])) {
  96. \OCP\Util::writeLog('files', 'No class given for objectstore', ILogger::ERROR);
  97. }
  98. if (!isset($config['arguments'])) {
  99. $config['arguments'] = [];
  100. }
  101. $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
  102. if ($bucket === null) {
  103. /*
  104. * Use any provided bucket argument as prefix
  105. * and add the mapping from username => bucket
  106. */
  107. if (!isset($config['arguments']['bucket'])) {
  108. $config['arguments']['bucket'] = '';
  109. }
  110. $mapper = new \OC\Files\ObjectStore\Mapper($user);
  111. $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
  112. $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
  113. $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
  114. } else {
  115. $config['arguments']['bucket'] = $bucket;
  116. }
  117. // instantiate object store implementation
  118. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  119. $config['arguments']['user'] = $user;
  120. return $config;
  121. }
  122. }