Folder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Vincent Petry <vincent@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Files\Node;
  33. use OC\Files\Cache\QuerySearchHelper;
  34. use OC\Files\Search\SearchBinaryOperator;
  35. use OC\Files\Search\SearchComparison;
  36. use OC\Files\Search\SearchOrder;
  37. use OC\Files\Search\SearchQuery;
  38. use OC\Files\Utils\PathHelper;
  39. use OCP\Files\Cache\ICacheEntry;
  40. use OCP\Files\FileInfo;
  41. use OCP\Files\Mount\IMountPoint;
  42. use OCP\Files\NotFoundException;
  43. use OCP\Files\NotPermittedException;
  44. use OCP\Files\Search\ISearchBinaryOperator;
  45. use OCP\Files\Search\ISearchComparison;
  46. use OCP\Files\Search\ISearchOperator;
  47. use OCP\Files\Search\ISearchOrder;
  48. use OCP\Files\Search\ISearchQuery;
  49. use OCP\IUserManager;
  50. class Folder extends Node implements \OCP\Files\Folder {
  51. /**
  52. * Creates a Folder that represents a non-existing path
  53. *
  54. * @param string $path path
  55. * @return NonExistingFolder non-existing node
  56. */
  57. protected function createNonExistingNode($path) {
  58. return new NonExistingFolder($this->root, $this->view, $path);
  59. }
  60. /**
  61. * @param string $path path relative to the folder
  62. * @return string
  63. * @throws \OCP\Files\NotPermittedException
  64. */
  65. public function getFullPath($path) {
  66. $path = $this->normalizePath($path);
  67. if (!$this->isValidPath($path)) {
  68. throw new NotPermittedException('Invalid path "' . $path . '"');
  69. }
  70. return $this->path . $path;
  71. }
  72. /**
  73. * @param string $path
  74. * @return string|null
  75. */
  76. public function getRelativePath($path) {
  77. return PathHelper::getRelativePath($this->getPath(), $path);
  78. }
  79. /**
  80. * check if a node is a (grand-)child of the folder
  81. *
  82. * @param \OC\Files\Node\Node $node
  83. * @return bool
  84. */
  85. public function isSubNode($node) {
  86. return strpos($node->getPath(), $this->path . '/') === 0;
  87. }
  88. /**
  89. * get the content of this directory
  90. *
  91. * @return Node[]
  92. * @throws \OCP\Files\NotFoundException
  93. */
  94. public function getDirectoryListing() {
  95. $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false));
  96. return array_map(function (FileInfo $info) {
  97. if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
  98. return new Folder($this->root, $this->view, $info->getPath(), $info, $this);
  99. } else {
  100. return new File($this->root, $this->view, $info->getPath(), $info, $this);
  101. }
  102. }, $folderContent);
  103. }
  104. /**
  105. * @param string $path
  106. * @param FileInfo $info
  107. * @return File|Folder
  108. */
  109. protected function createNode($path, FileInfo $info = null, bool $infoHasSubMountsIncluded = true) {
  110. if (is_null($info)) {
  111. $isDir = $this->view->is_dir($path);
  112. } else {
  113. $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
  114. }
  115. $parent = dirname($path) === $this->getPath() ? $this : null;
  116. if ($isDir) {
  117. return new Folder($this->root, $this->view, $path, $info, $parent, $infoHasSubMountsIncluded);
  118. } else {
  119. return new File($this->root, $this->view, $path, $info, $parent);
  120. }
  121. }
  122. /**
  123. * Get the node at $path
  124. *
  125. * @param string $path
  126. * @return \OC\Files\Node\Node
  127. * @throws \OCP\Files\NotFoundException
  128. */
  129. public function get($path) {
  130. return $this->root->get($this->getFullPath($path));
  131. }
  132. /**
  133. * @param string $path
  134. * @return bool
  135. */
  136. public function nodeExists($path) {
  137. try {
  138. $this->get($path);
  139. return true;
  140. } catch (NotFoundException $e) {
  141. return false;
  142. }
  143. }
  144. /**
  145. * @param string $path
  146. * @return \OC\Files\Node\Folder
  147. * @throws \OCP\Files\NotPermittedException
  148. */
  149. public function newFolder($path) {
  150. if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
  151. $fullPath = $this->getFullPath($path);
  152. $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
  153. $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
  154. if (!$this->view->mkdir($fullPath)) {
  155. throw new NotPermittedException('Could not create folder "' . $fullPath . '"');
  156. }
  157. $parent = dirname($fullPath) === $this->getPath() ? $this : null;
  158. $node = new Folder($this->root, $this->view, $fullPath, null, $parent);
  159. $this->sendHooks(['postWrite', 'postCreate'], [$node]);
  160. return $node;
  161. } else {
  162. throw new NotPermittedException('No create permission for folder "' . $path . '"');
  163. }
  164. }
  165. /**
  166. * @param string $path
  167. * @param string | resource | null $content
  168. * @return \OC\Files\Node\File
  169. * @throws \OCP\Files\NotPermittedException
  170. */
  171. public function newFile($path, $content = null) {
  172. if (empty($path)) {
  173. throw new NotPermittedException('Could not create as provided path is empty');
  174. }
  175. if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
  176. $fullPath = $this->getFullPath($path);
  177. $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
  178. $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
  179. if ($content !== null) {
  180. $result = $this->view->file_put_contents($fullPath, $content);
  181. } else {
  182. $result = $this->view->touch($fullPath);
  183. }
  184. if ($result === false) {
  185. throw new NotPermittedException('Could not create path "' . $fullPath . '"');
  186. }
  187. $node = new File($this->root, $this->view, $fullPath, null, $this);
  188. $this->sendHooks(['postWrite', 'postCreate'], [$node]);
  189. return $node;
  190. }
  191. throw new NotPermittedException('No create permission for path "' . $path . '"');
  192. }
  193. private function queryFromOperator(ISearchOperator $operator, string $uid = null, int $limit = 0, int $offset = 0): ISearchQuery {
  194. if ($uid === null) {
  195. $user = null;
  196. } else {
  197. /** @var IUserManager $userManager */
  198. $userManager = \OC::$server->query(IUserManager::class);
  199. $user = $userManager->get($uid);
  200. }
  201. return new SearchQuery($operator, $limit, $offset, [], $user);
  202. }
  203. /**
  204. * search for files with the name matching $query
  205. *
  206. * @param string|ISearchQuery $query
  207. * @return \OC\Files\Node\Node[]
  208. */
  209. public function search($query) {
  210. if (is_string($query)) {
  211. $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'));
  212. }
  213. // search is handled by a single query covering all caches that this folder contains
  214. // this is done by collect
  215. $limitToHome = $query->limitToHome();
  216. if ($limitToHome && count(explode('/', $this->path)) !== 3) {
  217. throw new \InvalidArgumentException('searching by owner is only allowed in the users home folder');
  218. }
  219. /** @var QuerySearchHelper $searchHelper */
  220. $searchHelper = \OC::$server->get(QuerySearchHelper::class);
  221. [$caches, $mountByMountPoint] = $searchHelper->getCachesAndMountPointsForSearch($this->root, $this->path, $limitToHome);
  222. $resultsPerCache = $searchHelper->searchInCaches($query, $caches);
  223. // loop through all results per-cache, constructing the FileInfo object from the CacheEntry and merge them all
  224. $files = array_merge(...array_map(function (array $results, string $relativeMountPoint) use ($mountByMountPoint) {
  225. $mount = $mountByMountPoint[$relativeMountPoint];
  226. return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) {
  227. return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result);
  228. }, $results);
  229. }, array_values($resultsPerCache), array_keys($resultsPerCache)));
  230. // don't include this folder in the results
  231. $files = array_filter($files, function (FileInfo $file) {
  232. return $file->getPath() !== $this->getPath();
  233. });
  234. // since results were returned per-cache, they are no longer fully sorted
  235. $order = $query->getOrder();
  236. if ($order) {
  237. usort($files, function (FileInfo $a, FileInfo $b) use ($order) {
  238. foreach ($order as $orderField) {
  239. $cmp = $orderField->sortFileInfo($a, $b);
  240. if ($cmp !== 0) {
  241. return $cmp;
  242. }
  243. }
  244. return 0;
  245. });
  246. }
  247. return array_map(function (FileInfo $file) {
  248. return $this->createNode($file->getPath(), $file);
  249. }, $files);
  250. }
  251. private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo {
  252. $cacheEntry['internalPath'] = $cacheEntry['path'];
  253. $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/');
  254. $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : '';
  255. return new \OC\Files\FileInfo($this->path . $subPath, $mount->getStorage(), $cacheEntry['internalPath'], $cacheEntry, $mount);
  256. }
  257. /**
  258. * search for files by mimetype
  259. *
  260. * @param string $mimetype
  261. * @return Node[]
  262. */
  263. public function searchByMime($mimetype) {
  264. if (strpos($mimetype, '/') === false) {
  265. $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%'));
  266. } else {
  267. $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype));
  268. }
  269. return $this->search($query);
  270. }
  271. /**
  272. * search for files by tag
  273. *
  274. * @param string|int $tag name or tag id
  275. * @param string $userId owner of the tags
  276. * @return Node[]
  277. */
  278. public function searchByTag($tag, $userId) {
  279. $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId);
  280. return $this->search($query);
  281. }
  282. /**
  283. * @param int $id
  284. * @return \OC\Files\Node\Node[]
  285. */
  286. public function getById($id) {
  287. return $this->root->getByIdInPath((int)$id, $this->getPath());
  288. }
  289. protected function getAppDataDirectoryName(): string {
  290. $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
  291. return 'appdata_' . $instanceId;
  292. }
  293. /**
  294. * In case the path we are currently in is inside the appdata_* folder,
  295. * the original getById method does not work, because it can only look inside
  296. * the user's mount points. But the user has no mount point for the root storage.
  297. *
  298. * So in that case we directly check the mount of the root if it contains
  299. * the id. If it does we check if the path is inside the path we are working
  300. * in.
  301. *
  302. * @param int $id
  303. * @return array
  304. */
  305. protected function getByIdInRootMount(int $id): array {
  306. $mount = $this->root->getMount('');
  307. $cacheEntry = $mount->getStorage()->getCache($this->path)->get($id);
  308. if (!$cacheEntry) {
  309. return [];
  310. }
  311. $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/');
  312. $currentPath = rtrim($this->path, '/') . '/';
  313. if (strpos($absolutePath, $currentPath) !== 0) {
  314. return [];
  315. }
  316. return [$this->root->createNode(
  317. $absolutePath, new \OC\Files\FileInfo(
  318. $absolutePath,
  319. $mount->getStorage(),
  320. $cacheEntry->getPath(),
  321. $cacheEntry,
  322. $mount
  323. ))];
  324. }
  325. public function getFreeSpace() {
  326. return $this->view->free_space($this->path);
  327. }
  328. public function delete() {
  329. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  330. $this->sendHooks(['preDelete']);
  331. $fileInfo = $this->getFileInfo();
  332. $this->view->rmdir($this->path);
  333. $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
  334. $this->sendHooks(['postDelete'], [$nonExisting]);
  335. } else {
  336. throw new NotPermittedException('No delete permission for path "' . $this->path . '"');
  337. }
  338. }
  339. /**
  340. * Add a suffix to the name in case the file exists
  341. *
  342. * @param string $name
  343. * @return string
  344. * @throws NotPermittedException
  345. */
  346. public function getNonExistingName($name) {
  347. $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
  348. return trim($this->getRelativePath($uniqueName), '/');
  349. }
  350. /**
  351. * @param int $limit
  352. * @param int $offset
  353. * @return \OCP\Files\Node[]
  354. */
  355. public function getRecent($limit, $offset = 0) {
  356. $filterOutNonEmptyFolder = new SearchBinaryOperator(
  357. // filter out non empty folders
  358. ISearchBinaryOperator::OPERATOR_OR,
  359. [
  360. new SearchBinaryOperator(
  361. ISearchBinaryOperator::OPERATOR_NOT,
  362. [
  363. new SearchComparison(
  364. ISearchComparison::COMPARE_EQUAL,
  365. 'mimetype',
  366. FileInfo::MIMETYPE_FOLDER
  367. ),
  368. ]
  369. ),
  370. new SearchComparison(
  371. ISearchComparison::COMPARE_EQUAL,
  372. 'size',
  373. 0
  374. ),
  375. ]
  376. );
  377. $filterNonRecentFiles = new SearchComparison(
  378. ISearchComparison::COMPARE_GREATER_THAN,
  379. 'mtime',
  380. strtotime("-2 week")
  381. );
  382. if ($offset === 0 && $limit <= 100) {
  383. $query = new SearchQuery(
  384. new SearchBinaryOperator(
  385. ISearchBinaryOperator::OPERATOR_AND,
  386. [
  387. $filterOutNonEmptyFolder,
  388. $filterNonRecentFiles,
  389. ],
  390. ),
  391. $limit,
  392. $offset,
  393. [
  394. new SearchOrder(
  395. ISearchOrder::DIRECTION_DESCENDING,
  396. 'mtime'
  397. ),
  398. ]
  399. );
  400. } else {
  401. $query = new SearchQuery(
  402. $filterOutNonEmptyFolder,
  403. $limit,
  404. $offset,
  405. [
  406. new SearchOrder(
  407. ISearchOrder::DIRECTION_DESCENDING,
  408. 'mtime'
  409. ),
  410. ]
  411. );
  412. }
  413. return $this->search($query);
  414. }
  415. }