1
0

PreviewController.php 3.4 KB

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