PreviewController.php 4.2 KB

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