PreviewController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Controller;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\Files\File;
  27. use OCP\AppFramework\Http;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\Http\FileDisplayResponse;
  30. use OCP\Files\IRootFolder;
  31. use OCP\Files\NotFoundException;
  32. use OCP\IPreview;
  33. use OCP\IRequest;
  34. class PreviewController extends Controller {
  35. /** @var string */
  36. private $userId;
  37. /** @var IRootFolder */
  38. private $root;
  39. /** @var IPreview */
  40. private $preview;
  41. /** @var ITimeFactory */
  42. private $timeFactory;
  43. /**
  44. * PreviewController constructor.
  45. *
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param IPreview $preview
  49. * @param IRootFolder $root
  50. * @param string $userId
  51. */
  52. public function __construct($appName,
  53. IRequest $request,
  54. IPreview $preview,
  55. IRootFolder $root,
  56. $userId,
  57. ITimeFactory $timeFactory
  58. ) {
  59. parent::__construct($appName, $request);
  60. $this->preview = $preview;
  61. $this->root = $root;
  62. $this->userId = $userId;
  63. $this->timeFactory = $timeFactory;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. * @NoCSRFRequired
  68. *
  69. * @param string $file
  70. * @param int $x
  71. * @param int $y
  72. * @param bool $a
  73. * @param bool $forceIcon
  74. * @param string $mode
  75. * @return DataResponse|Http\FileDisplayResponse
  76. */
  77. public function getPreview(
  78. $file = '',
  79. $x = 32,
  80. $y = 32,
  81. $a = false,
  82. $forceIcon = true,
  83. $mode = 'fill') {
  84. if ($file === '' || $x === 0 || $y === 0) {
  85. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  86. }
  87. try {
  88. $userFolder = $this->root->getUserFolder($this->userId);
  89. $file = $userFolder->get($file);
  90. } catch (NotFoundException $e) {
  91. return new DataResponse([], Http::STATUS_NOT_FOUND);
  92. }
  93. if (!($file instanceof File) || (!$forceIcon && !$this->preview->isAvailable($file))) {
  94. return new DataResponse([], Http::STATUS_NOT_FOUND);
  95. } else if (!$file->isReadable()) {
  96. return new DataResponse([], Http::STATUS_FORBIDDEN);
  97. }
  98. try {
  99. $f = $this->preview->getPreview($file, $x, $y, !$a, $mode);
  100. $response = new FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  101. // Let cache this!
  102. $response->addHeader('Pragma', 'public');
  103. // Cache previews for 24H
  104. $response->cacheFor(3600 * 24);
  105. $expires = new \DateTime();
  106. $expires->setTimestamp($this->timeFactory->getTime());
  107. $expires->add(new \DateInterval('P1D'));
  108. $response->addHeader('Expires', $expires->format(\DateTime::RFC2822));
  109. return $response;
  110. } catch (NotFoundException $e) {
  111. return new DataResponse([], Http::STATUS_NOT_FOUND);
  112. }
  113. }
  114. }