ConfigAdapter.php 5.9 KB

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