PreviewController.php 4.5 KB

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