PreviewController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Controller;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\Http\FileDisplayResponse;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\Files\File;
  32. use OCP\Files\IRootFolder;
  33. use OCP\Files\Node;
  34. use OCP\Files\NotFoundException;
  35. use OCP\IPreview;
  36. use OCP\IRequest;
  37. class PreviewController extends Controller {
  38. /** @var string */
  39. private $userId;
  40. /** @var IRootFolder */
  41. private $root;
  42. /** @var IPreview */
  43. private $preview;
  44. /**
  45. * PreviewController constructor.
  46. *
  47. * @param string $appName
  48. * @param IRequest $request
  49. * @param IPreview $preview
  50. * @param IRootFolder $root
  51. * @param string $userId
  52. * @param ITimeFactory $timeFactory
  53. */
  54. public function __construct(string $appName,
  55. IRequest $request,
  56. IPreview $preview,
  57. IRootFolder $root,
  58. ?string $userId
  59. ) {
  60. parent::__construct($appName, $request);
  61. $this->preview = $preview;
  62. $this->root = $root;
  63. $this->userId = $userId;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. * @NoCSRFRequired
  68. *
  69. * @param string $file
  70. * @param int $x
  71. * @param int $y
  72. * @param bool $a
  73. * @param bool $forceIcon
  74. * @param string $mode
  75. * @return DataResponse|FileDisplayResponse
  76. */
  77. public function getPreview (
  78. string $file = '',
  79. int $x = 32,
  80. int $y = 32,
  81. bool $a = false,
  82. bool $forceIcon = true,
  83. string $mode = 'fill'): Http\Response {
  84. if ($file === '' || $x === 0 || $y === 0) {
  85. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  86. }
  87. try {
  88. $userFolder = $this->root->getUserFolder($this->userId);
  89. $node = $userFolder->get($file);
  90. } catch (NotFoundException $e) {
  91. return new DataResponse([], Http::STATUS_NOT_FOUND);
  92. }
  93. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
  94. }
  95. /**
  96. * @NoAdminRequired
  97. * @NoCSRFRequired
  98. *
  99. * @param int $fileId
  100. * @param int $x
  101. * @param int $y
  102. * @param bool $a
  103. * @param bool $forceIcon
  104. * @param string $mode
  105. *
  106. * @return DataResponse|FileDisplayResponse
  107. */
  108. public function getPreviewByFileId(
  109. int $fileId = -1,
  110. int $x = 32,
  111. int $y = 32,
  112. bool $a = false,
  113. bool $forceIcon = true,
  114. string $mode = 'fill') {
  115. if ($fileId === -1 || $x === 0 || $y === 0) {
  116. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  117. }
  118. $userFolder = $this->root->getUserFolder($this->userId);
  119. $nodes = $userFolder->getById($fileId);
  120. if (\count($nodes) === 0) {
  121. return new DataResponse([], Http::STATUS_NOT_FOUND);
  122. }
  123. $node = array_pop($nodes);
  124. return $this->fetchPreview($node, $x, $y, $a, $forceIcon, $mode);
  125. }
  126. /**
  127. * @param Node $node
  128. * @param int $x
  129. * @param int $y
  130. * @param bool $a
  131. * @param bool $forceIcon
  132. * @param string $mode
  133. * @return DataResponse|FileDisplayResponse
  134. */
  135. private function fetchPreview(
  136. Node $node,
  137. int $x,
  138. int $y,
  139. bool $a = false,
  140. bool $forceIcon = true,
  141. string $mode) : 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. try {
  149. $f = $this->preview->getPreview($node, $x, $y, !$a, $mode);
  150. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  151. $response->cacheFor(3600 * 24);
  152. return $response;
  153. } catch (NotFoundException $e) {
  154. return new DataResponse([], Http::STATUS_NOT_FOUND);
  155. } catch (\InvalidArgumentException $e) {
  156. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  157. }
  158. }
  159. }