ShareInfoController.php 4.0 KB

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