DirectController.php 3.8 KB

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