You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.6 KiB

  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 OC\Core\Controller;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\Files\File;
  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. class PreviewController extends Controller {
  36. /** @var string */
  37. private $userId;
  38. /** @var IRootFolder */
  39. private $root;
  40. /** @var IPreview */
  41. private $preview;
  42. /** @var ITimeFactory */
  43. private $timeFactory;
  44. /**
  45. * PreviewController constructor.
  46. *
  47. * @param string $appName
  48. * @param IRequest $request
  49. * @param IPreview $preview
  50. * @param IRootFolder $root
  51. * @param string $userId
  52. */
  53. public function __construct($appName,
  54. IRequest $request,
  55. IPreview $preview,
  56. IRootFolder $root,
  57. $userId,
  58. ITimeFactory $timeFactory
  59. ) {
  60. parent::__construct($appName, $request);
  61. $this->preview = $preview;
  62. $this->root = $root;
  63. $this->userId = $userId;
  64. $this->timeFactory = $timeFactory;
  65. }
  66. /**
  67. * @NoAdminRequired
  68. * @NoCSRFRequired
  69. *
  70. * @param string $file
  71. * @param int $x
  72. * @param int $y
  73. * @param bool $a
  74. * @param bool $forceIcon
  75. * @param string $mode
  76. * @return DataResponse|Http\FileDisplayResponse
  77. */
  78. public function getPreview(
  79. $file = '',
  80. $x = 32,
  81. $y = 32,
  82. $a = false,
  83. $forceIcon = true,
  84. $mode = 'fill') {
  85. if ($file === '' || $x === 0 || $y === 0) {
  86. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  87. }
  88. try {
  89. $userFolder = $this->root->getUserFolder($this->userId);
  90. $file = $userFolder->get($file);
  91. } catch (NotFoundException $e) {
  92. return new DataResponse([], Http::STATUS_NOT_FOUND);
  93. }
  94. if (!($file instanceof File) || (!$forceIcon && !$this->preview->isAvailable($file))) {
  95. return new DataResponse([], Http::STATUS_NOT_FOUND);
  96. } else if (!$file->isReadable()) {
  97. return new DataResponse([], Http::STATUS_FORBIDDEN);
  98. }
  99. try {
  100. $f = $this->preview->getPreview($file, $x, $y, !$a, $mode);
  101. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  102. // Let cache this!
  103. $response->addHeader('Pragma', 'public');
  104. // Cache previews for 24H
  105. $response->cacheFor(3600 * 24);
  106. $expires = new \DateTime();
  107. $expires->setTimestamp($this->timeFactory->getTime());
  108. $expires->add(new \DateInterval('P1D'));
  109. $response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
  110. return $response;
  111. } catch (NotFoundException $e) {
  112. return new DataResponse([], Http::STATUS_NOT_FOUND);
  113. } catch (\InvalidArgumentException $e) {
  114. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  115. }
  116. }
  117. }