PreviewController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 OCA\Files_Trashbin\Controller;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\Files\File;
  28. use OCP\Files\Folder;
  29. use OCP\Files\IMimeTypeDetector;
  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 IRootFolder */
  36. private $rootFolder;
  37. /** @var string */
  38. private $userId;
  39. /** @var IMimeTypeDetector */
  40. private $mimeTypeDetector;
  41. /** @var IPreview */
  42. private $previewManager;
  43. /**
  44. * @param string $appName
  45. * @param IRequest $request
  46. * @param IRootFolder $rootFolder
  47. * @param $userId
  48. * @param IMimeTypeDetector $mimeTypeDetector
  49. * @param IPreview $previewManager
  50. */
  51. public function __construct($appName,
  52. IRequest $request,
  53. IRootFolder $rootFolder,
  54. $userId,
  55. IMimeTypeDetector $mimeTypeDetector,
  56. IPreview $previewManager) {
  57. parent::__construct($appName, $request);
  58. $this->rootFolder = $rootFolder;
  59. $this->userId = $userId;
  60. $this->mimeTypeDetector = $mimeTypeDetector;
  61. $this->previewManager = $previewManager;
  62. }
  63. /**
  64. * @NoAdminRequired
  65. * @NoCSRFRequired
  66. *
  67. * @param string $file
  68. * @param int $x
  69. * @param int $y
  70. * @return DataResponse|Http\FileDisplayResponse
  71. */
  72. public function getPreview(
  73. $file = '',
  74. $x = 44,
  75. $y = 44
  76. ) {
  77. if ($file === '') {
  78. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  79. }
  80. if ($x === 0 || $y === 0) {
  81. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  82. }
  83. try {
  84. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  85. /** @var Folder $trash */
  86. $trash = $userFolder->getParent()->get('files_trashbin/files');
  87. $trashFile = $trash->get($file);
  88. if ($trashFile instanceof Folder) {
  89. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  90. }
  91. /** @var File $trashFile */
  92. $fileName = $trashFile->getName();
  93. $i = strrpos($fileName, '.');
  94. if ($i !== false) {
  95. $fileName = substr($fileName, 0, $i);
  96. }
  97. $mimeType = $this->mimeTypeDetector->detectPath($fileName);
  98. $f = $this->previewManager->getPreview($trashFile, $x, $y, true, IPreview::MODE_FILL, $mimeType);
  99. return new Http\FileDisplayResponse($f, Http::STATUS_OK, ['Content-Type' => $f->getMimeType()]);
  100. } catch (NotFoundException $e) {
  101. return new DataResponse([], Http::STATUS_NOT_FOUND);
  102. }
  103. }
  104. }