ConfigAdapter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_External\Config;
  31. use OC\Files\Storage\FailedStorage;
  32. use OC\Files\Storage\Wrapper\Availability;
  33. use OCA\Files_External\Lib\PersonalMount;
  34. use OCA\Files_External\Lib\StorageConfig;
  35. use OCA\Files_External\Service\UserGlobalStoragesService;
  36. use OCA\Files_External\Service\UserStoragesService;
  37. use OCP\Files\Config\IMountProvider;
  38. use OCP\Files\Storage;
  39. use OCP\Files\Storage\IStorageFactory;
  40. use OCP\Files\StorageNotAvailableException;
  41. use OCP\IUser;
  42. /**
  43. * Make the old files_external config work with the new public mount config api
  44. */
  45. class ConfigAdapter implements IMountProvider {
  46. /** @var UserStoragesService */
  47. private $userStoragesService;
  48. /** @var UserGlobalStoragesService */
  49. private $userGlobalStoragesService;
  50. /**
  51. * @param UserStoragesService $userStoragesService
  52. * @param UserGlobalStoragesService $userGlobalStoragesService
  53. */
  54. public function __construct(
  55. UserStoragesService $userStoragesService,
  56. UserGlobalStoragesService $userGlobalStoragesService
  57. ) {
  58. $this->userStoragesService = $userStoragesService;
  59. $this->userGlobalStoragesService = $userGlobalStoragesService;
  60. }
  61. /**
  62. * Process storage ready for mounting
  63. *
  64. * @param StorageConfig $storage
  65. * @param IUser $user
  66. * @throws \OCP\AppFramework\QueryException
  67. */
  68. private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
  69. foreach ($storage->getBackendOptions() as $option => $value) {
  70. $storage->setBackendOption($option, \OCA\Files_External\MountConfig::substitutePlaceholdersInConfig($value, $user->getUID()));
  71. }
  72. $objectStore = $storage->getBackendOption('objectstore');
  73. if ($objectStore) {
  74. $objectClass = $objectStore['class'];
  75. if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
  76. throw new \InvalidArgumentException('Invalid object store');
  77. }
  78. $storage->setBackendOption('objectstore', new $objectClass($objectStore));
  79. }
  80. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
  81. $storage->getBackend()->manipulateStorageConfig($storage, $user);
  82. }
  83. /**
  84. * Construct the storage implementation
  85. *
  86. * @param StorageConfig $storageConfig
  87. * @return Storage
  88. */
  89. private function constructStorage(StorageConfig $storageConfig) {
  90. $class = $storageConfig->getBackend()->getStorageClass();
  91. $storage = new $class($storageConfig->getBackendOptions());
  92. // auth mechanism should fire first
  93. $storage = $storageConfig->getBackend()->wrapStorage($storage);
  94. $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
  95. return $storage;
  96. }
  97. /**
  98. * Get all mountpoints applicable for the user
  99. *
  100. * @param \OCP\IUser $user
  101. * @param \OCP\Files\Storage\IStorageFactory $loader
  102. * @return \OCP\Files\Mount\IMountPoint[]
  103. */
  104. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  105. $this->userStoragesService->setUser($user);
  106. $this->userGlobalStoragesService->setUser($user);
  107. $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
  108. $storages = array_map(function (StorageConfig $storageConfig) use ($user) {
  109. try {
  110. $this->prepareStorageConfig($storageConfig, $user);
  111. return $this->constructStorage($storageConfig);
  112. } catch (\Exception $e) {
  113. // propagate exception into filesystem
  114. return new FailedStorage(['exception' => $e]);
  115. }
  116. }, $storageConfigs);
  117. \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function (Storage\IStorage $storage) {
  118. return $storage->getId();
  119. }, $storages));
  120. $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) {
  121. try {
  122. $availability = $storage->getAvailability();
  123. if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
  124. $storage = new FailedStorage([
  125. 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
  126. ]);
  127. }
  128. } catch (\Exception $e) {
  129. // propagate exception into filesystem
  130. $storage = new FailedStorage(['exception' => $e]);
  131. }
  132. return $storage;
  133. }, $storages, $storageConfigs);
  134. $mounts = array_map(function (StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) {
  135. if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) {
  136. return new PersonalMount(
  137. $this->userStoragesService,
  138. $storageConfig,
  139. $storageConfig->getId(),
  140. $storage,
  141. '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
  142. null,
  143. $loader,
  144. $storageConfig->getMountOptions(),
  145. $storageConfig->getId()
  146. );
  147. } else {
  148. return new SystemMountPoint(
  149. $storageConfig,
  150. $storage,
  151. '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
  152. null,
  153. $loader,
  154. $storageConfig->getMountOptions(),
  155. $storageConfig->getId()
  156. );
  157. }
  158. }, $storageConfigs, $availableStorages);
  159. $this->userStoragesService->resetUser();
  160. $this->userGlobalStoragesService->resetUser();
  161. return $mounts;
  162. }
  163. }