ConfigAdapter.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 OC\Files\Storage\Wrapper\KnownMtime;
  34. use OCA\Files_External\Lib\PersonalMount;
  35. use OCA\Files_External\Lib\StorageConfig;
  36. use OCA\Files_External\Service\UserGlobalStoragesService;
  37. use OCA\Files_External\Service\UserStoragesService;
  38. use OCP\Files\Config\IMountProvider;
  39. use OCP\Files\Storage;
  40. use OCP\Files\Storage\IStorageFactory;
  41. use OCP\Files\StorageNotAvailableException;
  42. use OCP\IUser;
  43. use Psr\Clock\ClockInterface;
  44. /**
  45. * Make the old files_external config work with the new public mount config api
  46. */
  47. class ConfigAdapter implements IMountProvider {
  48. public function __construct(
  49. private UserStoragesService $userStoragesService,
  50. private UserGlobalStoragesService $userGlobalStoragesService,
  51. private ClockInterface $clock,
  52. ) {
  53. }
  54. /**
  55. * Process storage ready for mounting
  56. *
  57. * @param StorageConfig $storage
  58. * @param IUser $user
  59. * @throws \OCP\AppFramework\QueryException
  60. */
  61. private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
  62. foreach ($storage->getBackendOptions() as $option => $value) {
  63. $storage->setBackendOption($option, \OCA\Files_External\MountConfig::substitutePlaceholdersInConfig($value, $user->getUID()));
  64. }
  65. $objectStore = $storage->getBackendOption('objectstore');
  66. if ($objectStore) {
  67. $objectClass = $objectStore['class'];
  68. if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
  69. throw new \InvalidArgumentException('Invalid object store');
  70. }
  71. $storage->setBackendOption('objectstore', new $objectClass($objectStore));
  72. }
  73. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
  74. $storage->getBackend()->manipulateStorageConfig($storage, $user);
  75. }
  76. /**
  77. * Construct the storage implementation
  78. *
  79. * @param StorageConfig $storageConfig
  80. * @return Storage
  81. */
  82. private function constructStorage(StorageConfig $storageConfig) {
  83. $class = $storageConfig->getBackend()->getStorageClass();
  84. $storage = new $class($storageConfig->getBackendOptions());
  85. // auth mechanism should fire first
  86. $storage = $storageConfig->getBackend()->wrapStorage($storage);
  87. $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
  88. return $storage;
  89. }
  90. /**
  91. * Get all mountpoints applicable for the user
  92. *
  93. * @param \OCP\IUser $user
  94. * @param \OCP\Files\Storage\IStorageFactory $loader
  95. * @return \OCP\Files\Mount\IMountPoint[]
  96. */
  97. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  98. $this->userStoragesService->setUser($user);
  99. $this->userGlobalStoragesService->setUser($user);
  100. $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
  101. $storages = array_map(function (StorageConfig $storageConfig) use ($user) {
  102. try {
  103. $this->prepareStorageConfig($storageConfig, $user);
  104. return $this->constructStorage($storageConfig);
  105. } catch (\Exception $e) {
  106. // propagate exception into filesystem
  107. return new FailedStorage(['exception' => $e]);
  108. }
  109. }, $storageConfigs);
  110. \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function (Storage\IStorage $storage) {
  111. return $storage->getId();
  112. }, $storages));
  113. $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) {
  114. try {
  115. $availability = $storage->getAvailability();
  116. if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
  117. $storage = new FailedStorage([
  118. 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
  119. ]);
  120. }
  121. } catch (\Exception $e) {
  122. // propagate exception into filesystem
  123. $storage = new FailedStorage(['exception' => $e]);
  124. }
  125. return $storage;
  126. }, $storages, $storageConfigs);
  127. $mounts = array_map(function (StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) {
  128. $storage->setOwner($user->getUID());
  129. if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
  130. return new PersonalMount(
  131. $this->userStoragesService,
  132. $storageConfig,
  133. $storageConfig->getId(),
  134. new KnownMtime([
  135. 'storage' => $storage,
  136. 'clock' => $this->clock,
  137. ]),
  138. '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
  139. null,
  140. $loader,
  141. $storageConfig->getMountOptions(),
  142. $storageConfig->getId()
  143. );
  144. } else {
  145. return new SystemMountPoint(
  146. $storageConfig,
  147. $storage,
  148. '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
  149. null,
  150. $loader,
  151. $storageConfig->getMountOptions(),
  152. $storageConfig->getId()
  153. );
  154. }
  155. }, $storageConfigs, $availableStorages);
  156. $this->userStoragesService->resetUser();
  157. $this->userGlobalStoragesService->resetUser();
  158. return $mounts;
  159. }
  160. }