ShareInfoController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public function info(string $t, ?string $password = null, ?string $dir = null, int $depth = -1): JSONResponse {
  58. try {
  59. $share = $this->shareManager->getShareByToken($t);
  60. } catch (ShareNotFound $e) {
  61. $response = new JSONResponse([], Http::STATUS_NOT_FOUND);
  62. $response->throttle(['token' => $t]);
  63. return $response;
  64. }
  65. if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
  66. $response = new JSONResponse([], Http::STATUS_FORBIDDEN);
  67. $response->throttle(['token' => $t]);
  68. return $response;
  69. }
  70. if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
  71. $response = new JSONResponse([], Http::STATUS_FORBIDDEN);
  72. $response->throttle(['token' => $t]);
  73. return $response;
  74. }
  75. $permissionMask = $share->getPermissions();
  76. $node = $share->getNode();
  77. if ($dir !== null && $node instanceof Folder) {
  78. try {
  79. $node = $node->get($dir);
  80. } catch (NotFoundException $e) {
  81. }
  82. }
  83. return new JSONResponse($this->parseNode($node, $permissionMask, $depth));
  84. }
  85. private function parseNode(Node $node, int $permissionMask, int $depth): array {
  86. if ($node instanceof File) {
  87. return $this->parseFile($node, $permissionMask);
  88. }
  89. /** @var Folder $node */
  90. return $this->parseFolder($node, $permissionMask, $depth);
  91. }
  92. private function parseFile(File $file, int $permissionMask): array {
  93. return $this->format($file, $permissionMask);
  94. }
  95. private function parseFolder(Folder $folder, int $permissionMask, int $depth): array {
  96. $data = $this->format($folder, $permissionMask);
  97. if ($depth === 0) {
  98. return $data;
  99. }
  100. $data['children'] = [];
  101. $nodes = $folder->getDirectoryListing();
  102. foreach ($nodes as $node) {
  103. $data['children'][] = $this->parseNode($node, $permissionMask, $depth <= -1 ? -1 : $depth - 1);
  104. }
  105. return $data;
  106. }
  107. private function format(Node $node, int $permissionMask): array {
  108. $entry = [];
  109. $entry['id'] = $node->getId();
  110. $entry['parentId'] = $node->getParent()->getId();
  111. $entry['mtime'] = $node->getMTime();
  112. $entry['name'] = $node->getName();
  113. $entry['permissions'] = $node->getPermissions() & $permissionMask;
  114. $entry['mimetype'] = $node->getMimetype();
  115. $entry['size'] = $node->getSize();
  116. $entry['type'] = $node->getType();
  117. $entry['etag'] = $node->getEtag();
  118. return $entry;
  119. }
  120. }