DirectController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. public function __construct(
  27. string $appName,
  28. IRequest $request,
  29. private IRootFolder $rootFolder,
  30. private string $userId,
  31. private DirectMapper $mapper,
  32. private ISecureRandom $random,
  33. private ITimeFactory $timeFactory,
  34. private IURLGenerator $urlGenerator,
  35. private IEventDispatcher $eventDispatcher,
  36. ) {
  37. parent::__construct($appName, $request);
  38. }
  39. /**
  40. * Get a direct link to a file
  41. *
  42. * @param int $fileId ID of the file
  43. * @param int $expirationTime Duration until the link expires
  44. * @return DataResponse<Http::STATUS_OK, array{url: string}, array{}>
  45. * @throws OCSNotFoundException File not found
  46. * @throws OCSBadRequestException Getting direct link is not possible
  47. * @throws OCSForbiddenException Missing permissions to get direct link
  48. *
  49. * 200: Direct link returned
  50. */
  51. #[NoAdminRequired]
  52. public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
  53. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  54. $file = $userFolder->getFirstNodeById($fileId);
  55. if (!$file) {
  56. throw new OCSNotFoundException();
  57. }
  58. if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
  59. throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
  60. }
  61. if (!($file instanceof File)) {
  62. throw new OCSBadRequestException('Direct download only works for files');
  63. }
  64. $event = new BeforeDirectFileDownloadEvent($userFolder->getRelativePath($file->getPath()));
  65. $this->eventDispatcher->dispatchTyped($event);
  66. if ($event->isSuccessful() === false) {
  67. throw new OCSForbiddenException('Permission denied to download file');
  68. }
  69. //TODO: at some point we should use the directdownlaod function of storages
  70. $direct = new Direct();
  71. $direct->setUserId($this->userId);
  72. $direct->setFileId($fileId);
  73. $token = $this->random->generate(60, ISecureRandom::CHAR_ALPHANUMERIC);
  74. $direct->setToken($token);
  75. $direct->setExpiration($this->timeFactory->getTime() + $expirationTime);
  76. $this->mapper->insert($direct);
  77. $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/' . $token);
  78. return new DataResponse([
  79. 'url' => $url,
  80. ]);
  81. }
  82. }