UploadFolder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $this->node->createFile($name, $data);
  47. }
  48. public function createDirectory($name) {
  49. throw new Forbidden('Permission denied to create file (filename ' . $name . ')');
  50. }
  51. public function getChild($name) {
  52. if ($name === '.file') {
  53. return new FutureFile($this->node, '.file');
  54. }
  55. return new UploadFile($this->node->getChild($name));
  56. }
  57. public function getChildren() {
  58. $tmpChildren = $this->node->getChildren();
  59. $children = [];
  60. $children[] = new FutureFile($this->node, '.file');
  61. foreach ($tmpChildren as $child) {
  62. $children[] = new UploadFile($child);
  63. }
  64. if ($this->storage->instanceOfStorage(ObjectStoreStorage::class)) {
  65. /** @var ObjectStoreStorage $storage */
  66. $objectStore = $this->storage->getObjectStore();
  67. if ($objectStore instanceof IObjectStoreMultiPartUpload) {
  68. $cache = \OC::$server->getMemCacheFactory()->createDistributed(ChunkingV2Plugin::CACHE_KEY);
  69. $uploadSession = $cache->get($this->getName());
  70. if ($uploadSession) {
  71. $uploadId = $uploadSession[ChunkingV2Plugin::UPLOAD_ID];
  72. $id = $uploadSession[ChunkingV2Plugin::UPLOAD_TARGET_ID];
  73. $parts = $objectStore->getMultipartUploads($this->storage->getURN($id), $uploadId);
  74. foreach ($parts as $part) {
  75. $children[] = new PartFile($this->node, $part);
  76. }
  77. }
  78. }
  79. }
  80. return $children;
  81. }
  82. public function childExists($name) {
  83. if ($name === '.file') {
  84. return true;
  85. }
  86. return $this->node->childExists($name);
  87. }
  88. public function delete() {
  89. $this->node->delete();
  90. // Background cleanup job is not needed anymore
  91. $this->cleanupService->removeJob($this->getName());
  92. }
  93. public function getName() {
  94. return $this->node->getName();
  95. }
  96. public function setName($name) {
  97. throw new Forbidden('Permission denied to rename this folder');
  98. }
  99. public function getLastModified() {
  100. return $this->node->getLastModified();
  101. }
  102. public function getStorage() {
  103. return $this->storage;
  104. }
  105. }