PreviewController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Jakub Onderka <ahoj@jakubonderka.cz>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author simonspa <1677436+simonspa@users.noreply.github.com>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Files_Trashbin\Controller;
  29. use OCA\Files_Trashbin\Trash\ITrashManager;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\Files\Folder;
  35. use OCP\Files\IMimeTypeDetector;
  36. use OCP\Files\IRootFolder;
  37. use OCP\Files\NotFoundException;
  38. use OCP\IPreview;
  39. use OCP\IRequest;
  40. use OCP\IUserSession;
  41. class PreviewController extends Controller {
  42. /** @var IRootFolder */
  43. private $rootFolder;
  44. /** @var ITrashManager */
  45. private $trashManager;
  46. /** @var IUserSession */
  47. private $userSession;
  48. /** @var IMimeTypeDetector */
  49. private $mimeTypeDetector;
  50. /** @var IPreview */
  51. private $previewManager;
  52. /** @var ITimeFactory */
  53. private $time;
  54. public function __construct(
  55. string $appName,
  56. IRequest $request,
  57. IRootFolder $rootFolder,
  58. ITrashManager $trashManager,
  59. IUserSession $userSession,
  60. IMimeTypeDetector $mimeTypeDetector,
  61. IPreview $previewManager,
  62. ITimeFactory $time
  63. ) {
  64. parent::__construct($appName, $request);
  65. $this->trashManager = $trashManager;
  66. $this->rootFolder = $rootFolder;
  67. $this->userSession = $userSession;
  68. $this->mimeTypeDetector = $mimeTypeDetector;
  69. $this->previewManager = $previewManager;
  70. $this->time = $time;
  71. }
  72. /**
  73. * @NoAdminRequired
  74. * @NoCSRFRequired
  75. *
  76. * @return DataResponse|Http\FileDisplayResponse
  77. */
  78. public function getPreview(
  79. int $fileId = -1,
  80. int $x = 128,
  81. int $y = 128
  82. ) {
  83. if ($fileId === -1 || $x === 0 || $y === 0) {
  84. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  85. }
  86. try {
  87. $file = $this->trashManager->getTrashNodeById($this->userSession->getUser(), $fileId);
  88. if ($file === null) {
  89. return new DataResponse([], Http::STATUS_NOT_FOUND);
  90. }
  91. if ($file instanceof Folder) {
  92. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  93. }
  94. $pathParts = pathinfo($file->getName());
  95. $extension = $pathParts['extension'] ?? '';
  96. $fileName = $pathParts['filename'];
  97. /*
  98. * Files in the root of the trashbin are timetamped.
  99. * So we have to strip that in order to properly detect the mimetype of the file.
  100. */
  101. if (preg_match('/d\d+/', $extension)) {
  102. $mimeType = $this->mimeTypeDetector->detectPath($fileName);
  103. } else {
  104. $mimeType = $this->mimeTypeDetector->detectPath($file->getName());
  105. }
  106. $f = $this->previewManager->getPreview($file, $x, $y, true, IPreview::MODE_FILL, $mimeType);
  107. $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  108. // Cache previews for 24H
  109. $response->cacheFor(3600 * 24);
  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. }