ApiController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jesús Macias <jmacias@solidgear.es>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_External\Controller;
  29. use OCA\Files_External\Lib\StorageConfig;
  30. use OCA\Files_External\Service\UserGlobalStoragesService;
  31. use OCA\Files_External\Service\UserStoragesService;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\IRequest;
  35. use OCP\IUserSession;
  36. class ApiController extends OCSController {
  37. /** @var IUserSession */
  38. private $userSession;
  39. /** @var UserGlobalStoragesService */
  40. private $userGlobalStoragesService;
  41. /** @var UserStoragesService */
  42. private $userStoragesService;
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. IUserSession $userSession,
  47. UserGlobalStoragesService $userGlobalStorageService,
  48. UserStoragesService $userStorageService
  49. ) {
  50. parent::__construct($appName, $request);
  51. $this->userSession = $userSession;
  52. $this->userGlobalStoragesService = $userGlobalStorageService;
  53. $this->userStoragesService = $userStorageService;
  54. }
  55. /**
  56. * Formats the given mount config to a mount entry.
  57. *
  58. * @param string $mountPoint mount point name, relative to the data dir
  59. * @param StorageConfig $mountConfig mount config to format
  60. *
  61. * @return array entry
  62. */
  63. private function formatMount(string $mountPoint, StorageConfig $mountConfig): array {
  64. // split path from mount point
  65. $path = \dirname($mountPoint);
  66. if ($path === '.' || $path === '/') {
  67. $path = '';
  68. }
  69. $isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN;
  70. $permissions = \OCP\Constants::PERMISSION_READ;
  71. // personal mounts can be deleted
  72. if (!$isSystemMount) {
  73. $permissions |= \OCP\Constants::PERMISSION_DELETE;
  74. }
  75. $entry = [
  76. 'name' => basename($mountPoint),
  77. 'path' => $path,
  78. 'type' => 'dir',
  79. 'backend' => $mountConfig->getBackend()->getText(),
  80. 'scope' => $isSystemMount ? 'system' : 'personal',
  81. 'permissions' => $permissions,
  82. 'id' => $mountConfig->getId(),
  83. 'class' => $mountConfig->getBackend()->getIdentifier(),
  84. ];
  85. return $entry;
  86. }
  87. /**
  88. * @NoAdminRequired
  89. *
  90. * Returns the mount points visible for this user.
  91. *
  92. * @return DataResponse share information
  93. */
  94. public function getUserMounts(): DataResponse {
  95. $entries = [];
  96. $mountPoints = [];
  97. foreach ($this->userGlobalStoragesService->getStorages() as $storage) {
  98. $mountPoint = $storage->getMountPoint();
  99. $mountPoints[$mountPoint] = $storage;
  100. }
  101. foreach ($this->userStoragesService->getStorages() as $storage) {
  102. $mountPoint = $storage->getMountPoint();
  103. $mountPoints[$mountPoint] = $storage;
  104. }
  105. foreach ($mountPoints as $mountPoint => $mount) {
  106. $entries[] = $this->formatMount($mountPoint, $mount);
  107. }
  108. return new DataResponse($entries);
  109. }
  110. }