UploadFolder.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Upload;
  26. use OC\Files\ObjectStore\ObjectStoreStorage;
  27. use OCA\DAV\Connector\Sabre\Directory;
  28. use OCP\Files\ObjectStore\IObjectStoreMultiPartUpload;
  29. use OCP\Files\Storage\IStorage;
  30. use Sabre\DAV\Exception\Forbidden;
  31. use Sabre\DAV\ICollection;
  32. class UploadFolder implements ICollection {
  33. /** @var Directory */
  34. private $node;
  35. /** @var CleanupService */
  36. private $cleanupService;
  37. /** @var IStorage */
  38. private $storage;
  39. public function __construct(Directory $node, CleanupService $cleanupService, IStorage $storage) {
  40. $this->node = $node;
  41. $this->cleanupService = $cleanupService;
  42. $this->storage = $storage;
  43. }
  44. public function createFile($name, $data = null) {
  45. // TODO: verify name - should be a simple number
  46. try {
  47. $this->node->createFile($name, $data);
  48. } catch (\Exception $e) {
  49. if ($this->node->childExists($name)) {
  50. $child = $this->node->getChild($name);
  51. $child->delete();
  52. }
  53. throw $e;
  54. }
  55. }
  56. public function createDirectory($name) {
  57. throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
  58. }
  59. public function getChild($name) {
  60. if ($name === '.file') {
  61. return new FutureFile($this->node, '.file');
  62. }
  63. return new UploadFile($this->node->getChild($name));
  64. }
  65. public function getChildren() {
  66. $tmpChildren = $this->node->getChildren();
  67. $children = [];
  68. $children[] = new FutureFile($this->node, '.file');
  69. foreach ($tmpChildren as $child) {
  70. $children[] = new UploadFile($child);
  71. }
  72. if ($this->storage->instanceOfStorage(ObjectStoreStorage::class)) {
  73. /** @var ObjectStoreStorage $storage */
  74. $objectStore = $this->storage->getObjectStore();
  75. if ($objectStore instanceof IObjectStoreMultiPartUpload) {
  76. $cache = \OC::$server->getMemCacheFactory()->createDistributed(ChunkingV2Plugin::CACHE_KEY);
  77. $uploadSession = $cache->get($this->getName());
  78. if ($uploadSession) {
  79. $uploadId = $uploadSession[ChunkingV2Plugin::UPLOAD_ID];
  80. $id = $uploadSession[ChunkingV2Plugin::UPLOAD_TARGET_ID];
  81. $parts = $objectStore->getMultipartUploads($this->storage->getURN($id), $uploadId);
  82. foreach ($parts as $part) {
  83. $children[] = new PartFile($this->node, $part);
  84. }
  85. }
  86. }
  87. }
  88. return $children;
  89. }
  90. public function childExists($name) {
  91. if ($name === '.file') {
  92. return true;
  93. }
  94. return $this->node->childExists($name);
  95. }
  96. public function delete() {
  97. $this->node->delete();
  98. // Background cleanup job is not needed anymore
  99. $this->cleanupService->removeJob($this->getName());
  100. }
  101. public function getName() {
  102. return $this->node->getName();
  103. }
  104. public function setName($name) {
  105. throw new Forbidden('Permission denied to rename this folder');
  106. }
  107. public function getLastModified() {
  108. return $this->node->getLastModified();
  109. }
  110. public function getStorage() {
  111. return $this->storage;
  112. }
  113. }