UploadFolder.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\ObjectStore\ObjectStoreStorage;
  9. use OCA\DAV\Connector\Sabre\Directory;
  10. use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
  11. use OCP\Files\Storage\IStorage;
  12. use Sabre\DAV\Exception\Forbidden;
  13. use Sabre\DAV\ICollection;
  14. class UploadFolder implements ICollection {
  15. public function __construct(
  16. private Directory $node,
  17. private CleanupService $cleanupService,
  18. private IStorage $storage,
  19. ) {
  20. }
  21. public function createFile($name, $data = null) {
  22. // TODO: verify name - should be a simple number
  23. try {
  24. $this->node->createFile($name, $data);
  25. } catch (\Exception $e) {
  26. if ($this->node->childExists($name)) {
  27. $child = $this->node->getChild($name);
  28. $child->delete();
  29. }
  30. throw $e;
  31. }
  32. }
  33. public function createDirectory($name) {
  34. throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
  35. }
  36. public function getChild($name) {
  37. if ($name === '.file') {
  38. return new FutureFile($this->node, '.file');
  39. }
  40. return new UploadFile($this->node->getChild($name));
  41. }
  42. public function getChildren() {
  43. $tmpChildren = $this->node->getChildren();
  44. $children = [];
  45. $children[] = new FutureFile($this->node, '.file');
  46. foreach ($tmpChildren as $child) {
  47. $children[] = new UploadFile($child);
  48. }
  49. if ($this->storage->instanceOfStorage(ObjectStoreStorage::class)) {
  50. /** @var ObjectStoreStorage $storage */
  51. $objectStore = $this->storage->getObjectStore();
  52. if ($objectStore instanceof IObjectStoreMultiPartUpload) {
  53. $cache = \OC::$server->getMemCacheFactory()->createDistributed(ChunkingV2Plugin::CACHE_KEY);
  54. $uploadSession = $cache->get($this->getName());
  55. if ($uploadSession) {
  56. $uploadId = $uploadSession[ChunkingV2Plugin::UPLOAD_ID];
  57. $id = $uploadSession[ChunkingV2Plugin::UPLOAD_TARGET_ID];
  58. $parts = $objectStore->getMultipartUploads($this->storage->getURN($id), $uploadId);
  59. foreach ($parts as $part) {
  60. $children[] = new PartFile($this->node, $part);
  61. }
  62. }
  63. }
  64. }
  65. return $children;
  66. }
  67. public function childExists($name) {
  68. if ($name === '.file') {
  69. return true;
  70. }
  71. return $this->node->childExists($name);
  72. }
  73. public function delete() {
  74. $this->node->delete();
  75. // Background cleanup job is not needed anymore
  76. $this->cleanupService->removeJob($this->getName());
  77. }
  78. public function getName() {
  79. return $this->node->getName();
  80. }
  81. public function setName($name) {
  82. throw new Forbidden('Permission denied to rename this folder');
  83. }
  84. public function getLastModified() {
  85. return $this->node->getLastModified();
  86. }
  87. public function getStorage() {
  88. return $this->storage;
  89. }
  90. }