Folder.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Node;
  27. use OC\DB\QueryBuilder\Literal;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\Files\Config\ICachedMountInfo;
  30. use OCP\Files\FileInfo;
  31. use OCP\Files\Mount\IMountPoint;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\NotPermittedException;
  34. class Folder extends Node implements \OCP\Files\Folder {
  35. /**
  36. * @param string $path path relative to the folder
  37. * @return string
  38. * @throws \OCP\Files\NotPermittedException
  39. */
  40. public function getFullPath($path) {
  41. if (!$this->isValidPath($path)) {
  42. throw new NotPermittedException('Invalid path');
  43. }
  44. return $this->path . $this->normalizePath($path);
  45. }
  46. /**
  47. * @param string $path
  48. * @return string
  49. */
  50. public function getRelativePath($path) {
  51. if ($this->path === '' or $this->path === '/') {
  52. return $this->normalizePath($path);
  53. }
  54. if ($path === $this->path) {
  55. return '/';
  56. } else if (strpos($path, $this->path . '/') !== 0) {
  57. return null;
  58. } else {
  59. $path = substr($path, strlen($this->path));
  60. return $this->normalizePath($path);
  61. }
  62. }
  63. /**
  64. * check if a node is a (grand-)child of the folder
  65. *
  66. * @param \OC\Files\Node\Node $node
  67. * @return bool
  68. */
  69. public function isSubNode($node) {
  70. return strpos($node->getPath(), $this->path . '/') === 0;
  71. }
  72. /**
  73. * get the content of this directory
  74. *
  75. * @throws \OCP\Files\NotFoundException
  76. * @return Node[]
  77. */
  78. public function getDirectoryListing() {
  79. $folderContent = $this->view->getDirectoryContent($this->path);
  80. return array_map(function (FileInfo $info) {
  81. if ($info->getMimetype() === 'httpd/unix-directory') {
  82. return new Folder($this->root, $this->view, $info->getPath(), $info);
  83. } else {
  84. return new File($this->root, $this->view, $info->getPath(), $info);
  85. }
  86. }, $folderContent);
  87. }
  88. /**
  89. * @param string $path
  90. * @param FileInfo $info
  91. * @return File|Folder
  92. */
  93. protected function createNode($path, FileInfo $info = null) {
  94. if (is_null($info)) {
  95. $isDir = $this->view->is_dir($path);
  96. } else {
  97. $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
  98. }
  99. if ($isDir) {
  100. return new Folder($this->root, $this->view, $path, $info);
  101. } else {
  102. return new File($this->root, $this->view, $path, $info);
  103. }
  104. }
  105. /**
  106. * Get the node at $path
  107. *
  108. * @param string $path
  109. * @return \OC\Files\Node\Node
  110. * @throws \OCP\Files\NotFoundException
  111. */
  112. public function get($path) {
  113. return $this->root->get($this->getFullPath($path));
  114. }
  115. /**
  116. * @param string $path
  117. * @return bool
  118. */
  119. public function nodeExists($path) {
  120. try {
  121. $this->get($path);
  122. return true;
  123. } catch (NotFoundException $e) {
  124. return false;
  125. }
  126. }
  127. /**
  128. * @param string $path
  129. * @return \OC\Files\Node\Folder
  130. * @throws \OCP\Files\NotPermittedException
  131. */
  132. public function newFolder($path) {
  133. if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
  134. $fullPath = $this->getFullPath($path);
  135. $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
  136. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  137. $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
  138. $this->view->mkdir($fullPath);
  139. $node = new Folder($this->root, $this->view, $fullPath);
  140. $this->root->emit('\OC\Files', 'postWrite', array($node));
  141. $this->root->emit('\OC\Files', 'postCreate', array($node));
  142. return $node;
  143. } else {
  144. throw new NotPermittedException('No create permission for folder');
  145. }
  146. }
  147. /**
  148. * @param string $path
  149. * @return \OC\Files\Node\File
  150. * @throws \OCP\Files\NotPermittedException
  151. */
  152. public function newFile($path) {
  153. if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
  154. $fullPath = $this->getFullPath($path);
  155. $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
  156. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  157. $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
  158. $this->view->touch($fullPath);
  159. $node = new File($this->root, $this->view, $fullPath);
  160. $this->root->emit('\OC\Files', 'postWrite', array($node));
  161. $this->root->emit('\OC\Files', 'postCreate', array($node));
  162. return $node;
  163. } else {
  164. throw new NotPermittedException('No create permission for path');
  165. }
  166. }
  167. /**
  168. * search for files with the name matching $query
  169. *
  170. * @param string $query
  171. * @return \OC\Files\Node\Node[]
  172. */
  173. public function search($query) {
  174. return $this->searchCommon('search', array('%' . $query . '%'));
  175. }
  176. /**
  177. * search for files by mimetype
  178. *
  179. * @param string $mimetype
  180. * @return Node[]
  181. */
  182. public function searchByMime($mimetype) {
  183. return $this->searchCommon('searchByMime', array($mimetype));
  184. }
  185. /**
  186. * search for files by tag
  187. *
  188. * @param string|int $tag name or tag id
  189. * @param string $userId owner of the tags
  190. * @return Node[]
  191. */
  192. public function searchByTag($tag, $userId) {
  193. return $this->searchCommon('searchByTag', array($tag, $userId));
  194. }
  195. /**
  196. * @param string $method cache method
  197. * @param array $args call args
  198. * @return \OC\Files\Node\Node[]
  199. */
  200. private function searchCommon($method, $args) {
  201. $files = array();
  202. $rootLength = strlen($this->path);
  203. $mount = $this->root->getMount($this->path);
  204. $storage = $mount->getStorage();
  205. $internalPath = $mount->getInternalPath($this->path);
  206. $internalPath = rtrim($internalPath, '/');
  207. if ($internalPath !== '') {
  208. $internalPath = $internalPath . '/';
  209. }
  210. $internalRootLength = strlen($internalPath);
  211. $cache = $storage->getCache('');
  212. $results = call_user_func_array(array($cache, $method), $args);
  213. foreach ($results as $result) {
  214. if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
  215. $result['internalPath'] = $result['path'];
  216. $result['path'] = substr($result['path'], $internalRootLength);
  217. $result['storage'] = $storage;
  218. $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
  219. }
  220. }
  221. $mounts = $this->root->getMountsIn($this->path);
  222. foreach ($mounts as $mount) {
  223. $storage = $mount->getStorage();
  224. if ($storage) {
  225. $cache = $storage->getCache('');
  226. $relativeMountPoint = substr($mount->getMountPoint(), $rootLength);
  227. $results = call_user_func_array(array($cache, $method), $args);
  228. foreach ($results as $result) {
  229. $result['internalPath'] = $result['path'];
  230. $result['path'] = $relativeMountPoint . $result['path'];
  231. $result['storage'] = $storage;
  232. $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
  233. }
  234. }
  235. }
  236. return array_map(function (FileInfo $file) {
  237. return $this->createNode($file->getPath(), $file);
  238. }, $files);
  239. }
  240. /**
  241. * @param int $id
  242. * @return \OC\Files\Node\Node[]
  243. */
  244. public function getById($id) {
  245. $mountCache = $this->root->getUserMountCache();
  246. $mountsContainingFile = $mountCache->getMountsForFileId((int)$id);
  247. $mounts = $this->root->getMountsIn($this->path);
  248. $mounts[] = $this->root->getMount($this->path);
  249. /** @var IMountPoint[] $folderMounts */
  250. $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
  251. return $mountPoint->getMountPoint();
  252. }, $mounts), $mounts);
  253. /** @var ICachedMountInfo[] $mountsContainingFile */
  254. $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
  255. return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
  256. }));
  257. if (count($mountsContainingFile) === 0) {
  258. return [];
  259. }
  260. // we only need to get the cache info once, since all mounts we found point to the same storage
  261. $mount = $folderMounts[$mountsContainingFile[0]->getMountPoint()];
  262. $cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
  263. if (!$cacheEntry) {
  264. return [];
  265. }
  266. // cache jails will hide the "true" internal path
  267. $internalPath = ltrim($mountsContainingFile[0]->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
  268. $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($cacheEntry, $folderMounts, $internalPath) {
  269. $mount = $folderMounts[$cachedMountInfo->getMountPoint()];
  270. $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
  271. $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
  272. $absolutePath = $cachedMountInfo->getMountPoint() . $pathRelativeToMount;
  273. return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
  274. $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
  275. \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
  276. ));
  277. }, $mountsContainingFile);
  278. return array_filter($nodes, function (Node $node) {
  279. return $this->getRelativePath($node->getPath());
  280. });
  281. }
  282. public function getFreeSpace() {
  283. return $this->view->free_space($this->path);
  284. }
  285. public function delete() {
  286. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  287. $this->sendHooks(array('preDelete'));
  288. $fileInfo = $this->getFileInfo();
  289. $this->view->rmdir($this->path);
  290. $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
  291. $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
  292. $this->exists = false;
  293. } else {
  294. throw new NotPermittedException('No delete permission for path');
  295. }
  296. }
  297. /**
  298. * @param string $targetPath
  299. * @throws \OCP\Files\NotPermittedException
  300. * @return \OC\Files\Node\Node
  301. */
  302. public function copy($targetPath) {
  303. $targetPath = $this->normalizePath($targetPath);
  304. $parent = $this->root->get(dirname($targetPath));
  305. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  306. $nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
  307. $this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
  308. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  309. $this->view->copy($this->path, $targetPath);
  310. $targetNode = $this->root->get($targetPath);
  311. $this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
  312. $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
  313. return $targetNode;
  314. } else {
  315. throw new NotPermittedException('No permission to copy to path');
  316. }
  317. }
  318. /**
  319. * @param string $targetPath
  320. * @throws \OCP\Files\NotPermittedException
  321. * @return \OC\Files\Node\Node
  322. */
  323. public function move($targetPath) {
  324. $targetPath = $this->normalizePath($targetPath);
  325. $parent = $this->root->get(dirname($targetPath));
  326. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  327. $nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
  328. $this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
  329. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  330. $this->view->rename($this->path, $targetPath);
  331. $targetNode = $this->root->get($targetPath);
  332. $this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
  333. $this->root->emit('\OC\Files', 'postWrite', array($targetNode));
  334. $this->path = $targetPath;
  335. return $targetNode;
  336. } else {
  337. throw new NotPermittedException('No permission to move to path');
  338. }
  339. }
  340. /**
  341. * Add a suffix to the name in case the file exists
  342. *
  343. * @param string $name
  344. * @return string
  345. * @throws NotPermittedException
  346. */
  347. public function getNonExistingName($name) {
  348. $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
  349. return trim($this->getRelativePath($uniqueName), '/');
  350. }
  351. /**
  352. * @param int $limit
  353. * @param int $offset
  354. * @return \OCP\Files\Node[]
  355. */
  356. public function getRecent($limit, $offset = 0) {
  357. $mimetypeLoader = \OC::$server->getMimeTypeLoader();
  358. $mounts = $this->root->getMountsIn($this->path);
  359. $mounts[] = $this->getMountPoint();
  360. $mounts = array_filter($mounts, function (IMountPoint $mount) {
  361. return $mount->getStorage();
  362. });
  363. $storageIds = array_map(function (IMountPoint $mount) {
  364. return $mount->getStorage()->getCache()->getNumericStorageId();
  365. }, $mounts);
  366. /** @var IMountPoint[] $mountMap */
  367. $mountMap = array_combine($storageIds, $mounts);
  368. $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
  369. //todo look into options of filtering path based on storage id (only search in files/ for home storage, filter by share root for shared, etc)
  370. $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  371. $query = $builder
  372. ->select('f.*')
  373. ->from('filecache', 'f')
  374. ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
  375. ->andWhere($builder->expr()->orX(
  376. // handle non empty folders separate
  377. $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
  378. $builder->expr()->eq('f.size', new Literal(0))
  379. ))
  380. ->orderBy('f.mtime', 'DESC')
  381. ->setMaxResults($limit)
  382. ->setFirstResult($offset);
  383. $result = $query->execute()->fetchAll();
  384. $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
  385. $mount = $mountMap[$entry['storage']];
  386. $entry['internalPath'] = $entry['path'];
  387. $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
  388. $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
  389. $path = $this->getAbsolutePath($mount, $entry['path']);
  390. if (is_null($path)) {
  391. return null;
  392. }
  393. $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
  394. return $this->root->createNode($fileInfo->getPath(), $fileInfo);
  395. }, $result));
  396. return array_values(array_filter($files, function (Node $node) {
  397. $relative = $this->getRelativePath($node->getPath());
  398. return $relative !== null && $relative !== '/';
  399. }));
  400. }
  401. private function getAbsolutePath(IMountPoint $mount, $path) {
  402. $storage = $mount->getStorage();
  403. if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
  404. /** @var \OC\Files\Storage\Wrapper\Jail $storage */
  405. $jailRoot = $storage->getSourcePath('');
  406. $rootLength = strlen($jailRoot) + 1;
  407. if ($path === $jailRoot) {
  408. return $mount->getMountPoint();
  409. } else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
  410. return $mount->getMountPoint() . substr($path, $rootLength);
  411. } else {
  412. return null;
  413. }
  414. } else {
  415. return $mount->getMountPoint() . $path;
  416. }
  417. }
  418. }