UploadHome.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. public function __construct(
  15. private array $principalInfo,
  16. private CleanupService $cleanupService,
  17. ) {
  18. }
  19. public function createFile($name, $data = null) {
  20. throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
  21. }
  22. public function createDirectory($name) {
  23. $this->impl()->createDirectory($name);
  24. // Add a cleanup job
  25. $this->cleanupService->addJob($name);
  26. }
  27. public function getChild($name): UploadFolder {
  28. return new UploadFolder($this->impl()->getChild($name), $this->cleanupService, $this->getStorage());
  29. }
  30. public function getChildren(): array {
  31. return array_map(function ($node) {
  32. return new UploadFolder($node, $this->cleanupService, $this->getStorage());
  33. }, $this->impl()->getChildren());
  34. }
  35. public function childExists($name): bool {
  36. return !is_null($this->getChild($name));
  37. }
  38. public function delete() {
  39. $this->impl()->delete();
  40. }
  41. public function getName() {
  42. [,$name] = \Sabre\Uri\split($this->principalInfo['uri']);
  43. return $name;
  44. }
  45. public function setName($name) {
  46. throw new Forbidden('Permission denied to rename this folder');
  47. }
  48. public function getLastModified() {
  49. return $this->impl()->getLastModified();
  50. }
  51. /**
  52. * @return Directory
  53. */
  54. private function impl() {
  55. $view = $this->getView();
  56. $rootInfo = $view->getFileInfo('');
  57. return new Directory($view, $rootInfo);
  58. }
  59. private function getView() {
  60. $rootView = new View();
  61. $user = \OC::$server->getUserSession()->getUser();
  62. Filesystem::initMountPoints($user->getUID());
  63. if (!$rootView->file_exists('/' . $user->getUID() . '/uploads')) {
  64. $rootView->mkdir('/' . $user->getUID() . '/uploads');
  65. }
  66. return new View('/' . $user->getUID() . '/uploads');
  67. }
  68. private function getStorage() {
  69. $view = $this->getView();
  70. $storage = $view->getFileInfo('')->getStorage();
  71. return $storage;
  72. }
  73. }