PreviewController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Versions\Controller;
  26. use OCA\Files_Versions\Versions\IVersionManager;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\FileDisplayResponse;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\IPreview;
  34. use OCP\IRequest;
  35. use OCP\IUserSession;
  36. class PreviewController extends Controller {
  37. /** @var IRootFolder */
  38. private $rootFolder;
  39. /** @var IUserSession */
  40. private $userSession;
  41. /** @var IVersionManager */
  42. private $versionManager;
  43. /** @var IPreview */
  44. private $previewManager;
  45. public function __construct(
  46. string $appName,
  47. IRequest $request,
  48. IRootFolder $rootFolder,
  49. IUserSession $userSession,
  50. IVersionManager $versionManager,
  51. IPreview $previewManager
  52. ) {
  53. parent::__construct($appName, $request);
  54. $this->rootFolder = $rootFolder;
  55. $this->userSession = $userSession;
  56. $this->versionManager = $versionManager;
  57. $this->previewManager = $previewManager;
  58. }
  59. /**
  60. * @NoAdminRequired
  61. * @NoCSRFRequired
  62. *
  63. * Get the preview for a file version
  64. *
  65. * @param string $file Path of the file
  66. * @param int $x Width of the preview
  67. * @param int $y Height of the preview
  68. * @param string $version Version of the file to get the preview for
  69. * @return FileDisplayResponse<Http::STATUS_OK, array{Content-Type: string}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND, array<empty>, array{}>
  70. *
  71. * 200: Preview returned
  72. * 400: Getting preview is not possible
  73. * 404: Preview not found
  74. */
  75. public function getPreview(
  76. string $file = '',
  77. int $x = 44,
  78. int $y = 44,
  79. string $version = ''
  80. ) {
  81. if ($file === '' || $version === '' || $x === 0 || $y === 0) {
  82. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  83. }
  84. try {
  85. $user = $this->userSession->getUser();
  86. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  87. $file = $userFolder->get($file);
  88. $versionFile = $this->versionManager->getVersionFile($user, $file, $version);
  89. $preview = $this->previewManager->getPreview($versionFile, $x, $y, true, IPreview::MODE_FILL, $versionFile->getMimetype());
  90. return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
  91. } catch (NotFoundException $e) {
  92. return new DataResponse([], Http::STATUS_NOT_FOUND);
  93. } catch (\InvalidArgumentException $e) {
  94. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  95. }
  96. }
  97. }