MountProvider.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_Sharing\External;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\Federation\ICloudIdManager;
  10. use OCP\Files\Config\IMountProvider;
  11. use OCP\Files\Storage\IStorageFactory;
  12. use OCP\IDBConnection;
  13. use OCP\IUser;
  14. class MountProvider implements IMountProvider {
  15. public const STORAGE = '\OCA\Files_Sharing\External\Storage';
  16. /**
  17. * @var callable
  18. */
  19. private $managerProvider;
  20. /**
  21. * @param IDBConnection $connection
  22. * @param callable $managerProvider due to setup order we need a callable that return the manager instead of the manager itself
  23. * @param ICloudIdManager $cloudIdManager
  24. */
  25. public function __construct(
  26. private IDBConnection $connection,
  27. callable $managerProvider,
  28. private ICloudIdManager $cloudIdManager,
  29. ) {
  30. $this->managerProvider = $managerProvider;
  31. }
  32. public function getMount(IUser $user, $data, IStorageFactory $storageFactory) {
  33. $managerProvider = $this->managerProvider;
  34. $manager = $managerProvider();
  35. $data['manager'] = $manager;
  36. $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/');
  37. $data['mountpoint'] = $mountPoint;
  38. $data['cloudId'] = $this->cloudIdManager->getCloudId($data['owner'], $data['remote']);
  39. $data['certificateManager'] = \OC::$server->getCertificateManager();
  40. $data['HttpClientService'] = \OC::$server->getHTTPClientService();
  41. return new Mount(self::STORAGE, $mountPoint, $data, $manager, $storageFactory);
  42. }
  43. public function getMountsForUser(IUser $user, IStorageFactory $loader) {
  44. $qb = $this->connection->getQueryBuilder();
  45. $qb->select('remote', 'share_token', 'password', 'mountpoint', 'owner')
  46. ->from('share_external')
  47. ->where($qb->expr()->eq('user', $qb->createNamedParameter($user->getUID())))
  48. ->andWhere($qb->expr()->eq('accepted', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)));
  49. $result = $qb->executeQuery();
  50. $mounts = [];
  51. while ($row = $result->fetch()) {
  52. $row['manager'] = $this;
  53. $row['token'] = $row['share_token'];
  54. $mounts[] = $this->getMount($user, $row, $loader);
  55. }
  56. $result->closeCursor();
  57. return $mounts;
  58. }
  59. }