directory.php 8.3 KB

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