ConfigAdapter.php 5.2 KB

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