PreviewController.php 4.6 KB

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