Directory.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Jakob Sack <mail@jakobsack.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\Connector\Sabre;
  32. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  33. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  34. use OCA\DAV\Connector\Sabre\Exception\FileLocked;
  35. use OCP\Files\ForbiddenException;
  36. use OCP\Lock\ILockingProvider;
  37. use OCP\Lock\LockedException;
  38. use Sabre\DAV\Exception\Locked;
  39. class Directory extends \OCA\DAV\Connector\Sabre\Node
  40. implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota {
  41. /**
  42. * Cached directory content
  43. *
  44. * @var \OCP\Files\FileInfo[]
  45. */
  46. private $dirContent;
  47. /**
  48. * Cached quota info
  49. *
  50. * @var array
  51. */
  52. private $quotaInfo;
  53. /**
  54. * @var ObjectTree|null
  55. */
  56. private $tree;
  57. /**
  58. * Sets up the node, expects a full path name
  59. *
  60. * @param \OC\Files\View $view
  61. * @param \OCP\Files\FileInfo $info
  62. * @param ObjectTree|null $tree
  63. * @param \OCP\Share\IManager $shareManager
  64. */
  65. public function __construct($view, $info, $tree = null, $shareManager = null) {
  66. parent::__construct($view, $info, $shareManager);
  67. $this->tree = $tree;
  68. }
  69. /**
  70. * Creates a new file in the directory
  71. *
  72. * Data will either be supplied as a stream resource, or in certain cases
  73. * as a string. Keep in mind that you may have to support either.
  74. *
  75. * After successful creation of the file, you may choose to return the ETag
  76. * of the new file here.
  77. *
  78. * The returned ETag must be surrounded by double-quotes (The quotes should
  79. * be part of the actual string).
  80. *
  81. * If you cannot accurately determine the ETag, you should not return it.
  82. * If you don't store the file exactly as-is (you're transforming it
  83. * somehow) you should also not return an ETag.
  84. *
  85. * This means that if a subsequent GET to this new file does not exactly
  86. * return the same contents of what was submitted here, you are strongly
  87. * recommended to omit the ETag.
  88. *
  89. * @param string $name Name of the file
  90. * @param resource|string $data Initial payload
  91. * @return null|string
  92. * @throws Exception\EntityTooLarge
  93. * @throws Exception\UnsupportedMediaType
  94. * @throws FileLocked
  95. * @throws InvalidPath
  96. * @throws \Sabre\DAV\Exception
  97. * @throws \Sabre\DAV\Exception\BadRequest
  98. * @throws \Sabre\DAV\Exception\Forbidden
  99. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  100. */
  101. public function createFile($name, $data = null) {
  102. try {
  103. // for chunked upload also updating a existing file is a "createFile"
  104. // because we create all the chunks before re-assemble them to the existing file.
  105. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  106. // exit if we can't create a new file and we don't updatable existing file
  107. $info = \OC_FileChunking::decodeName($name);
  108. if (!$this->fileView->isCreatable($this->path) &&
  109. !$this->fileView->isUpdatable($this->path . '/' . $info['name'])
  110. ) {
  111. throw new \Sabre\DAV\Exception\Forbidden();
  112. }
  113. } else {
  114. // For non-chunked upload it is enough to check if we can create a new file
  115. if (!$this->fileView->isCreatable($this->path)) {
  116. throw new \Sabre\DAV\Exception\Forbidden();
  117. }
  118. }
  119. $this->fileView->verifyPath($this->path, $name);
  120. $path = $this->fileView->getAbsolutePath($this->path) . '/' . $name;
  121. // using a dummy FileInfo is acceptable here since it will be refreshed after the put is complete
  122. $info = new \OC\Files\FileInfo($path, null, null, array(), null);
  123. $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
  124. $node->acquireLock(ILockingProvider::LOCK_SHARED);
  125. return $node->put($data);
  126. } catch (\OCP\Files\StorageNotAvailableException $e) {
  127. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
  128. } catch (\OCP\Files\InvalidPathException $ex) {
  129. throw new InvalidPath($ex->getMessage());
  130. } catch (ForbiddenException $ex) {
  131. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  132. } catch (LockedException $e) {
  133. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  134. }
  135. }
  136. /**
  137. * Creates a new subdirectory
  138. *
  139. * @param string $name
  140. * @throws FileLocked
  141. * @throws InvalidPath
  142. * @throws \Sabre\DAV\Exception\Forbidden
  143. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  144. */
  145. public function createDirectory($name) {
  146. try {
  147. if (!$this->info->isCreatable()) {
  148. throw new \Sabre\DAV\Exception\Forbidden();
  149. }
  150. $this->fileView->verifyPath($this->path, $name);
  151. $newPath = $this->path . '/' . $name;
  152. if (!$this->fileView->mkdir($newPath)) {
  153. throw new \Sabre\DAV\Exception\Forbidden('Could not create directory ' . $newPath);
  154. }
  155. } catch (\OCP\Files\StorageNotAvailableException $e) {
  156. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
  157. } catch (\OCP\Files\InvalidPathException $ex) {
  158. throw new InvalidPath($ex->getMessage());
  159. } catch (ForbiddenException $ex) {
  160. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  161. } catch (LockedException $e) {
  162. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  163. }
  164. }
  165. /**
  166. * Returns a specific child node, referenced by its name
  167. *
  168. * @param string $name
  169. * @param \OCP\Files\FileInfo $info
  170. * @return \Sabre\DAV\INode
  171. * @throws InvalidPath
  172. * @throws \Sabre\DAV\Exception\NotFound
  173. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  174. */
  175. public function getChild($name, $info = null) {
  176. $path = $this->path . '/' . $name;
  177. if (is_null($info)) {
  178. try {
  179. $this->fileView->verifyPath($this->path, $name);
  180. $info = $this->fileView->getFileInfo($path);
  181. } catch (\OCP\Files\StorageNotAvailableException $e) {
  182. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
  183. } catch (\OCP\Files\InvalidPathException $ex) {
  184. throw new InvalidPath($ex->getMessage());
  185. } catch (ForbiddenException $e) {
  186. throw new \Sabre\DAV\Exception\Forbidden();
  187. }
  188. }
  189. if (!$info) {
  190. throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
  191. }
  192. if ($info['mimetype'] == 'httpd/unix-directory') {
  193. $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager);
  194. } else {
  195. $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager);
  196. }
  197. if ($this->tree) {
  198. $this->tree->cacheNode($node);
  199. }
  200. return $node;
  201. }
  202. /**
  203. * Returns an array with all the child nodes
  204. *
  205. * @return \Sabre\DAV\INode[]
  206. */
  207. public function getChildren() {
  208. if (!is_null($this->dirContent)) {
  209. return $this->dirContent;
  210. }
  211. try {
  212. $folderContent = $this->fileView->getDirectoryContent($this->path);
  213. } catch (LockedException $e) {
  214. throw new Locked();
  215. }
  216. $nodes = array();
  217. foreach ($folderContent as $info) {
  218. $node = $this->getChild($info->getName(), $info);
  219. $nodes[] = $node;
  220. }
  221. $this->dirContent = $nodes;
  222. return $this->dirContent;
  223. }
  224. /**
  225. * Checks if a child exists.
  226. *
  227. * @param string $name
  228. * @return bool
  229. */
  230. public function childExists($name) {
  231. // note: here we do NOT resolve the chunk file name to the real file name
  232. // to make sure we return false when checking for file existence with a chunk
  233. // file name.
  234. // This is to make sure that "createFile" is still triggered
  235. // (required old code) instead of "updateFile".
  236. //
  237. // TODO: resolve chunk file name here and implement "updateFile"
  238. $path = $this->path . '/' . $name;
  239. return $this->fileView->file_exists($path);
  240. }
  241. /**
  242. * Deletes all files in this directory, and then itself
  243. *
  244. * @return void
  245. * @throws FileLocked
  246. * @throws \Sabre\DAV\Exception\Forbidden
  247. */
  248. public function delete() {
  249. if ($this->path === '' || $this->path === '/' || !$this->info->isDeletable()) {
  250. throw new \Sabre\DAV\Exception\Forbidden();
  251. }
  252. try {
  253. if (!$this->fileView->rmdir($this->path)) {
  254. // assume it wasn't possible to remove due to permission issue
  255. throw new \Sabre\DAV\Exception\Forbidden();
  256. }
  257. } catch (ForbiddenException $ex) {
  258. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  259. } catch (LockedException $e) {
  260. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  261. }
  262. }
  263. /**
  264. * Returns available diskspace information
  265. *
  266. * @return array
  267. */
  268. public function getQuotaInfo() {
  269. if ($this->quotaInfo) {
  270. return $this->quotaInfo;
  271. }
  272. try {
  273. $storageInfo = \OC_Helper::getStorageInfo($this->info->getPath(), $this->info);
  274. if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
  275. $free = \OCP\Files\FileInfo::SPACE_UNLIMITED;
  276. } else {
  277. $free = $storageInfo['free'];
  278. }
  279. $this->quotaInfo = array(
  280. $storageInfo['used'],
  281. $free
  282. );
  283. return $this->quotaInfo;
  284. } catch (\OCP\Files\StorageNotAvailableException $e) {
  285. return array(0, 0);
  286. }
  287. }
  288. }