PublicPreviewController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 OCP\AppFramework\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. try {
  98. $node = $share->getNode();
  99. if ($node instanceof Folder) {
  100. $file = $node->get($file);
  101. } else {
  102. $file = $node;
  103. }
  104. $f = $this->previewManager->getPreview($file, $x, $y, !$a);
  105. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  106. $response->cacheFor(3600 * 24);
  107. return $response;
  108. } catch (NotFoundException $e) {
  109. return new DataResponse([], Http::STATUS_NOT_FOUND);
  110. } catch (\InvalidArgumentException $e) {
  111. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  112. }
  113. }
  114. /**
  115. * @PublicPage
  116. * @NoCSRFRequired
  117. * @NoSameSiteCookieRequired
  118. *
  119. * @param $token
  120. * @return DataResponse|FileDisplayResponse
  121. */
  122. public function directLink($token) {
  123. // No token no image
  124. if ($token === '') {
  125. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  126. }
  127. // No share no image
  128. try {
  129. $share = $this->shareManager->getShareByToken($token);
  130. } catch (ShareNotFound $e) {
  131. return new DataResponse([], Http::STATUS_NOT_FOUND);
  132. }
  133. // No permissions no image
  134. if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) {
  135. return new DataResponse([], Http::STATUS_FORBIDDEN);
  136. }
  137. // Password protected shares have no direct link!
  138. if ($share->getPassword() !== null) {
  139. return new DataResponse([], Http::STATUS_FORBIDDEN);
  140. }
  141. try {
  142. $node = $share->getNode();
  143. if ($node instanceof Folder) {
  144. // Direct link only works for single files
  145. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  146. }
  147. $f = $this->previewManager->getPreview($node, -1, -1, false);
  148. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  149. $response->cacheFor(3600 * 24);
  150. return $response;
  151. } catch (NotFoundException $e) {
  152. return new DataResponse([], Http::STATUS_NOT_FOUND);
  153. } catch (\InvalidArgumentException $e) {
  154. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  155. }
  156. }
  157. }