DirectController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\OCSForbiddenException;
  33. use OCP\AppFramework\OCS\OCSNotFoundException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\AppFramework\Utility\ITimeFactory;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  38. use OCP\Files\File;
  39. use OCP\Files\IRootFolder;
  40. use OCP\IRequest;
  41. use OCP\IURLGenerator;
  42. use OCP\Security\ISecureRandom;
  43. class DirectController extends OCSController {
  44. /** @var IRootFolder */
  45. private $rootFolder;
  46. /** @var string */
  47. private $userId;
  48. /** @var DirectMapper */
  49. private $mapper;
  50. /** @var ISecureRandom */
  51. private $random;
  52. /** @var ITimeFactory */
  53. private $timeFactory;
  54. /** @var IURLGenerator */
  55. private $urlGenerator;
  56. /** @var IEventDispatcher */
  57. private $eventDispatcher;
  58. public function __construct(string $appName,
  59. IRequest $request,
  60. IRootFolder $rootFolder,
  61. string $userId,
  62. DirectMapper $mapper,
  63. ISecureRandom $random,
  64. ITimeFactory $timeFactory,
  65. IURLGenerator $urlGenerator,
  66. IEventDispatcher $eventDispatcher) {
  67. parent::__construct($appName, $request);
  68. $this->rootFolder = $rootFolder;
  69. $this->userId = $userId;
  70. $this->mapper = $mapper;
  71. $this->random = $random;
  72. $this->timeFactory = $timeFactory;
  73. $this->urlGenerator = $urlGenerator;
  74. $this->eventDispatcher = $eventDispatcher;
  75. }
  76. /**
  77. * @NoAdminRequired
  78. *
  79. * Get a direct link to a file
  80. *
  81. * @param int $fileId ID of the file
  82. * @param int $expirationTime Duration until the link expires
  83. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>
  84. * @throws OCSNotFoundException File not found
  85. * @throws OCSBadRequestException Getting direct link is not possible
  86. * @throws OCSForbiddenException Missing permissions to get direct link
  87. *
  88. * 200: Direct link returned
  89. */
  90. public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
  91. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  92. $file = $userFolder->getFirstNodeById($fileId);
  93. if (!$file) {
  94. throw new OCSNotFoundException();
  95. }
  96. if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
  97. throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
  98. }
  99. if (!($file instanceof File)) {
  100. throw new OCSBadRequestException('Direct download only works for files');
  101. }
  102. $event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath()));
  103. $this->eventDispatcher->dispatchTyped($event);
  104. if ($event->isSuccessful() === false) {
  105. throw new OCSForbiddenException('Permission denied to download file');
  106. }
  107. //TODO: at some point we should use the directdownlaod function of storages
  108. $direct = new Direct();
  109. $direct->setUserId($this->userId);
  110. $direct->setFileId($fileId);
  111. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  112. $direct->setToken($token);
  113. $direct->setExpiration($this->timeFactory->getTime() + $expirationTime);
  114. $this->mapper->insert($direct);
  115. $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token);
  116. return new DataResponse([
  117. 'url' => $url,
  118. ]);
  119. }
  120. }