1
0

PreviewController.php 3.0 KB

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