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 = 32,
  81. int $y = 32,
  82. bool $a = false,
  83. ) {
  84. if ($fileId === -1 || $x === 0 || $y === 0) {
  85. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  86. }
  87. try {
  88. $file = $this->trashManager->getTrashNodeById($this->userSession->getUser(), $fileId);
  89. if ($file === null) {
  90. return new DataResponse([], Http::STATUS_NOT_FOUND);
  91. }
  92. if ($file instanceof Folder) {
  93. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  94. }
  95. $pathParts = pathinfo($file->getName());
  96. $extension = $pathParts['extension'] ?? '';
  97. $fileName = $pathParts['filename'];
  98. /*
  99. * Files in the root of the trashbin are timetamped.
  100. * So we have to strip that in order to properly detect the mimetype of the file.
  101. */
  102. if (preg_match('/d\d+/', $extension)) {
  103. $mimeType = $this->mimeTypeDetector->detectPath($fileName);
  104. } else {
  105. $mimeType = $this->mimeTypeDetector->detectPath($file->getName());
  106. }
  107. $f = $this->previewManager->getPreview($file, $x, $y, $a, IPreview::MODE_FILL, $mimeType);
  108. $response = new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  109. // Cache previews for 24H
  110. $response->cacheFor(3600 * 24);
  111. return $response;
  112. } catch (NotFoundException $e) {
  113. return new DataResponse([], Http::STATUS_NOT_FOUND);
  114. } catch (\InvalidArgumentException $e) {
  115. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  116. }
  117. }
  118. }