Skip to content

Commit 692da35

Browse files
committed
feat(IFileAccess#getByAncestorInStorage): Add new method to retrieve all files in a mount
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent d5ad9b8 commit 692da35

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

lib/private/Files/Cache/FileAccess.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OC\FilesMetadata\FilesMetadataManager;
1212
use OC\SystemConfig;
13+
use OCP\DB\Exception;
1314
use OCP\DB\QueryBuilder\IQueryBuilder;
1415
use OCP\Files\Cache\IFileAccess;
1516
use OCP\Files\IMimeTypeLoader;
@@ -94,4 +95,70 @@ public function getByFileIdsInStorage(array $fileIds, int $storageId): array {
9495
$rows = $query->executeQuery()->fetchAll();
9596
return $this->rowsToEntries($rows);
9697
}
98+
99+
/**
100+
* Retrieves files stored in a specific storage that have a specified ancestor in the file hierarchy.
101+
* Allows filtering by mime types, encryption status, and limits the number of results.
102+
*
103+
* @param int $storageId The ID of the storage to search within.
104+
* @param int $rootId The file ID of the ancestor to base the search on.
105+
* @param int $lastFileId The last processed file ID. Only files with a higher ID will be included. Defaults to 0.
106+
* @param array $mimeTypes An array of mime types to filter the results. If empty, no mime type filtering will be applied.
107+
* @param bool $endToEndEncrypted Whether to include EndToEndEncrypted files
108+
* @param bool $serverSideEncrypted Whether to include ServerSideEncrypted files
109+
* @param int $maxResults The maximum number of results to retrieve. If set to 0, all matching files will be retrieved.
110+
* @return \Generator A generator yielding matching files as cache entries.
111+
* @throws Exception
112+
*/
113+
public function getByAncestorInStorage(int $storageId, int $rootId, int $lastFileId = 0, array $mimeTypes = [], bool $endToEndEncrypted = true, bool $serverSideEncrypted = true, int $maxResults = 100): \Generator {
114+
$qb = $this->getQuery();
115+
$qb->selectFileCache();
116+
$qb->andWhere($qb->expr()->eq('filecache.fileid', $qb->createNamedParameter($rootId, IQueryBuilder::PARAM_INT)));
117+
$result = $qb->executeQuery();
118+
/** @var array{path:string}|false $root */
119+
$root = $result->fetch();
120+
$result->closeCursor();
121+
122+
if ($root === false) {
123+
throw new Exception('Could not fetch storage root');
124+
}
125+
126+
$qb = $this->getQuery();
127+
128+
$path = $root['path'] === '' ? '' : $root['path'] . '/';
129+
130+
$qb->select('*')
131+
->from('filecache', 'filecache')
132+
->andWhere($qb->expr()->like('filecache.path', $qb->createNamedParameter($path . '%')))
133+
->andWhere($qb->expr()->eq('filecache.storage', $qb->createNamedParameter($storageId)))
134+
->andWhere($qb->expr()->gt('filecache.fileid', $qb->createNamedParameter($lastFileId)));
135+
136+
if (!$endToEndEncrypted) {
137+
$qb->innerJoin('filecache', 'filecache', 'p', $qb->expr()->eq('filecache.parent', 'p.fileid'));
138+
$qb->andWhere($qb->expr()->eq('p.encrypted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
139+
}
140+
141+
if (!$serverSideEncrypted) {
142+
$qb->andWhere($qb->expr()->eq('filecache.encrypted', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
143+
}
144+
145+
if (count($mimeTypes) > 0) {
146+
$qb->andWhere($qb->expr()->in('filecache.mimetype', $qb->createNamedParameter($mimeTypes, IQueryBuilder::PARAM_INT_ARRAY)));
147+
}
148+
149+
if ($maxResults !== 0) {
150+
$qb->setMaxResults($maxResults);
151+
}
152+
$files = $qb->orderBy('filecache.fileid', 'ASC')
153+
->executeQuery();
154+
155+
while (
156+
/** @var array */
157+
$row = $files->fetch()
158+
) {
159+
yield Cache::cacheEntryFromData($row, $this->mimeTypeLoader);
160+
}
161+
162+
$files->closeCursor();
163+
}
97164
}

lib/public/Files/Cache/IFileAccess.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99
namespace OCP\Files\Cache;
1010

11+
use OCP\DB\Exception;
12+
1113
/**
1214
* Low level access to the file cache.
1315
*
@@ -79,4 +81,22 @@ public function getByFileIds(array $fileIds): array;
7981
* @since 29.0.0
8082
*/
8183
public function getByFileIdsInStorage(array $fileIds, int $storageId): array;
84+
85+
/**
86+
* Retrieves files stored in a specific storage that have a specified ancestor in the file hierarchy.
87+
* Allows filtering by mime types, encryption status, and limits the number of results.
88+
*
89+
* @param int $storageId The ID of the storage to search within.
90+
* @param int $rootId The file ID of the ancestor to base the search on.
91+
* @param int $lastFileId The last processed file ID. Only files with a higher ID will be included. Defaults to 0.
92+
* @param array $mimeTypes An array of mime types to filter the results. If empty, no mime type filtering will be applied.
93+
* @param bool $endToEndEncrypted Whether to include EndToEndEncrypted files
94+
* @param bool $serverSideEncrypted Whether to include ServerSideEncrypted files
95+
* @param int $maxResults The maximum number of results to retrieve. If set to 0, all matching files will be retrieved.
96+
* @return \Generator A generator yielding matching files as cache entries.
97+
* @throws Exception
98+
*
99+
* @since 32.0.0
100+
*/
101+
public function getByAncestorInStorage(int $storageId, int $rootId, int $lastFileId = 0, array $mimeTypes = [], bool $endToEndEncrypted = true, bool $serverSideEncrypted = true, int $maxResults = 100): \Generator;
82102
}

0 commit comments

Comments
 (0)