PreviewController.php 5.3 KB

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