PublicPreviewController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\Http\FileDisplayResponse;
  29. use OCP\AppFramework\PublicShareController;
  30. use OCP\Constants;
  31. use OCP\Files\Folder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\IPreview;
  34. use OCP\IRequest;
  35. use OCP\ISession;
  36. use OCP\Share\Exceptions\ShareNotFound;
  37. use OCP\Share\IManager as ShareManager;
  38. use OCP\Share\IShare;
  39. class PublicPreviewController extends PublicShareController {
  40. /** @var ShareManager */
  41. private $shareManager;
  42. /** @var IPreview */
  43. private $previewManager;
  44. /** @var IShare */
  45. private $share;
  46. public function __construct(string $appName,
  47. IRequest $request,
  48. ShareManager $shareManger,
  49. ISession $session,
  50. IPreview $previewManager) {
  51. parent::__construct($appName, $request, $session);
  52. $this->shareManager = $shareManger;
  53. $this->previewManager = $previewManager;
  54. }
  55. protected function getPasswordHash(): string {
  56. return $this->share->getPassword();
  57. }
  58. public function isValidToken(): bool {
  59. try {
  60. $this->share = $this->shareManager->getShareByToken($this->getToken());
  61. return true;
  62. } catch (ShareNotFound $e) {
  63. return false;
  64. }
  65. }
  66. protected function isPasswordProtected(): bool {
  67. return $this->share->getPassword() !== null;
  68. }
  69. /**
  70. * @PublicPage
  71. * @NoCSRFRequired
  72. *
  73. * @param string $file
  74. * @param int $x
  75. * @param int $y
  76. * @param bool $a
  77. * @return DataResponse|FileDisplayResponse
  78. */
  79. public function getPreview(
  80. string $token,
  81. string $file = '',
  82. int $x = 32,
  83. int $y = 32,
  84. $a = false
  85. ) {
  86. if ($token === '' || $x === 0 || $y === 0) {
  87. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  88. }
  89. try {
  90. $share = $this->shareManager->getShareByToken($token);
  91. } catch (ShareNotFound $e) {
  92. return new DataResponse([], Http::STATUS_NOT_FOUND);
  93. }
  94. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  95. return new DataResponse([], Http::STATUS_FORBIDDEN);
  96. }
  97. $attributes = $share->getAttributes();
  98. if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
  99. return new DataResponse([], Http::STATUS_FORBIDDEN);
  100. }
  101. try {
  102. $node = $share->getNode();
  103. if ($node instanceof Folder) {
  104. $file = $node->get($file);
  105. } else {
  106. $file = $node;
  107. }
  108. $f = $this->previewManager->getPreview($file, $x, $y, !$a);
  109. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  110. $response->cacheFor(3600 * 24);
  111. return $response;
  112. } catch (NotFoundException $e) {
  113. return new DataResponse([], Http::STATUS_NOT_FOUND);
  114. } catch (\InvalidArgumentException $e) {
  115. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  116. }
  117. }
  118. /**
  119. * @PublicPage
  120. * @NoCSRFRequired
  121. * @NoSameSiteCookieRequired
  122. *
  123. * @param $token
  124. * @return DataResponse|FileDisplayResponse
  125. */
  126. public function directLink(string $token) {
  127. // No token no image
  128. if ($token === '') {
  129. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  130. }
  131. // No share no image
  132. try {
  133. $share = $this->shareManager->getShareByToken($token);
  134. } catch (ShareNotFound $e) {
  135. return new DataResponse([], Http::STATUS_NOT_FOUND);
  136. }
  137. // No permissions no image
  138. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  139. return new DataResponse([], Http::STATUS_FORBIDDEN);
  140. }
  141. // Password protected shares have no direct link!
  142. if ($share->getPassword() !== null) {
  143. return new DataResponse([], Http::STATUS_FORBIDDEN);
  144. }
  145. $attributes = $share->getAttributes();
  146. if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
  147. return new DataResponse([], Http::STATUS_FORBIDDEN);
  148. }
  149. try {
  150. $node = $share->getNode();
  151. if ($node instanceof Folder) {
  152. // Direct link only works for single files
  153. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  154. }
  155. $f = $this->previewManager->getPreview($node, -1, -1, false);
  156. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  157. $response->cacheFor(3600 * 24);
  158. return $response;
  159. } catch (NotFoundException $e) {
  160. return new DataResponse([], Http::STATUS_NOT_FOUND);
  161. } catch (\InvalidArgumentException $e) {
  162. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  163. }
  164. }
  165. }