DirectController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Controller;
  25. use OCA\DAV\Db\Direct;
  26. use OCA\DAV\Db\DirectMapper;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\OCS\OCSBadRequestException;
  29. use OCP\AppFramework\OCS\OCSNotFoundException;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\File;
  33. use OCP\Files\IRootFolder;
  34. use OCP\IRequest;
  35. use OCP\IURLGenerator;
  36. use OCP\Security\ISecureRandom;
  37. class DirectController extends OCSController {
  38. /** @var IRootFolder */
  39. private $rootFolder;
  40. /** @var string */
  41. private $userId;
  42. /** @var DirectMapper */
  43. private $mapper;
  44. /** @var ISecureRandom */
  45. private $random;
  46. /** @var ITimeFactory */
  47. private $timeFactory;
  48. /** @var IURLGenerator */
  49. private $urlGenerator;
  50. public function __construct(string $appName,
  51. IRequest $request,
  52. IRootFolder $rootFolder,
  53. string $userId,
  54. DirectMapper $mapper,
  55. ISecureRandom $random,
  56. ITimeFactory $timeFactory,
  57. IURLGenerator $urlGenerator) {
  58. parent::__construct($appName, $request);
  59. $this->rootFolder = $rootFolder;
  60. $this->userId = $userId;
  61. $this->mapper = $mapper;
  62. $this->random = $random;
  63. $this->timeFactory = $timeFactory;
  64. $this->urlGenerator = $urlGenerator;
  65. }
  66. /**
  67. * @NoAdminRequired
  68. */
  69. public function getUrl(int $fileId): DataResponse {
  70. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  71. $files = $userFolder->getById($fileId);
  72. if ($files === []) {
  73. throw new OCSNotFoundException();
  74. }
  75. $file = array_shift($files);
  76. if (!($file instanceof File)) {
  77. throw new OCSBadRequestException('Direct download only works for files');
  78. }
  79. //TODO: at some point we should use the directdownlaod function of storages
  80. $direct = new Direct();
  81. $direct->setUserId($this->userId);
  82. $direct->setFileId($fileId);
  83. $token = $this->random->generate(60, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
  84. $direct->setToken($token);
  85. $direct->setExpiration($this->timeFactory->getTime() + 60 * 60 * 8);
  86. $this->mapper->insert($direct);
  87. $url = $this->urlGenerator->getAbsoluteURL('remote.php/direct/'.$token);
  88. return new DataResponse([
  89. 'url' => $url,
  90. ]);
  91. }
  92. }