You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigAdapter.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Config;
  8. use OC\Files\Storage\FailedStorage;
  9. use OC\Files\Storage\Wrapper\Availability;
  10. use OC\Files\Storage\Wrapper\KnownMtime;
  11. use OCA\Files_External\Lib\PersonalMount;
  12. use OCA\Files_External\Lib\StorageConfig;
  13. use OCA\Files_External\Service\UserGlobalStoragesService;
  14. use OCA\Files_External\Service\UserStoragesService;
  15. use OCP\Files\Config\IMountProvider;
  16. use OCP\Files\Storage;
  17. use OCP\Files\Storage\IStorageFactory;
  18. use OCP\Files\StorageNotAvailableException;
  19. use OCP\IUser;
  20. use Psr\Clock\ClockInterface;
  21. /**
  22. * Make the old files_external config work with the new public mount config api
  23. */
  24. class ConfigAdapter implements IMountProvider {
  25. public function __construct(
  26. private UserStoragesService $userStoragesService,
  27. private UserGlobalStoragesService $userGlobalStoragesService,
  28. private ClockInterface $clock,
  29. ) {
  30. }
  31. /**
  32. * Process storage ready for mounting
  33. *
  34. * @param StorageConfig $storage
  35. * @param IUser $user
  36. * @throws \OCP\AppFramework\QueryException
  37. */
  38. private function prepareStorageConfig(StorageConfig &$storage, IUser $user) {
  39. foreach ($storage->getBackendOptions() as $option => $value) {
  40. $storage->setBackendOption($option, \OCA\Files_External\MountConfig::substitutePlaceholdersInConfig($value, $user->getUID()));
  41. }
  42. $objectStore = $storage->getBackendOption('objectstore');
  43. if ($objectStore) {
  44. $objectClass = $objectStore['class'];
  45. if (!is_subclass_of($objectClass, '\OCP\Files\ObjectStore\IObjectStore')) {
  46. throw new \InvalidArgumentException('Invalid object store');
  47. }
  48. $storage->setBackendOption('objectstore', new $objectClass($objectStore));
  49. }
  50. $storage->getAuthMechanism()->manipulateStorageConfig($storage, $user);
  51. $storage->getBackend()->manipulateStorageConfig($storage, $user);
  52. }
  53. /**
  54. * Construct the storage implementation
  55. *
  56. * @param StorageConfig $storageConfig
  57. * @return Storage
  58. */
  59. private function constructStorage(StorageConfig $storageConfig) {
  60. $class = $storageConfig->getBackend()->getStorageClass();
  61. $storage = new $class($storageConfig->getBackendOptions());
  62. // auth mechanism should fire first
  63. $storage = $storageConfig->getBackend()->wrapStorage($storage);
  64. $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage);
  65. return $storage;
  66. }
  67. /**
  68. * Get all mountpoints applicable for the user
  69. *
  70. * @param \OCP\IUser $user
  71. * @param \OCP\Files\Storage\IStorageFactory $loader
  72. * @return \OCP\Files\Mount\IMountPoint[]
  73. */
  74. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  75. $this->userStoragesService->setUser($user);
  76. $this->userGlobalStoragesService->setUser($user);
  77. $storageConfigs = $this->userGlobalStoragesService->getAllStoragesForUser();
  78. $storages = array_map(function (StorageConfig $storageConfig) use ($user) {
  79. try {
  80. $this->prepareStorageConfig($storageConfig, $user);
  81. return $this->constructStorage($storageConfig);
  82. } catch (\Exception $e) {
  83. // propagate exception into filesystem
  84. return new FailedStorage(['exception' => $e]);
  85. }
  86. }, $storageConfigs);
  87. \OC\Files\Cache\Storage::getGlobalCache()->loadForStorageIds(array_map(function (Storage\IStorage $storage) {
  88. return $storage->getId();
  89. }, $storages));
  90. $availableStorages = array_map(function (Storage\IStorage $storage, StorageConfig $storageConfig) {
  91. try {
  92. $availability = $storage->getAvailability();
  93. if (!$availability['available'] && !Availability::shouldRecheck($availability)) {
  94. $storage = new FailedStorage([
  95. 'exception' => new StorageNotAvailableException('Storage with mount id ' . $storageConfig->getId() . ' is not available')
  96. ]);
  97. }
  98. } catch (\Exception $e) {
  99. // propagate exception into filesystem
  100. $storage = new FailedStorage(['exception' => $e]);
  101. }
  102. return $storage;
  103. }, $storages, $storageConfigs);
  104. $mounts = array_map(function (StorageConfig $storageConfig, Storage\IStorage $storage) use ($user, $loader) {
  105. $storage->setOwner($user->getUID());
  106. if ($storageConfig->getType() === StorageConfig::MOUNT_TYPE_PERSONAL) {
  107. return new PersonalMount(
  108. $this->userStoragesService,
  109. $storageConfig,
  110. $storageConfig->getId(),
  111. new KnownMtime([
  112. 'storage' => $storage,
  113. 'clock' => $this->clock,
  114. ]),
  115. '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
  116. null,
  117. $loader,
  118. $storageConfig->getMountOptions(),
  119. $storageConfig->getId()
  120. );
  121. } else {
  122. return new SystemMountPoint(
  123. $storageConfig,
  124. $storage,
  125. '/' . $user->getUID() . '/files' . $storageConfig->getMountPoint(),
  126. null,
  127. $loader,
  128. $storageConfig->getMountOptions(),
  129. $storageConfig->getId()
  130. );
  131. }
  132. }, $storageConfigs, $availableStorages);
  133. $this->userStoragesService->resetUser();
  134. $this->userGlobalStoragesService->resetUser();
  135. return $mounts;
  136. }
  137. }