PreviewController.php 2.5 KB

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