PreviewController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Versions\Controller;
  7. use OCA\Files_Versions\Versions\IVersionManager;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  11. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\Http\FileDisplayResponse;
  14. use OCP\Files\IRootFolder;
  15. use OCP\Files\NotFoundException;
  16. use OCP\IPreview;
  17. use OCP\IRequest;
  18. use OCP\IUserSession;
  19. class PreviewController extends Controller {
  20. /** @var IRootFolder */
  21. private $rootFolder;
  22. /** @var IUserSession */
  23. private $userSession;
  24. /** @var IVersionManager */
  25. private $versionManager;
  26. /** @var IPreview */
  27. private $previewManager;
  28. public function __construct(
  29. string $appName,
  30. IRequest $request,
  31. IRootFolder $rootFolder,
  32. IUserSession $userSession,
  33. IVersionManager $versionManager,
  34. IPreview $previewManager
  35. ) {
  36. parent::__construct($appName, $request);
  37. $this->rootFolder = $rootFolder;
  38. $this->userSession = $userSession;
  39. $this->versionManager = $versionManager;
  40. $this->previewManager = $previewManager;
  41. }
  42. /**
  43. * Get the preview for a file version
  44. *
  45. * @param string $file Path of the file
  46. * @param int $x Width of the preview
  47. * @param int $y Height of the preview
  48. * @param string $version Version of the file to get the preview for
  49. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  50. *
  51. * 200: Preview returned
  52. * 400: Getting preview is not possible
  53. * 404: Preview not found
  54. */
  55. #[NoAdminRequired]
  56. #[NoCSRFRequired]
  57. public function getPreview(
  58. string $file = '',
  59. int $x = 44,
  60. int $y = 44,
  61. string $version = ''
  62. ) {
  63. if ($file === '' || $version === '' || $x === 0 || $y === 0) {
  64. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  65. }
  66. try {
  67. $user = $this->userSession->getUser();
  68. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  69. $file = $userFolder->get($file);
  70. $versionFile = $this->versionManager->getVersionFile($user, $file, $version);
  71. $preview = $this->previewManager->getPreview($versionFile, $x, $y, true, IPreview::MODE_FILL, $versionFile->getMimetype());
  72. return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
  73. } catch (NotFoundException $e) {
  74. return new DataResponse([], Http::STATUS_NOT_FOUND);
  75. } catch (\InvalidArgumentException $e) {
  76. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  77. }
  78. }
  79. }