DirectController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Controller;
  8. use OCA\DAV\Db\Direct;
  9. use OCA\DAV\Db\DirectMapper;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\OCS\OCSBadRequestException;
  13. use OCP\AppFramework\OCS\OCSForbiddenException;
  14. use OCP\AppFramework\OCS\OCSNotFoundException;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\AppFramework\Utility\ITimeFactory;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  19. use OCP\Files\File;
  20. use OCP\Files\IRootFolder;
  21. use OCP\IRequest;
  22. use OCP\IURLGenerator;
  23. use OCP\Security\ISecureRandom;
  24. class DirectController extends OCSController {
  25. /** @var IRootFolder */
  26. private $rootFolder;
  27. /** @var string */
  28. private $userId;
  29. /** @var DirectMapper */
  30. private $mapper;
  31. /** @var ISecureRandom */
  32. private $random;
  33. /** @var ITimeFactory */
  34. private $timeFactory;
  35. /** @var IURLGenerator */
  36. private $urlGenerator;
  37. /** @var IEventDispatcher */
  38. private $eventDispatcher;
  39. public function __construct(string $appName,
  40. IRequest $request,
  41. IRootFolder $rootFolder,
  42. string $userId,
  43. DirectMapper $mapper,
  44. ISecureRandom $random,
  45. ITimeFactory $timeFactory,
  46. IURLGenerator $urlGenerator,
  47. IEventDispatcher $eventDispatcher) {
  48. parent::__construct($appName, $request);
  49. $this->rootFolder = $rootFolder;
  50. $this->userId = $userId;
  51. $this->mapper = $mapper;
  52. $this->random = $random;
  53. $this->timeFactory = $timeFactory;
  54. $this->urlGenerator = $urlGenerator;
  55. $this->eventDispatcher = $eventDispatcher;
  56. }
  57. /**
  58. * @NoAdminRequired
  59. *
  60. * Get a direct link to a file
  61. *
  62. * @param int $fileId ID of the file
  63. * @param int $expirationTime Duration until the link expires
  64. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>
  65. * @throws OCSNotFoundException File not found
  66. * @throws OCSBadRequestException Getting direct link is not possible
  67. * @throws OCSForbiddenException Missing permissions to get direct link
  68. *
  69. * 200: Direct link returned
  70. */
  71. public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
  72. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  73. $file = $userFolder->getFirstNodeById($fileId);
  74. if (!$file) {
  75. throw new OCSNotFoundException();
  76. }
  77. if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
  78. throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
  79. }
  80. if (!($file instanceof File)) {
  81. throw new OCSBadRequestException('Direct download only works for files');
  82. }
  83. $event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath()));
  84. $this->eventDispatcher->dispatchTyped($event);
  85. if ($event->isSuccessful() === false) {
  86. throw new OCSForbiddenException('Permission denied to download file');
  87. }
  88. //TODO: at some point we should use the directdownlaod function of storages
  89. $direct = new Direct();
  90. $direct->setUserId($this->userId);
  91. $direct->setFileId($fileId);
  92. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  93. $direct->setToken($token);
  94. $direct->setExpiration($this->timeFactory->getTime() + $expirationTime);
  95. $this->mapper->insert($direct);
  96. $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token);
  97. return new DataResponse([
  98. 'url' => $url,
  99. ]);
  100. }
  101. }