UploadFolder.php 3.0 KB

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