1
0

ApiController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files_External\Controller;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\ResponseDefinitions;
  11. use OCA\Files_External\Service\UserGlobalStoragesService;
  12. use OCA\Files_External\Service\UserStoragesService;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  15. use OCP\AppFramework\Http\DataResponse;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\Constants;
  18. use OCP\IRequest;
  19. /**
  20. * @psalm-import-type Files_ExternalMount from ResponseDefinitions
  21. */
  22. class ApiController extends OCSController {
  23. public function __construct(
  24. string $appName,
  25. IRequest $request,
  26. private UserGlobalStoragesService $userGlobalStoragesService,
  27. private UserStoragesService $userStoragesService,
  28. ) {
  29. parent::__construct($appName, $request);
  30. }
  31. /**
  32. * Formats the given mount config to a mount entry.
  33. *
  34. * @param string $mountPoint mount point name, relative to the data dir
  35. * @param StorageConfig $mountConfig mount config to format
  36. *
  37. * @return Files_ExternalMount
  38. */
  39. private function formatMount(string $mountPoint, StorageConfig $mountConfig): array {
  40. // split path from mount point
  41. $path = \dirname($mountPoint);
  42. if ($path === '.' || $path === '/') {
  43. $path = '';
  44. }
  45. $isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN;
  46. $permissions = Constants::PERMISSION_READ;
  47. // personal mounts can be deleted
  48. if (!$isSystemMount) {
  49. $permissions |= Constants::PERMISSION_DELETE;
  50. }
  51. $entry = [
  52. 'id' => $mountConfig->getId(),
  53. 'type' => 'dir',
  54. 'name' => basename($mountPoint),
  55. 'path' => $path,
  56. 'permissions' => $permissions,
  57. 'scope' => $isSystemMount ? 'system' : 'personal',
  58. 'backend' => $mountConfig->getBackend()->getText(),
  59. 'class' => $mountConfig->getBackend()->getIdentifier(),
  60. 'config' => $mountConfig->jsonSerialize(true),
  61. ];
  62. return $entry;
  63. }
  64. /**
  65. * Get the mount points visible for this user
  66. *
  67. * @return DataResponse<Http::STATUS_OK, list<Files_ExternalMount>, array{}>
  68. *
  69. * 200: User mounts returned
  70. */
  71. #[NoAdminRequired]
  72. public function getUserMounts(): DataResponse {
  73. $entries = [];
  74. $mountPoints = [];
  75. foreach ($this->userGlobalStoragesService->getStorages() as $storage) {
  76. $mountPoint = $storage->getMountPoint();
  77. $mountPoints[$mountPoint] = $storage;
  78. }
  79. foreach ($this->userStoragesService->getStorages() as $storage) {
  80. $mountPoint = $storage->getMountPoint();
  81. $mountPoints[$mountPoint] = $storage;
  82. }
  83. foreach ($mountPoints as $mountPoint => $mount) {
  84. $entries[] = $this->formatMount($mountPoint, $mount);
  85. }
  86. return new DataResponse($entries);
  87. }
  88. }