PreviewController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * @return DataResponse|FileDisplayResponse
  53. */
  54. public function getPreview(
  55. string $file = '',
  56. int $x = 32,
  57. int $y = 32,
  58. bool $a = false,
  59. bool $forceIcon = true,
  60. string $mode = 'fill'): Http\Response {
  61. if ($file === '' || $x === 0 || $y === 0) {
  62. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  63. }
  64. try {
  65. $userFolder = $this->root->getUserFolder($this->userId);
  66. $node = $userFolder->get($file);
  67. } catch (NotFoundException $e) {
  68. return new DataResponse([], Http::STATUS_NOT_FOUND);
  69. }
  70. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
  71. }
  72. /**
  73. * @NoAdminRequired
  74. * @NoCSRFRequired
  75. *
  76. * @return DataResponse|FileDisplayResponse
  77. */
  78. public function getPreviewByFileId(
  79. int $fileId = -1,
  80. int $x = 32,
  81. int $y = 32,
  82. bool $a = false,
  83. bool $forceIcon = true,
  84. string $mode = 'fill') {
  85. if ($fileId === -1 || $x === 0 || $y === 0) {
  86. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  87. }
  88. $userFolder = $this->root->getUserFolder($this->userId);
  89. $nodes = $userFolder->getById($fileId);
  90. if (\count($nodes) === 0) {
  91. return new DataResponse([], Http::STATUS_NOT_FOUND);
  92. }
  93. $node = array_pop($nodes);
  94. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
  95. }
  96. /**
  97. * @return DataResponse|FileDisplayResponse
  98. */
  99. private function fetchPreview(
  100. Node $node,
  101. int $x,
  102. int $y,
  103. bool $a,
  104. bool $forceIcon,
  105. string $mode) : Http\Response {
  106. if (!($node instanceof File) || (!$forceIcon && !$this->preview->isAvailable($node))) {
  107. return new DataResponse([], Http::STATUS_NOT_FOUND);
  108. }
  109. if (!$node->isReadable()) {
  110. return new DataResponse([], Http::STATUS_FORBIDDEN);
  111. }
  112. $storage = $node->getStorage();
  113. if ($storage->instanceOfStorage(SharedStorage::class)) {
  114. /** @var SharedStorage $storage */
  115. $share = $storage->getShare();
  116. $attributes = $share->getAttributes();
  117. if ($attributes !== null && $attributes->getAttribute('permissions', 'download') === false) {
  118. return new DataResponse([], Http::STATUS_FORBIDDEN);
  119. }
  120. }
  121. try {
  122. $f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
  123. $response = new FileDisplayResponse($f, Http::STATUS_OK, [
  124. 'Content-Type' => $f->getMimeType(),
  125. ]);
  126. $response->cacheFor(3600 * 24, false, true);
  127. return $response;
  128. } catch (NotFoundException $e) {
  129. return new DataResponse([], Http::STATUS_NOT_FOUND);
  130. } catch (\InvalidArgumentException $e) {
  131. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  132. }
  133. }
  134. }