You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

folder.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Node;
  9. use OC\Files\Cache\Cache;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\NotPermittedException;
  12. class Folder extends Node implements \OCP\Files\Folder {
  13. /**
  14. * @param string $path path relative to the folder
  15. * @return string
  16. * @throws \OCP\Files\NotPermittedException
  17. */
  18. public function getFullPath($path) {
  19. if (!$this->isValidPath($path)) {
  20. throw new NotPermittedException();
  21. }
  22. return $this->path . $this->normalizePath($path);
  23. }
  24. /**
  25. * @param string $path
  26. * @throws \OCP\Files\NotFoundException
  27. * @return string
  28. */
  29. public function getRelativePath($path) {
  30. if ($this->path === '' or $this->path === '/') {
  31. return $this->normalizePath($path);
  32. }
  33. if (strpos($path, $this->path) !== 0) {
  34. throw new NotFoundException();
  35. } else {
  36. $path = substr($path, strlen($this->path));
  37. if (strlen($path) === 0) {
  38. return '/';
  39. } else {
  40. return $this->normalizePath($path);
  41. }
  42. }
  43. }
  44. /**
  45. * check if a node is a (grand-)child of the folder
  46. *
  47. * @param \OC\Files\Node\Node $node
  48. * @return bool
  49. */
  50. public function isSubNode($node) {
  51. return strpos($node->getPath(), $this->path . '/') === 0;
  52. }
  53. /**
  54. * get the content of this directory
  55. *
  56. * @throws \OCP\Files\NotFoundException
  57. * @return Node[]
  58. */
  59. public function getDirectoryListing() {
  60. $result = array();
  61. /**
  62. * @var \OC\Files\Storage\Storage $storage
  63. */
  64. list($storage, $internalPath) = $this->view->resolvePath($this->path);
  65. if ($storage) {
  66. $cache = $storage->getCache($internalPath);
  67. //trigger cache update check
  68. $this->view->getFileInfo($this->path);
  69. $files = $cache->getFolderContents($internalPath);
  70. } else {
  71. $files = array();
  72. }
  73. //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
  74. $mounts = $this->root->getMountsIn($this->path);
  75. $dirLength = strlen($this->path);
  76. foreach ($mounts as $mount) {
  77. $subStorage = $mount->getStorage();
  78. if ($subStorage) {
  79. $subCache = $subStorage->getCache('');
  80. if ($subCache->getStatus('') === Cache::NOT_FOUND) {
  81. $subScanner = $subStorage->getScanner('');
  82. $subScanner->scanFile('');
  83. }
  84. $rootEntry = $subCache->get('');
  85. if ($rootEntry) {
  86. $relativePath = trim(substr($mount->getMountPoint(), $dirLength), '/');
  87. if ($pos = strpos($relativePath, '/')) {
  88. //mountpoint inside subfolder add size to the correct folder
  89. $entryName = substr($relativePath, 0, $pos);
  90. foreach ($files as &$entry) {
  91. if ($entry['name'] === $entryName) {
  92. if ($rootEntry['size'] >= 0) {
  93. $entry['size'] += $rootEntry['size'];
  94. } else {
  95. $entry['size'] = -1;
  96. }
  97. }
  98. }
  99. } else { //mountpoint in this folder, add an entry for it
  100. $rootEntry['name'] = $relativePath;
  101. $rootEntry['storageObject'] = $subStorage;
  102. //remove any existing entry with the same name
  103. foreach ($files as $i => $file) {
  104. if ($file['name'] === $rootEntry['name']) {
  105. $files[$i] = null;
  106. break;
  107. }
  108. }
  109. $files[] = $rootEntry;
  110. }
  111. }
  112. }
  113. }
  114. foreach ($files as $file) {
  115. if ($file) {
  116. $node = $this->createNode($this->path . '/' . $file['name'], $file);
  117. $result[] = $node;
  118. }
  119. }
  120. return $result;
  121. }
  122. /**
  123. * @param string $path
  124. * @param array $info
  125. * @return File|Folder
  126. */
  127. protected function createNode($path, $info = array()) {
  128. if (!isset($info['mimetype'])) {
  129. $isDir = $this->view->is_dir($path);
  130. } else {
  131. $isDir = $info['mimetype'] === 'httpd/unix-directory';
  132. }
  133. if ($isDir) {
  134. return new Folder($this->root, $this->view, $path);
  135. } else {
  136. return new File($this->root, $this->view, $path);
  137. }
  138. }
  139. /**
  140. * Get the node at $path
  141. *
  142. * @param string $path
  143. * @return \OC\Files\Node\Node
  144. * @throws \OCP\Files\NotFoundException
  145. */
  146. public function get($path) {
  147. return $this->root->get($this->getFullPath($path));
  148. }
  149. /**
  150. * @param string $path
  151. * @return bool
  152. */
  153. public function nodeExists($path) {
  154. try {
  155. $this->get($path);
  156. return true;
  157. } catch (NotFoundException $e) {
  158. return false;
  159. }
  160. }
  161. /**
  162. * @param string $path
  163. * @return \OC\Files\Node\Folder
  164. * @throws \OCP\Files\NotPermittedException
  165. */
  166. public function newFolder($path) {
  167. if ($this->checkPermissions(\OCP\PERMISSION_CREATE)) {
  168. $fullPath = $this->getFullPath($path);
  169. $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
  170. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  171. $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
  172. $this->view->mkdir($fullPath);
  173. $node = new Folder($this->root, $this->view, $fullPath);
  174. $this->root->emit('\OC\Files', 'postWrite', array($node));
  175. $this->root->emit('\OC\Files', 'postCreate', array($node));
  176. return $node;
  177. } else {
  178. throw new NotPermittedException();
  179. }
  180. }
  181. /**
  182. * @param string $path
  183. * @return \OC\Files\Node\File
  184. * @throws \OCP\Files\NotPermittedException
  185. */
  186. public function newFile($path) {
  187. if ($this->checkPermissions(\OCP\PERMISSION_CREATE)) {
  188. $fullPath = $this->getFullPath($path);
  189. $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
  190. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  191. $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
  192. $this->view->touch($fullPath);
  193. $node = new File($this->root, $this->view, $fullPath);
  194. $this->root->emit('\OC\Files', 'postWrite', array($node));
  195. $this->root->emit('\OC\Files', 'postCreate', array($node));
  196. return $node;
  197. } else {
  198. throw new NotPermittedException();
  199. }
  200. }
  201. /**
  202. * search for files with the name matching $query
  203. *
  204. * @param string $query
  205. * @return \OC\Files\Node\Node[]
  206. */
  207. public function search($query) {
  208. return $this->searchCommon('%' . $query . '%', 'search');
  209. }
  210. /**
  211. * search for files by mimetype
  212. *
  213. * @param string $mimetype
  214. * @return Node[]
  215. */
  216. public function searchByMime($mimetype) {
  217. return $this->searchCommon($mimetype, 'searchByMime');
  218. }
  219. /**
  220. * @param string $query
  221. * @param string $method
  222. * @return \OC\Files\Node\Node[]
  223. */
  224. private function searchCommon($query, $method) {
  225. $files = array();
  226. $rootLength = strlen($this->path);
  227. /**
  228. * @var \OC\Files\Storage\Storage $storage
  229. */
  230. list($storage, $internalPath) = $this->view->resolvePath($this->path);
  231. $internalRootLength = strlen($internalPath);
  232. $cache = $storage->getCache('');
  233. $results = $cache->$method($query);
  234. foreach ($results as $result) {
  235. if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
  236. $result['internalPath'] = $result['path'];
  237. $result['path'] = substr($result['path'], $internalRootLength);
  238. $result['storage'] = $storage;
  239. $files[] = $result;
  240. }
  241. }
  242. $mounts = $this->root->getMountsIn($this->path);
  243. foreach ($mounts as $mount) {
  244. $storage = $mount->getStorage();
  245. if ($storage) {
  246. $cache = $storage->getCache('');
  247. $relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
  248. $results = $cache->$method($query);
  249. foreach ($results as $result) {
  250. $result['internalPath'] = $result['path'];
  251. $result['path'] = $relativeMountPoint . $result['path'];
  252. $result['storage'] = $storage;
  253. $files[] = $result;
  254. }
  255. }
  256. }
  257. $result = array();
  258. foreach ($files as $file) {
  259. $result[] = $this->createNode($this->normalizePath($this->path . '/' . $file['path']), $file);
  260. }
  261. return $result;
  262. }
  263. /**
  264. * @param int $id
  265. * @return \OC\Files\Node\Node[]
  266. */
  267. public function getById($id) {
  268. $nodes = $this->root->getById($id);
  269. $result = array();
  270. foreach ($nodes as $node) {
  271. $pathPart = substr($node->getPath(), 0, strlen($this->getPath()) + 1);
  272. if ($this->path === '/' or $pathPart === $this->getPath() . '/') {
  273. $result[] = $node;
  274. }
  275. }
  276. return $result;
  277. }
  278. public function getFreeSpace() {
  279. return $this->view->free_space($this->path);
  280. }
  281. /**
  282. * @return bool
  283. */
  284. public function isCreatable() {
  285. return $this->checkPermissions(\OCP\PERMISSION_CREATE);
  286. }
  287. public function delete() {
  288. if ($this->checkPermissions(\OCP\PERMISSION_DELETE)) {
  289. $this->sendHooks(array('preDelete'));
  290. $this->view->rmdir($this->path);
  291. $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path);
  292. $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
  293. $this->exists = false;
  294. } else {
  295. throw new NotPermittedException();
  296. }
  297. }
  298. /**
  299. * @param string $targetPath
  300. * @throws \OCP\Files\NotPermittedException
  301. * @return \OC\Files\Node\Node
  302. */
  303. public function copy($targetPath) {
  304. $targetPath = $this->normalizePath($targetPath);
  305. $parent = $this->root->get(dirname($targetPath));
  306. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  307. $nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
  308. $this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
  309. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  310. $this->view->copy($this->path, $targetPath);
  311. $targetNode = $this->root->get($targetPath);
  312. $this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
  313. $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
  314. return $targetNode;
  315. } else {
  316. throw new NotPermittedException();
  317. }
  318. }
  319. /**
  320. * @param string $targetPath
  321. * @throws \OCP\Files\NotPermittedException
  322. * @return \OC\Files\Node\Node
  323. */
  324. public function move($targetPath) {
  325. $targetPath = $this->normalizePath($targetPath);
  326. $parent = $this->root->get(dirname($targetPath));
  327. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  328. $nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
  329. $this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
  330. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  331. $this->view->rename($this->path, $targetPath);
  332. $targetNode = $this->root->get($targetPath);
  333. $this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
  334. $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
  335. $this->path = $targetPath;
  336. return $targetNode;
  337. } else {
  338. throw new NotPermittedException();
  339. }
  340. }
  341. }