folder.php 11 KB

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