1
0

UploadHome.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Upload;
  8. use OC\Files\Filesystem;
  9. use OC\Files\View;
  10. use OCA\DAV\Connector\Sabre\Directory;
  11. use Sabre\DAV\Exception\Forbidden;
  12. use Sabre\DAV\ICollection;
  13. class UploadHome implements ICollection {
  14. /** @var array */
  15. private $principalInfo;
  16. /** @var CleanupService */
  17. private $cleanupService;
  18. public function __construct(array $principalInfo, CleanupService $cleanupService) {
  19. $this->principalInfo = $principalInfo;
  20. $this->cleanupService = $cleanupService;
  21. }
  22. public function createFile($name, $data = null) {
  23. throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
  24. }
  25. public function createDirectory($name) {
  26. $this->impl()->createDirectory($name);
  27. // Add a cleanup job
  28. $this->cleanupService->addJob($name);
  29. }
  30. public function getChild($name): UploadFolder {
  31. return new UploadFolder($this->impl()->getChild($name), $this->cleanupService, $this->getStorage());
  32. }
  33. public function getChildren(): array {
  34. return array_map(function ($node) {
  35. return new UploadFolder($node, $this->cleanupService, $this->getStorage());
  36. }, $this->impl()->getChildren());
  37. }
  38. public function childExists($name): bool {
  39. return !is_null($this->getChild($name));
  40. }
  41. public function delete() {
  42. $this->impl()->delete();
  43. }
  44. public function getName() {
  45. [,$name] = \Sabre\Uri\split($this->principalInfo['uri']);
  46. return $name;
  47. }
  48. public function setName($name) {
  49. throw new Forbidden('Permission denied to rename this folder');
  50. }
  51. public function getLastModified() {
  52. return $this->impl()->getLastModified();
  53. }
  54. /**
  55. * @return Directory
  56. */
  57. private function impl() {
  58. $view = $this->getView();
  59. $rootInfo = $view->getFileInfo('');
  60. return new Directory($view, $rootInfo);
  61. }
  62. private function getView() {
  63. $rootView = new View();
  64. $user = \OC::$server->getUserSession()->getUser();
  65. Filesystem::initMountPoints($user->getUID());
  66. if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
  67. $rootView->mkdir('/' . $user->getUID() . '/uploads');
  68. }
  69. return new View('/' . $user->getUID() . '/uploads');
  70. }
  71. private function getStorage() {
  72. $view = $this->getView();
  73. $storage = $view->getFileInfo('')->getStorage();
  74. return $storage;
  75. }
  76. }