You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirectController.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\OCSController;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\Files\File;
  34. use OCP\Files\IRootFolder;
  35. use OCP\IRequest;
  36. use OCP\IURLGenerator;
  37. use OCP\Security\ISecureRandom;
  38. class DirectController extends OCSController {
  39. /** @var IRootFolder */
  40. private $rootFolder;
  41. /** @var string */
  42. private $userId;
  43. /** @var DirectMapper */
  44. private $mapper;
  45. /** @var ISecureRandom */
  46. private $random;
  47. /** @var ITimeFactory */
  48. private $timeFactory;
  49. /** @var IURLGenerator */
  50. private $urlGenerator;
  51. public function __construct(string $appName,
  52. IRequest $request,
  53. IRootFolder $rootFolder,
  54. string $userId,
  55. DirectMapper $mapper,
  56. ISecureRandom $random,
  57. ITimeFactory $timeFactory,
  58. IURLGenerator $urlGenerator) {
  59. parent::__construct($appName, $request);
  60. $this->rootFolder = $rootFolder;
  61. $this->userId = $userId;
  62. $this->mapper = $mapper;
  63. $this->random = $random;
  64. $this->timeFactory = $timeFactory;
  65. $this->urlGenerator = $urlGenerator;
  66. }
  67. /**
  68. * @NoAdminRequired
  69. */
  70. public function getUrl(int $fileId, int $expirationTime = 60 * 60 * 8): DataResponse {
  71. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  72. $files = $userFolder->getById($fileId);
  73. if ($files === []) {
  74. throw new OCSNotFoundException();
  75. }
  76. if ($expirationTime <= 0 || $expirationTime > (60 * 60 * 24)) {
  77. throw new OCSBadRequestException('Expiration time should be greater than 0 and less than or equal to ' . (60 * 60 * 24));
  78. }
  79. $file = array_shift($files);
  80. if (!($file instanceof File)) {
  81. throw new OCSBadRequestException('Direct download only works for files');
  82. }
  83. //TODO: at some point we should use the directdownlaod function of storages
  84. $direct = new Direct();
  85. $direct->setUserId($this->userId);
  86. $direct->setFileId($fileId);
  87. $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
  88. $direct->setToken($token);
  89. $direct->setExpiration($this->timeFactory->getTime() + $expirationTime);
  90. $this->mapper->insert($direct);
  91. $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token);
  92. return new DataResponse([
  93. 'url' => $url,
  94. ]);
  95. }
  96. }