1
0

DirectHome.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  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\Direct;
  26. use OCA\DAV\Db\DirectMapper;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\Files\IRootFolder;
  31. use OCP\IRequest;
  32. use OCP\Security\Bruteforce\IThrottler;
  33. use Sabre\DAV\Exception\Forbidden;
  34. use Sabre\DAV\Exception\MethodNotAllowed;
  35. use Sabre\DAV\Exception\NotFound;
  36. use Sabre\DAV\ICollection;
  37. class DirectHome implements ICollection {
  38. /** @var IRootFolder */
  39. private $rootFolder;
  40. /** @var DirectMapper */
  41. private $mapper;
  42. /** @var ITimeFactory */
  43. private $timeFactory;
  44. /** @var IThrottler */
  45. private $throttler;
  46. /** @var IRequest */
  47. private $request;
  48. /** @var IEventDispatcher */
  49. private $eventDispatcher;
  50. public function __construct(
  51. IRootFolder $rootFolder,
  52. DirectMapper $mapper,
  53. ITimeFactory $timeFactory,
  54. IThrottler $throttler,
  55. IRequest $request,
  56. IEventDispatcher $eventDispatcher
  57. ) {
  58. $this->rootFolder = $rootFolder;
  59. $this->mapper = $mapper;
  60. $this->timeFactory = $timeFactory;
  61. $this->throttler = $throttler;
  62. $this->request = $request;
  63. $this->eventDispatcher = $eventDispatcher;
  64. }
  65. public function createFile($name, $data = null) {
  66. throw new Forbidden();
  67. }
  68. public function createDirectory($name) {
  69. throw new Forbidden();
  70. }
  71. public function getChild($name): DirectFile {
  72. try {
  73. $direct = $this->mapper->getByToken($name);
  74. // Expired
  75. if ($direct->getExpiration() < $this->timeFactory->getTime()) {
  76. throw new NotFound();
  77. }
  78. return new DirectFile($direct, $this->rootFolder, $this->eventDispatcher);
  79. } catch (DoesNotExistException $e) {
  80. // Since the token space is so huge only throttle on non-existing token
  81. $this->throttler->registerAttempt('directlink', $this->request->getRemoteAddress());
  82. $this->throttler->sleepDelay($this->request->getRemoteAddress(), 'directlink');
  83. throw new NotFound();
  84. }
  85. }
  86. public function getChildren() {
  87. throw new MethodNotAllowed('Listing members of this collection is disabled');
  88. }
  89. public function childExists($name): bool {
  90. return false;
  91. }
  92. public function delete() {
  93. throw new Forbidden();
  94. }
  95. public function getName(): string {
  96. return 'direct';
  97. }
  98. public function setName($name) {
  99. throw new Forbidden();
  100. }
  101. public function getLastModified(): int {
  102. return 0;
  103. }
  104. }