You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.6 KiB

  1. <?php
  2. /**
  3. *
  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($appName,
  47. IRequest $request,
  48. IManager $shareManager) {
  49. parent::__construct($appName, $request);
  50. $this->shareManager = $shareManager;
  51. }
  52. /**
  53. * @PublicPage
  54. * @NoCSRFRequired
  55. *
  56. * @param string $t
  57. * @param null $password
  58. * @param null $dir
  59. * @return JSONResponse
  60. * @throws ShareNotFound
  61. */
  62. public function info($t, $password = null, $dir = null) {
  63. try {
  64. $share = $this->shareManager->getShareByToken($t);
  65. } catch (ShareNotFound $e) {
  66. return new JSONResponse([], Http::STATUS_NOT_FOUND);
  67. }
  68. if ($share->getPassword() && !$this->shareManager->checkPassword($share, $password)) {
  69. return new JSONResponse([], Http::STATUS_FORBIDDEN);
  70. }
  71. if (!($share->getPermissions() & Constants::PERMISSION_READ)) {
  72. return new JSONResponse([], Http::STATUS_FORBIDDEN);
  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));
  83. }
  84. private function parseNode(Node $node, int $permissionMask) {
  85. if ($node instanceof File) {
  86. return $this->parseFile($node, $permissionMask);
  87. }
  88. return $this->parseFolder($node, $permissionMask);
  89. }
  90. private function parseFile(File $file, int $permissionMask) {
  91. return $this->format($file, $permissionMask);
  92. }
  93. private function parseFolder(Folder $folder, int $permissionMask) {
  94. $data = $this->format($folder, $permissionMask);
  95. $data['children'] = [];
  96. $nodes = $folder->getDirectoryListing();
  97. foreach ($nodes as $node) {
  98. $data['children'][] = $this->parseNode($node, $permissionMask);
  99. }
  100. return $data;
  101. }
  102. private function format(Node $node, int $permissionMask) {
  103. $entry = [];
  104. $entry['id'] = $node->getId();
  105. $entry['parentId'] = $node->getParent()->getId();
  106. $entry['mtime'] = $node->getMTime();
  107. $entry['name'] = $node->getName();
  108. $entry['permissions'] = $node->getPermissions() & $permissionMask;
  109. $entry['mimetype'] = $node->getMimetype();
  110. $entry['size'] = $node->getSize();
  111. $entry['type'] = $node->getType();
  112. $entry['etag'] = $node->getEtag();
  113. return $entry;
  114. }
  115. }