ShareInfoController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $isWritable = $share->getPermissions() & (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_CREATE);
  75. if (!$isWritable) {
  76. $this->addROWrapper();
  77. }
  78. $node = $share->getNode();
  79. if ($dir !== null && $node instanceof Folder) {
  80. try {
  81. $node = $node->get($dir);
  82. } catch (NotFoundException $e) {
  83. }
  84. }
  85. return new JSONResponse($this->parseNode($node));
  86. }
  87. private function parseNode(Node $node) {
  88. if ($node instanceof File) {
  89. return $this->parseFile($node);
  90. }
  91. return $this->parseFolder($node);
  92. }
  93. private function parseFile(File $file) {
  94. return $this->format($file);
  95. }
  96. private function parseFolder(Folder $folder) {
  97. $data = $this->format($folder);
  98. $data['children'] = [];
  99. $nodes = $folder->getDirectoryListing();
  100. foreach ($nodes as $node) {
  101. $data['children'][] = $this->parseNode($node);
  102. }
  103. return $data;
  104. }
  105. private function format(Node $node) {
  106. $entry = [];
  107. $entry['id'] = $node->getId();
  108. $entry['parentId'] = $node->getParent()->getId();
  109. $entry['mtime'] = $node->getMTime();
  110. $entry['name'] = $node->getName();
  111. $entry['permissions'] = $node->getPermissions();
  112. $entry['mimetype'] = $node->getMimetype();
  113. $entry['size'] = $node->getSize();
  114. $entry['type'] = $node->getType();
  115. $entry['etag'] = $node->getEtag();
  116. return $entry;
  117. }
  118. protected function addROWrapper() {
  119. // FIXME: should not add storage wrappers outside of preSetup, need to find a better way
  120. $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false);
  121. \OC\Files\Filesystem::addStorageWrapper('readonly', function ($mountPoint, $storage) {
  122. return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_SHARE));
  123. });
  124. \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog);
  125. }
  126. }