1
0

PreviewController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\DataResponse;
  31. use OCP\AppFramework\Http\FileDisplayResponse;
  32. use OCP\AppFramework\Http\RedirectResponse;
  33. use OCP\Files\File;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Node;
  36. use OCP\Files\NotFoundException;
  37. use OCP\IPreview;
  38. use OCP\IRequest;
  39. use OCP\Preview\IMimeIconProvider;
  40. class PreviewController extends Controller {
  41. public function __construct(
  42. string $appName,
  43. IRequest $request,
  44. private IPreview $preview,
  45. private IRootFolder $root,
  46. private ?string $userId,
  47. private IMimeIconProvider $mimeIconProvider,
  48. ) {
  49. parent::__construct($appName, $request);
  50. }
  51. /**
  52. * @NoAdminRequired
  53. * @NoCSRFRequired
  54. *
  55. * Get a preview by file path
  56. *
  57. * @param string $file Path of the file
  58. * @param int $x Width of the preview
  59. * @param int $y Height of the preview
  60. * @param bool $a Whether to not crop the preview
  61. * @param bool $forceIcon Force returning an icon
  62. * @param string $mode How to crop the image
  63. * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
  64. * @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{}>
  65. *
  66. * 200: Preview returned
  67. * 303: Redirect to the mime icon url if mimeFallback is true
  68. * 400: Getting preview is not possible
  69. * 403: Getting preview is not allowed
  70. * 404: Preview not found
  71. */
  72. public function getPreview(
  73. string $file = '',
  74. int $x = 32,
  75. int $y = 32,
  76. bool $a = false,
  77. bool $forceIcon = true,
  78. string $mode = 'fill',
  79. bool $mimeFallback = false): Http\Response {
  80. if ($file === '' || $x === 0 || $y === 0) {
  81. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  82. }
  83. try {
  84. $userFolder = $this->root->getUserFolder($this->userId);
  85. $node = $userFolder->get($file);
  86. } catch (NotFoundException $e) {
  87. return new DataResponse([], Http::STATUS_NOT_FOUND);
  88. }
  89. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
  90. }
  91. /**
  92. * @NoAdminRequired
  93. * @NoCSRFRequired
  94. *
  95. * Get a preview by file ID
  96. *
  97. * @param int $fileId ID of the file
  98. * @param int $x Width of the preview
  99. * @param int $y Height of the preview
  100. * @param bool $a Whether to not crop the preview
  101. * @param bool $forceIcon Force returning an icon
  102. * @param string $mode How to crop the image
  103. * @param bool $mimeFallback Whether to fallback to the mime icon if no preview is available
  104. * @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{}>
  105. *
  106. * 200: Preview returned
  107. * 303: Redirect to the mime icon url if mimeFallback is true
  108. * 400: Getting preview is not possible
  109. * 403: Getting preview is not allowed
  110. * 404: Preview not found
  111. */
  112. public function getPreviewByFileId(
  113. int $fileId = -1,
  114. int $x = 32,
  115. int $y = 32,
  116. bool $a = false,
  117. bool $forceIcon = true,
  118. string $mode = 'fill',
  119. bool $mimeFallback = false) {
  120. if ($fileId === -1 || $x === 0 || $y === 0) {
  121. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  122. }
  123. $userFolder = $this->root->getUserFolder($this->userId);
  124. $nodes = $userFolder->getById($fileId);
  125. if (\count($nodes) === 0) {
  126. return new DataResponse([], Http::STATUS_NOT_FOUND);
  127. }
  128. $node = array_pop($nodes);
  129. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode, $mimeFallback);
  130. }
  131. /**
  132. * @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{}>
  133. */
  134. private function fetchPreview(
  135. Node $node,
  136. int $x,
  137. int $y,
  138. bool $a,
  139. bool $forceIcon,
  140. string $mode,
  141. bool $mimeFallback = false) : Http\Response {
  142. if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) {
  143. return new DataResponse([], Http::STATUS_NOT_FOUND);
  144. }
  145. if (!$node->isReadable()) {
  146. return new DataResponse([], Http::STATUS_FORBIDDEN);
  147. }
  148. $storage = $node->getStorage();
  149. if ($storage->instanceOfStorage(SharedStorage::class)) {
  150. /** @var SharedStorage $storage */
  151. $share = $storage->getShare();
  152. $attributes = $share->getAttributes();
  153. if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
  154. return new DataResponse([], Http::STATUS_FORBIDDEN);
  155. }
  156. }
  157. try {
  158. $f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
  159. $response = new FileDisplayResponse($f, Http::STATUS_OK, [
  160. 'Content-Type' => $f->getMimeType(),
  161. ]);
  162. $response->cacheFor(3600 * 24, false, true);
  163. return $response;
  164. } catch (NotFoundException $e) {
  165. // If we have no preview enabled, we can redirect to the mime icon if any
  166. if ($mimeFallback) {
  167. if ($url = $this->mimeIconProvider->getMimeIconUrl($node->getMimeType())) {
  168. return new RedirectResponse($url);
  169. }
  170. }
  171. return new DataResponse([], Http::STATUS_NOT_FOUND);
  172. } catch (\InvalidArgumentException $e) {
  173. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  174. }
  175. }
  176. }