PreviewController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Trashbin\Controller;
  8. use OCA\Files_Trashbin\Trash\ITrashManager;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\Files\Folder;
  14. use OCP\Files\IMimeTypeDetector;
  15. use OCP\Files\IRootFolder;
  16. use OCP\Files\NotFoundException;
  17. use OCP\IPreview;
  18. use OCP\IRequest;
  19. use OCP\IUserSession;
  20. class PreviewController extends Controller {
  21. /** @var IRootFolder */
  22. private $rootFolder;
  23. /** @var ITrashManager */
  24. private $trashManager;
  25. /** @var IUserSession */
  26. private $userSession;
  27. /** @var IMimeTypeDetector */
  28. private $mimeTypeDetector;
  29. /** @var IPreview */
  30. private $previewManager;
  31. /** @var ITimeFactory */
  32. private $time;
  33. public function __construct(
  34. string $appName,
  35. IRequest $request,
  36. IRootFolder $rootFolder,
  37. ITrashManager $trashManager,
  38. IUserSession $userSession,
  39. IMimeTypeDetector $mimeTypeDetector,
  40. IPreview $previewManager,
  41. ITimeFactory $time
  42. ) {
  43. parent::__construct($appName, $request);
  44. $this->trashManager = $trashManager;
  45. $this->rootFolder = $rootFolder;
  46. $this->userSession = $userSession;
  47. $this->mimeTypeDetector = $mimeTypeDetector;
  48. $this->previewManager = $previewManager;
  49. $this->time = $time;
  50. }
  51. /**
  52. * @NoAdminRequired
  53. * @NoCSRFRequired
  54. *
  55. * Get the preview for a file
  56. *
  57. * @param int $fileId ID of the file
  58. * @param int $x Width of the preview
  59. * @param int $y Height of the preview
  60. * @param bool $a Whether to not crop the preview
  61. *
  62. * @return Http\FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  63. *
  64. * 200: Preview returned
  65. * 400: Getting preview is not possible
  66. * 404: Preview not found
  67. */
  68. public function getPreview(
  69. int $fileId = -1,
  70. int $x = 32,
  71. int $y = 32,
  72. bool $a = false,
  73. ) {
  74. if ($fileId === -1 || $x === 0 || $y === 0) {
  75. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  76. }
  77. try {
  78. $file = $this->trashManager->getTrashNodeById($this->userSession->getUser(), $fileId);
  79. if ($file === null) {
  80. return new DataResponse([], Http::STATUS_NOT_FOUND);
  81. }
  82. if ($file instanceof Folder) {
  83. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  84. }
  85. $pathParts = pathinfo($file->getName());
  86. $extension = $pathParts['extension'] ?? '';
  87. $fileName = $pathParts['filename'];
  88. /*
  89. * Files in the root of the trashbin are timetamped.
  90. * So we have to strip that in order to properly detect the mimetype of the file.
  91. */
  92. if (preg_match('/d\d+/', $extension)) {
  93. $mimeType = $this->mimeTypeDetector->detectPath($fileName);
  94. } else {
  95. $mimeType = $this->mimeTypeDetector->detectPath($file->getName());
  96. }
  97. $f = $this->previewManager->getPreview($file, $x, $y, !$a, IPreview::MODE_FILL, $mimeType);
  98. $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  99. // Cache previews for 24H
  100. $response->cacheFor(3600 * 24);
  101. return $response;
  102. } catch (NotFoundException $e) {
  103. return new DataResponse([], Http::STATUS_NOT_FOUND);
  104. } catch (\InvalidArgumentException $e) {
  105. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  106. }
  107. }
  108. }