ShareInfoController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Sharing\Controller;
  26. use OCA\Files_External\NotFoundException;
  27. use OCA\Files_Sharing\ResponseDefinitions;
  28. use OCP\AppFramework\ApiController;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\Constants;
  32. use OCP\Files\File;
  33. use OCP\Files\Folder;
  34. use OCP\Files\Node;
  35. use OCP\IRequest;
  36. use OCP\Share\Exceptions\ShareNotFound;
  37. use OCP\Share\IManager;
  38. /**
  39. * @psalm-import-type Files_SharingShareInfo from ResponseDefinitions
  40. */
  41. class ShareInfoController extends ApiController {
  42. /** @var IManager */
  43. private $shareManager;
  44. /**
  45. * ShareInfoController constructor.
  46. *
  47. * @param string $appName
  48. * @param IRequest $request
  49. * @param IManager $shareManager
  50. */
  51. public function __construct(string $appName,
  52. IRequest $request,
  53. IManager $shareManager) {
  54. parent::__construct($appName, $request);
  55. $this->shareManager = $shareManager;
  56. }
  57. /**
  58. * @PublicPage
  59. * @NoCSRFRequired
  60. * @BruteForceProtection(action=shareinfo)
  61. *
  62. * Get the info about a share
  63. *
  64. * @param string $t Token of the share
  65. * @param string|null $password Password of the share
  66. * @param string|null $dir Subdirectory to get info about
  67. * @param int $depth Maximum depth to get info about
  68. * @return JSONResponse<Http::STATUS_OK, Files_SharingShareInfo, array{}>|JSONResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  69. *
  70. * 200: Share info returned
  71. * 403: Getting share info is not allowed
  72. * 404: Share not found
  73. */
  74. public function info(string $t, ?string $password = null, ?string $dir = null, int $depth = -1): JSONResponse {
  75. try {
  76. $share = $this->shareManager->getShareByToken($t);
  77. } catch (ShareNotFound $e) {
  78. $response = new JSONResponse([], Http::STATUS_NOT_FOUND);
  79. $response->throttle(['token' => $t]);
  80. return $response;
  81. }
  82. if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
  83. $response = new JSONResponse([], Http::STATUS_FORBIDDEN);
  84. $response->throttle(['token' => $t]);
  85. return $response;
  86. }
  87. if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
  88. $response = new JSONResponse([], Http::STATUS_FORBIDDEN);
  89. $response->throttle(['token' => $t]);
  90. return $response;
  91. }
  92. $permissionMask = $share->getPermissions();
  93. $node = $share->getNode();
  94. if ($dir !== null && $node instanceof Folder) {
  95. try {
  96. $node = $node->get($dir);
  97. } catch (NotFoundException $e) {
  98. }
  99. }
  100. return new JSONResponse($this->parseNode($node, $permissionMask, $depth));
  101. }
  102. /**
  103. * @return Files_SharingShareInfo
  104. */
  105. private function parseNode(Node $node, int $permissionMask, int $depth): array {
  106. if ($node instanceof File) {
  107. return $this->parseFile($node, $permissionMask);
  108. }
  109. /** @var Folder $node */
  110. return $this->parseFolder($node, $permissionMask, $depth);
  111. }
  112. /**
  113. * @return Files_SharingShareInfo
  114. */
  115. private function parseFile(File $file, int $permissionMask): array {
  116. return $this->format($file, $permissionMask);
  117. }
  118. /**
  119. * @return Files_SharingShareInfo
  120. */
  121. private function parseFolder(Folder $folder, int $permissionMask, int $depth): array {
  122. $data = $this->format($folder, $permissionMask);
  123. if ($depth === 0) {
  124. return $data;
  125. }
  126. $data['children'] = [];
  127. $nodes = $folder->getDirectoryListing();
  128. foreach ($nodes as $node) {
  129. $data['children'][] = $this->parseNode($node, $permissionMask, $depth <= -1 ? -1 : $depth - 1);
  130. }
  131. return $data;
  132. }
  133. /**
  134. * @return Files_SharingShareInfo
  135. */
  136. private function format(Node $node, int $permissionMask): array {
  137. $entry = [];
  138. $entry['id'] = $node->getId();
  139. $entry['parentId'] = $node->getParent()->getId();
  140. $entry['mtime'] = $node->getMTime();
  141. $entry['name'] = $node->getName();
  142. $entry['permissions'] = $node->getPermissions() & $permissionMask;
  143. $entry['mimetype'] = $node->getMimetype();
  144. $entry['size'] = $node->getSize();
  145. $entry['type'] = $node->getType();
  146. $entry['etag'] = $node->getEtag();
  147. return $entry;
  148. }
  149. }