1
0

DirectHome.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Direct;
  8. use OCA\DAV\Db\DirectMapper;
  9. use OCP\AppFramework\Db\DoesNotExistException;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Files\IRootFolder;
  13. use OCP\IRequest;
  14. use OCP\Security\Bruteforce\IThrottler;
  15. use Sabre\DAV\Exception\Forbidden;
  16. use Sabre\DAV\Exception\MethodNotAllowed;
  17. use Sabre\DAV\Exception\NotFound;
  18. use Sabre\DAV\ICollection;
  19. class DirectHome implements ICollection {
  20. /** @var IRootFolder */
  21. private $rootFolder;
  22. /** @var DirectMapper */
  23. private $mapper;
  24. /** @var ITimeFactory */
  25. private $timeFactory;
  26. /** @var IThrottler */
  27. private $throttler;
  28. /** @var IRequest */
  29. private $request;
  30. /** @var IEventDispatcher */
  31. private $eventDispatcher;
  32. public function __construct(
  33. IRootFolder $rootFolder,
  34. DirectMapper $mapper,
  35. ITimeFactory $timeFactory,
  36. IThrottler $throttler,
  37. IRequest $request,
  38. IEventDispatcher $eventDispatcher
  39. ) {
  40. $this->rootFolder = $rootFolder;
  41. $this->mapper = $mapper;
  42. $this->timeFactory = $timeFactory;
  43. $this->throttler = $throttler;
  44. $this->request = $request;
  45. $this->eventDispatcher = $eventDispatcher;
  46. }
  47. public function createFile($name, $data = null) {
  48. throw new Forbidden();
  49. }
  50. public function createDirectory($name) {
  51. throw new Forbidden();
  52. }
  53. public function getChild($name): DirectFile {
  54. try {
  55. $direct = $this->mapper->getByToken($name);
  56. // Expired
  57. if ($direct->getExpiration() < $this->timeFactory->getTime()) {
  58. throw new NotFound();
  59. }
  60. return new DirectFile($direct, $this->rootFolder, $this->eventDispatcher);
  61. } catch (DoesNotExistException $e) {
  62. // Since the token space is so huge only throttle on non-existing token
  63. $this->throttler->registerAttempt('directlink', $this->request->getRemoteAddress());
  64. $this->throttler->sleepDelay($this->request->getRemoteAddress(), 'directlink');
  65. throw new NotFound();
  66. }
  67. }
  68. public function getChildren() {
  69. throw new MethodNotAllowed('Listing members of this collection is disabled');
  70. }
  71. public function childExists($name): bool {
  72. return false;
  73. }
  74. public function delete() {
  75. throw new Forbidden();
  76. }
  77. public function getName(): string {
  78. return 'direct';
  79. }
  80. public function setName($name) {
  81. throw new Forbidden();
  82. }
  83. public function getLastModified(): int {
  84. return 0;
  85. }
  86. }