ObjectHomeMountProvider.php 3.8 KB

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