DirectController.php 3.5 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\Attribute\NoAdminRequired;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCS\OCSBadRequestException;
  14. use OCP\AppFramework\OCS\OCSForbiddenException;
  15. use OCP\AppFramework\OCS\OCSNotFoundException;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\AppFramework\Utility\ITimeFactory;
  18. use OCP\EventDispatcher\IEventDispatcher;
  19. use OCP\Files\Events\BeforeDirectFileDownloadEvent;
  20. use OCP\Files\File;
  21. use OCP\Files\IRootFolder;
  22. use OCP\IRequest;
  23. use OCP\IURLGenerator;
  24. use OCP\Security\ISecureRandom;
  25. class DirectController extends OCSController {
  26. /** @var IRootFolder */
  27. private $rootFolder;
  28. /** @var string */
  29. private $userId;
  30. /** @var DirectMapper */
  31. private $mapper;
  32. /** @var ISecureRandom */
  33. private $random;
  34. /** @var ITimeFactory */
  35. private $timeFactory;
  36. /** @var IURLGenerator */
  37. private $urlGenerator;
  38. /** @var IEventDispatcher */
  39. private $eventDispatcher;
  40. public function __construct(string $appName,
  41. IRequest $request,
  42. IRootFolder $rootFolder,
  43. string $userId,
  44. DirectMapper $mapper,
  45. ISecureRandom $random,
  46. ITimeFactory $timeFactory,
  47. IURLGenerator $urlGenerator,
  48. IEventDispatcher $eventDispatcher) {
  49. parent::__construct($appName, $request);
  50. $this->rootFolder = $rootFolder;
  51. $this->userId = $userId;
  52. $this->mapper = $mapper;
  53. $this->random = $random;
  54. $this->timeFactory = $timeFactory;
  55. $this->urlGenerator = $urlGenerator;
  56. $this->eventDispatcher = $eventDispatcher;
  57. }
  58. /**
  59. * Get a direct link to a file
  60. *
  61. * @param int $fileId ID of the file
  62. * @param int $expirationTime Duration until the link expires
  63. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>
  64. * @throws OCSNotFoundException File not found
  65. * @throws OCSBadRequestException Getting direct link is not possible
  66. * @throws OCSForbiddenException Missing permissions to get direct link
  67. *
  68. * 200: Direct link returned
  69. */
  70. #[NoAdminRequired]
  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. }