PreviewController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Controller;
  27. use OCA\Files_Sharing\SharedStorage;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\Http\FileDisplayResponse;
  33. use OCP\AppFramework\Http\RedirectResponse;
  34. use OCP\Files\File;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\Node;
  37. use OCP\Files\NotFoundException;
  38. use OCP\IPreview;
  39. use OCP\IRequest;
  40. use OCP\Preview\IMimeIconProvider;
  41. class PreviewController extends Controller {
  42. public function __construct(
  43. string $appName,
  44. IRequest $request,
  45. private IPreview $preview,
  46. private IRootFolder $root,
  47. private ?string $userId,
  48. private IMimeIconProvider $mimeIconProvider,
  49. ) {
  50. parent::__construct($appName, $request);
  51. }
  52. /**
  53. * @NoAdminRequired
  54. * @NoCSRFRequired
  55. *
  56. * Get a preview by file path
  57. *
  58. * @param string $file Path of the file
  59. * @param int $x Width of the preview
  60. * @param int $y Height of the preview
  61. * @param bool $a Whether to not crop the preview
  62. * @param bool $forceIcon Force returning an icon
  63. * @param string $mode How to crop the image
  64. * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
  65. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}>
  66. *
  67. * 200: Preview returned
  68. * 303: Redirect to the mime icon url if mimeFallback is true
  69. * 400: Getting preview is not possible
  70. * 403: Getting preview is not allowed
  71. * 404: Preview not found
  72. */
  73. #[FrontpageRoute(verb: 'GET', url: '/core/preview.png')]
  74. public function getPreview(
  75. string $file = '',
  76. int $x = 32,
  77. int $y = 32,
  78. bool $a = false,
  79. bool $forceIcon = true,
  80. string $mode = 'fill',
  81. bool $mimeFallback = false): Http\Response {
  82. if ($file === '' || $x === 0 || $y === 0) {
  83. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  84. }
  85. try {
  86. $userFolder = $this->root->getUserFolder($this->userId);
  87. $node = $userFolder->get($file);
  88. } catch (NotFoundException $e) {
  89. return new DataResponse([], Http::STATUS_NOT_FOUND);
  90. }
  91. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
  92. }
  93. /**
  94. * @NoAdminRequired
  95. * @NoCSRFRequired
  96. *
  97. * Get a preview by file ID
  98. *
  99. * @param int $fileId ID of the file
  100. * @param int $x Width of the preview
  101. * @param int $y Height of the preview
  102. * @param bool $a Whether to not crop the preview
  103. * @param bool $forceIcon Force returning an icon
  104. * @param string $mode How to crop the image
  105. * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
  106. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}>
  107. *
  108. * 200: Preview returned
  109. * 303: Redirect to the mime icon url if mimeFallback is true
  110. * 400: Getting preview is not possible
  111. * 403: Getting preview is not allowed
  112. * 404: Preview not found
  113. */
  114. #[FrontpageRoute(verb: 'GET', url: '/core/preview')]
  115. public function getPreviewByFileId(
  116. int $fileId = -1,
  117. int $x = 32,
  118. int $y = 32,
  119. bool $a = false,
  120. bool $forceIcon = true,
  121. string $mode = 'fill',
  122. bool $mimeFallback = false) {
  123. if ($fileId === -1 || $x === 0 || $y === 0) {
  124. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  125. }
  126. $userFolder = $this->root->getUserFolder($this->userId);
  127. $nodes = $userFolder->getById($fileId);
  128. if (\count($nodes) === 0) {
  129. return new DataResponse([], Http::STATUS_NOT_FOUND);
  130. }
  131. $node = array_pop($nodes);
  132. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
  133. }
  134. /**
  135. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND, array<empty>, array{}>|RedirectResponse<Http::STATUS_SEE_OTHER, array{}>
  136. */
  137. private function fetchPreview(
  138. Node $node,
  139. int $x,
  140. int $y,
  141. bool $a,
  142. bool $forceIcon,
  143. string $mode,
  144. bool $mimeFallback = false) : Http\Response {
  145. if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) {
  146. return new DataResponse([], Http::STATUS_NOT_FOUND);
  147. }
  148. if (!$node->isReadable()) {
  149. return new DataResponse([], Http::STATUS_FORBIDDEN);
  150. }
  151. $storage = $node->getStorage();
  152. if ($storage->instanceOfStorage(SharedStorage::class)) {
  153. /** @var SharedStorage $storage */
  154. $share = $storage->getShare();
  155. $attributes = $share->getAttributes();
  156. if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
  157. return new DataResponse([], Http::STATUS_FORBIDDEN);
  158. }
  159. }
  160. try {
  161. $f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
  162. $response = new FileDisplayResponse($f, Http::STATUS_OK, [
  163. 'Content-Type' => $f->getMimeType(),
  164. ]);
  165. $response->cacheFor(3600 * 24, false, true);
  166. return $response;
  167. } catch (NotFoundException $e) {
  168. // If we have no preview enabled, we can redirect to the mime icon if any
  169. if ($mimeFallback) {
  170. if ($url = $this->mimeIconProvider->getMimeIconUrl($node->getMimeType())) {
  171. return new RedirectResponse($url);
  172. }
  173. }
  174. return new DataResponse([], Http::STATUS_NOT_FOUND);
  175. } catch (\InvalidArgumentException $e) {
  176. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  177. }
  178. }
  179. }