ApiController.php 3.0 KB

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