ObjectHomeMountProvider.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\IUser;
  28. /**
  29. * Mount provider for object store home storages
  30. */
  31. class ObjectHomeMountProvider implements IHomeMountProvider {
  32. /**
  33. * @var IConfig
  34. */
  35. private $config;
  36. /**
  37. * ObjectStoreHomeMountProvider constructor.
  38. *
  39. * @param IConfig $config
  40. */
  41. public function __construct(IConfig $config) {
  42. $this->config = $config;
  43. }
  44. /**
  45. * Get the cache mount for a user
  46. *
  47. * @param IUser $user
  48. * @param IStorageFactory $loader
  49. * @return \OCP\Files\Mount\IMountPoint
  50. */
  51. public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
  52. $config = $this->getMultiBucketObjectStoreConfig($user);
  53. if ($config === null) {
  54. $config = $this->getSingleBucketObjectStoreConfig($user);
  55. }
  56. if ($config === null) {
  57. return null;
  58. }
  59. return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader);
  60. }
  61. /**
  62. * @param IUser $user
  63. * @return array|null
  64. */
  65. private function getSingleBucketObjectStoreConfig(IUser $user) {
  66. $config = $this->config->getSystemValue('objectstore');
  67. if (!is_array($config)) {
  68. return null;
  69. }
  70. // sanity checks
  71. if (empty($config['class'])) {
  72. \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
  73. }
  74. if (!isset($config['arguments'])) {
  75. $config['arguments'] = [];
  76. }
  77. // instantiate object store implementation
  78. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  79. $config['arguments']['user'] = $user;
  80. return $config;
  81. }
  82. /**
  83. * @param IUser $user
  84. * @return array|null
  85. */
  86. private function getMultiBucketObjectStoreConfig(IUser $user) {
  87. $config = $this->config->getSystemValue('objectstore_multibucket');
  88. if (!is_array($config)) {
  89. return null;
  90. }
  91. // sanity checks
  92. if (empty($config['class'])) {
  93. \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
  94. }
  95. if (!isset($config['arguments'])) {
  96. $config['arguments'] = [];
  97. }
  98. $config['arguments']['user'] = $user;
  99. $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
  100. if ($bucket === null) {
  101. /*
  102. * Use any provided bucket argument as prefix
  103. * and add the mapping from username => bucket
  104. */
  105. if (!isset($config['arguments']['bucket'])) {
  106. $config['arguments']['bucket'] = '';
  107. }
  108. $mapper = new \OC\Files\ObjectStore\Mapper($user);
  109. $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
  110. $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
  111. $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
  112. } else {
  113. $config['arguments']['bucket'] = $bucket;
  114. }
  115. // instantiate object store implementation
  116. $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
  117. return $config;
  118. }
  119. }