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