ShareInfoController.php 4.1 KB

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