123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?php
- namespace OC\Files\Node;
- use OC\Files\Cache\QuerySearchHelper;
- use OC\Files\Search\SearchBinaryOperator;
- use OC\Files\Search\SearchComparison;
- use OC\Files\Search\SearchOrder;
- use OC\Files\Search\SearchQuery;
- use OC\Files\Utils\PathHelper;
- use OC\User\LazyUser;
- use OCP\Files\Cache\ICacheEntry;
- use OCP\Files\FileInfo;
- use OCP\Files\Mount\IMountPoint;
- use OCP\Files\Node as INode;
- use OCP\Files\NotFoundException;
- use OCP\Files\NotPermittedException;
- use OCP\Files\Search\ISearchBinaryOperator;
- use OCP\Files\Search\ISearchComparison;
- use OCP\Files\Search\ISearchOperator;
- use OCP\Files\Search\ISearchOrder;
- use OCP\Files\Search\ISearchQuery;
- use OCP\IUserManager;
- class Folder extends Node implements \OCP\Files\Folder {
- private ?IUserManager $userManager = null;
-
- protected function createNonExistingNode($path) {
- return new NonExistingFolder($this->root, $this->view, $path);
- }
-
- public function getFullPath($path) {
- $path = $this->normalizePath($path);
- if (!$this->isValidPath($path)) {
- throw new NotPermittedException('Invalid path "' . $path . '"');
- }
- return $this->path . $path;
- }
-
- public function getRelativePath($path) {
- return PathHelper::getRelativePath($this->getPath(), $path);
- }
-
- public function isSubNode($node) {
- return str_starts_with($node->getPath(), $this->path . '/');
- }
-
- public function getDirectoryListing() {
- $folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false));
- return array_map(function (FileInfo $info) {
- if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
- return new Folder($this->root, $this->view, $info->getPath(), $info, $this);
- } else {
- return new File($this->root, $this->view, $info->getPath(), $info, $this);
- }
- }, $folderContent);
- }
- protected function createNode(string $path, ?FileInfo $info = null, bool $infoHasSubMountsIncluded = true): INode {
- if (is_null($info)) {
- $isDir = $this->view->is_dir($path);
- } else {
- $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
- }
- $parent = dirname($path) === $this->getPath() ? $this : null;
- if ($isDir) {
- return new Folder($this->root, $this->view, $path, $info, $parent, $infoHasSubMountsIncluded);
- } else {
- return new File($this->root, $this->view, $path, $info, $parent);
- }
- }
-
- public function get($path) {
- return $this->root->get($this->getFullPath($path));
- }
-
- public function nodeExists($path) {
- try {
- $this->get($path);
- return true;
- } catch (NotFoundException $e) {
- return false;
- }
- }
-
- public function newFolder($path) {
- if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
- $fullPath = $this->getFullPath($path);
- $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
- $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
- if (!$this->view->mkdir($fullPath) && !$this->view->is_dir($fullPath)) {
- throw new NotPermittedException('Could not create folder "' . $fullPath . '"');
- }
- $parent = dirname($fullPath) === $this->getPath() ? $this : null;
- $node = new Folder($this->root, $this->view, $fullPath, null, $parent);
- $this->sendHooks(['postWrite', 'postCreate'], [$node]);
- return $node;
- } else {
- throw new NotPermittedException('No create permission for folder "' . $path . '"');
- }
- }
-
- public function newFile($path, $content = null) {
- if ($path === '') {
- throw new NotPermittedException('Could not create as provided path is empty');
- }
- if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
- $fullPath = $this->getFullPath($path);
- $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
- $this->sendHooks(['preWrite', 'preCreate'], [$nonExisting]);
- if ($content !== null) {
- $result = $this->view->file_put_contents($fullPath, $content);
- } else {
- $result = $this->view->touch($fullPath);
- }
- if ($result === false) {
- throw new NotPermittedException('Could not create path "' . $fullPath . '"');
- }
- $node = new File($this->root, $this->view, $fullPath, null, $this);
- $this->sendHooks(['postWrite', 'postCreate'], [$node]);
- return $node;
- }
- throw new NotPermittedException('No create permission for path "' . $path . '"');
- }
- private function queryFromOperator(ISearchOperator $operator, ?string $uid = null, int $limit = 0, int $offset = 0): ISearchQuery {
- if ($uid === null) {
- $user = null;
- } else {
-
- $userManager = \OCP\Server::get(IUserManager::class);
- $user = $userManager->get($uid);
- }
- return new SearchQuery($operator, $limit, $offset, [], $user);
- }
-
- public function search($query) {
- if (is_string($query)) {
- $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query . '%'));
- }
-
-
- $limitToHome = $query->limitToHome();
- if ($limitToHome && count(explode('/', $this->path)) !== 3) {
- throw new \InvalidArgumentException('searching by owner is only allowed in the users home folder');
- }
-
- $searchHelper = \OC::$server->get(QuerySearchHelper::class);
- [$caches, $mountByMountPoint] = $searchHelper->getCachesAndMountPointsForSearch($this->root, $this->path, $limitToHome);
- $resultsPerCache = $searchHelper->searchInCaches($query, $caches);
-
- $files = array_merge(...array_map(function (array $results, string $relativeMountPoint) use ($mountByMountPoint) {
- $mount = $mountByMountPoint[$relativeMountPoint];
- return array_map(function (ICacheEntry $result) use ($relativeMountPoint, $mount) {
- return $this->cacheEntryToFileInfo($mount, $relativeMountPoint, $result);
- }, $results);
- }, array_values($resultsPerCache), array_keys($resultsPerCache)));
-
- $files = array_values(array_filter($files, function (FileInfo $file) {
- return $file->getPath() !== $this->getPath();
- }));
-
- $order = $query->getOrder();
- if ($order) {
- usort($files, function (FileInfo $a, FileInfo $b) use ($order) {
- foreach ($order as $orderField) {
- $cmp = $orderField->sortFileInfo($a, $b);
- if ($cmp !== 0) {
- return $cmp;
- }
- }
- return 0;
- });
- }
- return array_map(function (FileInfo $file) {
- return $this->createNode($file->getPath(), $file);
- }, $files);
- }
- private function cacheEntryToFileInfo(IMountPoint $mount, string $appendRoot, ICacheEntry $cacheEntry): FileInfo {
- $cacheEntry['internalPath'] = $cacheEntry['path'];
- $cacheEntry['path'] = rtrim($appendRoot . $cacheEntry->getPath(), '/');
- $subPath = $cacheEntry['path'] !== '' ? '/' . $cacheEntry['path'] : '';
- $storage = $mount->getStorage();
- $owner = null;
- $ownerId = $storage->getOwner($cacheEntry['internalPath']);
- if (!empty($ownerId)) {
-
- if ($this->userManager === null) {
- $this->userManager = \OCP\Server::get(IUserManager::class);
- }
- $owner = new LazyUser($ownerId, $this->userManager);
- }
- return new \OC\Files\FileInfo(
- $this->path . $subPath,
- $storage,
- $cacheEntry['internalPath'],
- $cacheEntry,
- $mount,
- $owner,
- );
- }
-
- public function searchByMime($mimetype) {
- if (!str_contains($mimetype, '/')) {
- $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%'));
- } else {
- $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype));
- }
- return $this->search($query);
- }
-
- public function searchByTag($tag, $userId) {
- $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', $tag), $userId);
- return $this->search($query);
- }
- public function searchBySystemTag(string $tagName, string $userId, int $limit = 0, int $offset = 0): array {
- $query = $this->queryFromOperator(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'systemtag', $tagName), $userId, $limit, $offset);
- return $this->search($query);
- }
-
- public function getById($id) {
- return $this->root->getByIdInPath((int)$id, $this->getPath());
- }
- public function getFirstNodeById(int $id): ?\OCP\Files\Node {
- return $this->root->getFirstNodeByIdInPath($id, $this->getPath());
- }
- public function getAppDataDirectoryName(): string {
- $instanceId = \OC::$server->getConfig()->getSystemValueString('instanceid');
- return 'appdata_' . $instanceId;
- }
-
- protected function getByIdInRootMount(int $id): array {
- if (!method_exists($this->root, 'createNode')) {
-
-
-
- return [];
- }
- $mount = $this->root->getMount('');
- $storage = $mount->getStorage();
- $cacheEntry = $storage?->getCache($this->path)->get($id);
- if (!$cacheEntry) {
- return [];
- }
- $absolutePath = '/' . ltrim($cacheEntry->getPath(), '/');
- $currentPath = rtrim($this->path, '/') . '/';
- if (!str_starts_with($absolutePath, $currentPath)) {
- return [];
- }
- return [$this->root->createNode(
- $absolutePath, new \OC\Files\FileInfo(
- $absolutePath,
- $storage,
- $cacheEntry->getPath(),
- $cacheEntry,
- $mount
- ))];
- }
- public function getFreeSpace() {
- return $this->view->free_space($this->path);
- }
- public function delete() {
- if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
- $this->sendHooks(['preDelete']);
- $fileInfo = $this->getFileInfo();
- $this->view->rmdir($this->path);
- $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
- $this->sendHooks(['postDelete'], [$nonExisting]);
- } else {
- throw new NotPermittedException('No delete permission for path "' . $this->path . '"');
- }
- }
-
- public function getNonExistingName($name) {
- $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
- return trim($this->getRelativePath($uniqueName), '/');
- }
-
- public function getRecent($limit, $offset = 0) {
- $filterOutNonEmptyFolder = new SearchBinaryOperator(
-
- ISearchBinaryOperator::OPERATOR_OR,
- [
- new SearchBinaryOperator(
- ISearchBinaryOperator::OPERATOR_NOT,
- [
- new SearchComparison(
- ISearchComparison::COMPARE_EQUAL,
- 'mimetype',
- FileInfo::MIMETYPE_FOLDER
- ),
- ]
- ),
- new SearchComparison(
- ISearchComparison::COMPARE_EQUAL,
- 'size',
- 0
- ),
- ]
- );
- $filterNonRecentFiles = new SearchComparison(
- ISearchComparison::COMPARE_GREATER_THAN,
- 'mtime',
- strtotime("-2 week")
- );
- if ($offset === 0 && $limit <= 100) {
- $query = new SearchQuery(
- new SearchBinaryOperator(
- ISearchBinaryOperator::OPERATOR_AND,
- [
- $filterOutNonEmptyFolder,
- $filterNonRecentFiles,
- ],
- ),
- $limit,
- $offset,
- [
- new SearchOrder(
- ISearchOrder::DIRECTION_DESCENDING,
- 'mtime'
- ),
- ]
- );
- } else {
- $query = new SearchQuery(
- $filterOutNonEmptyFolder,
- $limit,
- $offset,
- [
- new SearchOrder(
- ISearchOrder::DIRECTION_DESCENDING,
- 'mtime'
- ),
- ]
- );
- }
- return $this->search($query);
- }
- }
|