PreviewController.php 3.8 KB

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