ShareInfoController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Controller;
  7. use OCA\Files_External\NotFoundException;
  8. use OCA\Files_Sharing\ResponseDefinitions;
  9. use OCP\AppFramework\ApiController;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\BruteForceProtection;
  12. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  13. use OCP\AppFramework\Http\Attribute\PublicPage;
  14. use OCP\AppFramework\Http\JSONResponse;
  15. use OCP\Constants;
  16. use OCP\Files\File;
  17. use OCP\Files\Folder;
  18. use OCP\Files\Node;
  19. use OCP\IRequest;
  20. use OCP\Share\Exceptions\ShareNotFound;
  21. use OCP\Share\IManager;
  22. /**
  23. * @psalm-import-type Files_SharingShareInfo from ResponseDefinitions
  24. */
  25. class ShareInfoController extends ApiController {
  26. /**
  27. * ShareInfoController constructor.
  28. *
  29. * @param string $appName
  30. * @param IRequest $request
  31. * @param IManager $shareManager
  32. */
  33. public function __construct(
  34. string $appName,
  35. IRequest $request,
  36. private IManager $shareManager,
  37. ) {
  38. parent::__construct($appName, $request);
  39. }
  40. /**
  41. * Get the info about a share
  42. *
  43. * @param string $t Token of the share
  44. * @param string|null $password Password of the share
  45. * @param string|null $dir Subdirectory to get info about
  46. * @param int $depth Maximum depth to get info about
  47. * @return JSONResponse<Http::STATUS_OK, Files_SharingShareInfo, array{}>|JSONResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, list<empty>, array{}>
  48. *
  49. * 200: Share info returned
  50. * 403: Getting share info is not allowed
  51. * 404: Share not found
  52. */
  53. #[PublicPage]
  54. #[NoCSRFRequired]
  55. #[BruteForceProtection(action: 'shareinfo')]
  56. public function info(string $t, ?string $password = null, ?string $dir = null, int $depth = -1): JSONResponse {
  57. try {
  58. $share = $this->shareManager->getShareByToken($t);
  59. } catch (ShareNotFound $e) {
  60. $response = new JSONResponse([], Http::STATUS_NOT_FOUND);
  61. $response->throttle(['token' => $t]);
  62. return $response;
  63. }
  64. if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
  65. $response = new JSONResponse([], Http::STATUS_FORBIDDEN);
  66. $response->throttle(['token' => $t]);
  67. return $response;
  68. }
  69. if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
  70. $response = new JSONResponse([], Http::STATUS_FORBIDDEN);
  71. $response->throttle(['token' => $t]);
  72. return $response;
  73. }
  74. $permissionMask = $share->getPermissions();
  75. $node = $share->getNode();
  76. if ($dir !== null && $node instanceof Folder) {
  77. try {
  78. $node = $node->get($dir);
  79. } catch (NotFoundException $e) {
  80. }
  81. }
  82. return new JSONResponse($this->parseNode($node, $permissionMask, $depth));
  83. }
  84. /**
  85. * @return Files_SharingShareInfo
  86. */
  87. private function parseNode(Node $node, int $permissionMask, int $depth): array {
  88. if ($node instanceof File) {
  89. return $this->parseFile($node, $permissionMask);
  90. }
  91. /** @var Folder $node */
  92. return $this->parseFolder($node, $permissionMask, $depth);
  93. }
  94. /**
  95. * @return Files_SharingShareInfo
  96. */
  97. private function parseFile(File $file, int $permissionMask): array {
  98. return $this->format($file, $permissionMask);
  99. }
  100. /**
  101. * @return Files_SharingShareInfo
  102. */
  103. private function parseFolder(Folder $folder, int $permissionMask, int $depth): array {
  104. $data = $this->format($folder, $permissionMask);
  105. if ($depth === 0) {
  106. return $data;
  107. }
  108. $data['children'] = [];
  109. $nodes = $folder->getDirectoryListing();
  110. foreach ($nodes as $node) {
  111. $data['children'][] = $this->parseNode($node, $permissionMask, $depth <= -1 ? -1 : $depth - 1);
  112. }
  113. return $data;
  114. }
  115. /**
  116. * @return Files_SharingShareInfo
  117. */
  118. private function format(Node $node, int $permissionMask): array {
  119. $entry = [];
  120. $entry['id'] = $node->getId();
  121. $entry['parentId'] = $node->getParent()->getId();
  122. $entry['mtime'] = $node->getMTime();
  123. $entry['name'] = $node->getName();
  124. $entry['permissions'] = $node->getPermissions() & $permissionMask;
  125. $entry['mimetype'] = $node->getMimetype();
  126. $entry['size'] = $node->getSize();
  127. $entry['type'] = $node->getType();
  128. $entry['etag'] = $node->getEtag();
  129. return $entry;
  130. }
  131. }