PreviewController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @param string $file
  64. * @param int $x
  65. * @param int $y
  66. * @param string $version
  67. * @return DataResponse|FileDisplayResponse
  68. */
  69. public function getPreview(
  70. string $file = '',
  71. int $x = 44,
  72. int $y = 44,
  73. string $version = ''
  74. ) {
  75. if ($file === '' || $version === '' || $x === 0 || $y === 0) {
  76. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  77. }
  78. try {
  79. $user = $this->userSession->getUser();
  80. $userFolder = $this->rootFolder->getUserFolder($user->getUID());
  81. $file = $userFolder->get($file);
  82. $versionFile = $this->versionManager->getVersionFile($user, $file, $version);
  83. $preview = $this->previewManager->getPreview($versionFile, $x, $y, true, IPreview::MODE_FILL, $versionFile->getMimetype());
  84. return new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => $preview->getMimeType()]);
  85. } catch (NotFoundException $e) {
  86. return new DataResponse([], Http::STATUS_NOT_FOUND);
  87. } catch (\InvalidArgumentException $e) {
  88. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  89. }
  90. }
  91. }