DirectController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Iscle <albertiscle9@gmail.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Kate Döen <kate.doeen@nextcloud.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\Controller;
  27. use OCA\DAV\Db\Direct;
  28. use OCA\DAV\Db\DirectMapper;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSBadRequestException;
  32. use OCP\AppFramework\OCS\OCSNotFoundException;
  33. use OCP\AppFramework\OCS\OCSForbiddenException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\AppFramework\Utility\ITimeFactory;
  36. use OCP\EventDispatcher\GenericEvent;
  37. use OCP\EventDispatcher\IEventDispatcher;
  38. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  39. use OCP\Files\File;
  40. use OCP\Files\IRootFolder;
  41. use OCP\IRequest;
  42. use OCP\IURLGenerator;
  43. use OCP\Security\ISecureRandom;
  44. class DirectController extends OCSController {
  45. /** @var IRootFolder */
  46. private $rootFolder;
  47. /** @var string */
  48. private $userId;
  49. /** @var DirectMapper */
  50. private $mapper;
  51. /** @var ISecureRandom */
  52. private $random;
  53. /** @var ITimeFactory */
  54. private $timeFactory;
  55. /** @var IURLGenerator */
  56. private $urlGenerator;
  57. /** @var IEventDispatcher */
  58. private $eventDispatcher;
  59. public function __construct(string $appName,
  60. IRequest $request,
  61. IRootFolder $rootFolder,
  62. string $userId,
  63. DirectMapper $mapper,
  64. ISecureRandom $random,
  65. ITimeFactory $timeFactory,
  66. IURLGenerator $urlGenerator,
  67. IEventDispatcher $eventDispatcher) {
  68. parent::__construct($appName, $request);
  69. $this->rootFolder = $rootFolder;
  70. $this->userId = $userId;
  71. $this->mapper = $mapper;
  72. $this->random = $random;
  73. $this->timeFactory = $timeFactory;
  74. $this->urlGenerator = $urlGenerator;
  75. $this->eventDispatcher = $eventDispatcher;
  76. }
  77. /**
  78. * @NoAdminRequired
  79. *
  80. * Get a direct link to a file
  81. *
  82. * @param int $fileId ID of the file
  83. * @param int $expirationTime Duration until the link expires
  84. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>
  85. * @throws OCSNotFoundException File not found
  86. * @throws OCSBadRequestException Getting direct link is not possible
  87. * @throws OCSForbiddenException Missing permissions to get direct link
  88. *
  89. * 200: Direct link returned
  90. */
  91. public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
  92. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  93. $files = $userFolder->getById($fileId);
  94. if ($files === []) {
  95. throw new OCSNotFoundException();
  96. }
  97. if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
  98. throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
  99. }
  100. $file = array_shift($files);
  101. if (!($file instanceof File)) {
  102. throw new OCSBadRequestException('Direct download only works for files');
  103. }
  104. $event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath()));
  105. $this->eventDispatcher->dispatchTyped($event);
  106. if ($event->isSuccessful() === false) {
  107. throw new OCSForbiddenException('Permission denied to download file');
  108. }
  109. //TODO: at some point we should use the directdownlaod function of storages
  110. $direct = new Direct();
  111. $direct->setUserId($this->userId);
  112. $direct->setFileId($fileId);
  113. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  114. $direct->setToken($token);
  115. $direct->setExpiration($this->timeFactory->getTime() + $expirationTime);
  116. $this->mapper->insert($direct);
  117. $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token);
  118. return new DataResponse([
  119. 'url' => $url,
  120. ]);
  121. }
  122. }