1
0

PreviewController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Jakub Onderka <ahoj@jakubonderka.cz>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author simonspa <1677436+simonspa@users.noreply.github.com>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Files_Trashbin\Controller;
  29. use OCA\Files_Trashbin\Trash\ITrashManager;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\Files\Folder;
  35. use OCP\Files\IMimeTypeDetector;
  36. use OCP\Files\IRootFolder;
  37. use OCP\Files\NotFoundException;
  38. use OCP\IPreview;
  39. use OCP\IRequest;
  40. use OCP\IUserSession;
  41. class PreviewController extends Controller {
  42. /** @var IRootFolder */
  43. private $rootFolder;
  44. /** @var ITrashManager */
  45. private $trashManager;
  46. /** @var IUserSession */
  47. private $userSession;
  48. /** @var IMimeTypeDetector */
  49. private $mimeTypeDetector;
  50. /** @var IPreview */
  51. private $previewManager;
  52. /** @var ITimeFactory */
  53. private $time;
  54. public function __construct(
  55. string $appName,
  56. IRequest $request,
  57. IRootFolder $rootFolder,
  58. ITrashManager $trashManager,
  59. IUserSession $userSession,
  60. IMimeTypeDetector $mimeTypeDetector,
  61. IPreview $previewManager,
  62. ITimeFactory $time
  63. ) {
  64. parent::__construct($appName, $request);
  65. $this->trashManager = $trashManager;
  66. $this->rootFolder = $rootFolder;
  67. $this->userSession = $userSession;
  68. $this->mimeTypeDetector = $mimeTypeDetector;
  69. $this->previewManager = $previewManager;
  70. $this->time = $time;
  71. }
  72. /**
  73. * @NoAdminRequired
  74. * @NoCSRFRequired
  75. *
  76. * Get the preview for a file
  77. *
  78. * @param int $fileId ID of the file
  79. * @param int $x Width of the preview
  80. * @param int $y Height of the preview
  81. * @param bool $a Whether to not crop the preview
  82. *
  83. * @return Http\FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  84. *
  85. * 200: Preview returned
  86. * 400: Getting preview is not possible
  87. * 404: Preview not found
  88. */
  89. public function getPreview(
  90. int $fileId = -1,
  91. int $x = 32,
  92. int $y = 32,
  93. bool $a = false,
  94. ) {
  95. if ($fileId === -1 || $x === 0 || $y === 0) {
  96. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  97. }
  98. try {
  99. $file = $this->trashManager->getTrashNodeById($this->userSession->getUser(), $fileId);
  100. if ($file === null) {
  101. return new DataResponse([], Http::STATUS_NOT_FOUND);
  102. }
  103. if ($file instanceof Folder) {
  104. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  105. }
  106. $pathParts = pathinfo($file->getName());
  107. $extension = $pathParts['extension'] ?? '';
  108. $fileName = $pathParts['filename'];
  109. /*
  110. * Files in the root of the trashbin are timetamped.
  111. * So we have to strip that in order to properly detect the mimetype of the file.
  112. */
  113. if (preg_match('/d\d+/', $extension)) {
  114. $mimeType = $this->mimeTypeDetector->detectPath($fileName);
  115. } else {
  116. $mimeType = $this->mimeTypeDetector->detectPath($file->getName());
  117. }
  118. $f = $this->previewManager->getPreview($file, $x, $y, !$a, IPreview::MODE_FILL, $mimeType);
  119. $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  120. // Cache previews for 24H
  121. $response->cacheFor(3600 * 24);
  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. }