Skip to content

Commit d35773b

Browse files
authored
Merge pull request #57891 from nextcloud/carl/getDirectoryContent-mimetypefilter
perf: Allow filtering the directory content by mimetype
2 parents 55fb2f3 + edd37d3 commit d35773b

File tree

15 files changed

+146
-139
lines changed

15 files changed

+146
-139
lines changed

apps/encryption/tests/Crypto/EncryptAllTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function testEncryptUsersFiles(): void {
331331
->willReturnMap([
332332
[
333333
'/user1/files',
334-
'',
334+
null,
335335
null,
336336
[
337337
$this->createFileInfoMock(FileInfo::TYPE_FOLDER, 'foo'),
@@ -340,7 +340,7 @@ public function testEncryptUsersFiles(): void {
340340
],
341341
[
342342
'/user1/files/foo',
343-
'',
343+
null,
344344
null,
345345
[
346346
$this->createFileInfoMock(FileInfo::TYPE_FILE, 'subfile'),

apps/files/lib/Controller/ApiController.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,11 @@ public function getRecentFiles() {
247247

248248
/**
249249
* @param \OCP\Files\Node[] $nodes
250+
* @param ?non-empty-string $mimeTypeFilter limit returned content to this mimetype or mimepart
250251
* @param int $depth The depth to traverse into the contents of each node
252+
* @return FilesFolderTree
251253
*/
252-
private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0): array {
254+
private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0, ?string $mimeTypeFilter = null): array {
253255
if ($currentDepth >= $depth) {
254256
return [];
255257
}
@@ -264,14 +266,15 @@ private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0
264266
$entry = [
265267
'id' => $node->getId(),
266268
'basename' => $basename,
267-
'children' => $this->getChildren($node->getDirectoryListing(), $depth, $currentDepth + 1),
269+
'children' => $this->getChildren($node->getDirectoryListing($mimeTypeFilter), $depth, $currentDepth + 1),
268270
];
269271
$displayName = $node->getName();
270272
if ($basename !== $displayName) {
271273
$entry['displayName'] = $displayName;
272274
}
273275
$children[] = $entry;
274276
}
277+
/** @var FilesFolderTree $children */
275278
return $children;
276279
}
277280

@@ -308,8 +311,8 @@ public function getFolderTree(string $path = '/', int $depth = 1): JSONResponse
308311
'message' => $this->l10n->t('Invalid folder path'),
309312
], Http::STATUS_BAD_REQUEST);
310313
}
311-
$nodes = $node->getDirectoryListing();
312-
$tree = $this->getChildren($nodes, $depth);
314+
$nodes = $node->getDirectoryListing('httpd/unix-directory');
315+
$tree = $this->getChildren($nodes, $depth, 0, 'httpd/unix-directory');
313316
} catch (NotFoundException $e) {
314317
return new JSONResponse([
315318
'message' => $this->l10n->t('Folder not found'),

apps/files_sharing/lib/External/Cache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function get($file) {
4141
return $result;
4242
}
4343

44-
public function getFolderContentsById($fileId) {
45-
$results = parent::getFolderContentsById($fileId);
44+
public function getFolderContentsById($fileId, ?string $mimeTypeFilter = null): array {
45+
$results = parent::getFolderContentsById($fileId, $mimeTypeFilter);
4646
foreach ($results as &$file) {
4747
$file['displayname_owner'] = $this->cloudId->getDisplayId();
4848
}

lib/private/Files/Cache/Cache.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use OCP\IDBConnection;
3838
use OCP\Server;
3939
use OCP\Util;
40+
use Override;
4041
use Psr\Log\LoggerInterface;
4142

4243
/**
@@ -201,38 +202,37 @@ public static function cacheEntryFromData(array $data, IMimeTypeLoader $mimetype
201202
return new CacheEntry($normalized);
202203
}
203204

204-
/**
205-
* get the metadata of all files stored in $folder
206-
*
207-
* @param string $folder
208-
* @return ICacheEntry[]
209-
*/
210-
public function getFolderContents($folder) {
205+
#[Override]
206+
public function getFolderContents(string $folder, ?string $mimeTypeFilter = null) {
211207
$fileId = $this->getId($folder);
212-
return $this->getFolderContentsById($fileId);
208+
return $this->getFolderContentsById($fileId, $mimeTypeFilter);
213209
}
214210

215-
/**
216-
* get the metadata of all files stored in $folder
217-
*
218-
* @param int $fileId the file id of the folder
219-
* @return ICacheEntry[]
220-
*/
221-
public function getFolderContentsById($fileId) {
211+
#[Override]
212+
public function getFolderContentsById(int $fileId, ?string $mimeTypeFilter = null) {
222213
if ($fileId > -1) {
223214
$query = $this->getQueryBuilder();
224215
$query->selectFileCache()
225216
->whereParent($fileId)
226217
->whereStorageId($this->getNumericStorageId())
227218
->orderBy('name', 'ASC');
228219

220+
if ($mimeTypeFilter !== null) {
221+
$mimetype = $this->mimetypeLoader->getId($mimeTypeFilter);
222+
if (str_contains($mimeTypeFilter, '/')) {
223+
$query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimetype)));
224+
} else {
225+
$query->andWhere($query->expr()->eq('mimepart', $query->createNamedParameter($mimetype)));
226+
}
227+
}
228+
229229
$metadataQuery = $query->selectMetadata();
230230

231231
$result = $query->executeQuery();
232232
$files = $result->fetchAll();
233233
$result->closeCursor();
234234

235-
return array_map(function (array $data) use ($metadataQuery) {
235+
return array_map(function (array $data) use ($metadataQuery): ICacheEntry {
236236
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
237237
return self::cacheEntryFromData($data, $this->mimetypeLoader);
238238
}, $files);

lib/private/Files/Cache/FailedCache.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ public function __construct(
2626
) {
2727
}
2828

29-
30-
public function getNumericStorageId() {
29+
public function getNumericStorageId(): int {
3130
return -1;
3231
}
3332

34-
public function get($file) {
33+
public function get($file): false|ICacheEntry {
3534
if ($file === '') {
3635
return new CacheEntry([
3736
'fileid' => -1,
@@ -46,11 +45,11 @@ public function get($file) {
4645
}
4746
}
4847

49-
public function getFolderContents($folder) {
48+
public function getFolderContents(string $folder, ?string $mimeTypeFilter = null): array {
5049
return [];
5150
}
5251

53-
public function getFolderContentsById($fileId) {
52+
public function getFolderContentsById(int $fileId, ?string $mimeTypeFilter = null): array {
5453
return [];
5554
}
5655

@@ -63,15 +62,15 @@ public function insert($file, array $data) {
6362
public function update($id, array $data) {
6463
}
6564

66-
public function getId($file) {
65+
public function getId($file): int {
6766
return -1;
6867
}
6968

70-
public function getParentId($file) {
69+
public function getParentId($file): int {
7170
return -1;
7271
}
7372

74-
public function inCache($file) {
73+
public function inCache($file): bool {
7574
return false;
7675
}
7776

lib/private/Files/Cache/Wrapper/CacheWrapper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public function get($file) {
8989
* @param string $folder
9090
* @return ICacheEntry[]
9191
*/
92-
public function getFolderContents($folder) {
92+
public function getFolderContents(string $folder, ?string $mimeTypeFilter = null): array {
9393
// can't do a simple $this->getCache()->.... call here since getFolderContentsById needs to be called on this
9494
// and not the wrapped cache
9595
$fileId = $this->getId($folder);
96-
return $this->getFolderContentsById($fileId);
96+
return $this->getFolderContentsById($fileId, $mimeTypeFilter);
9797
}
9898

9999
/**
@@ -102,9 +102,9 @@ public function getFolderContents($folder) {
102102
* @param int $fileId the file id of the folder
103103
* @return array
104104
*/
105-
public function getFolderContentsById($fileId) {
106-
$results = $this->getCache()->getFolderContentsById($fileId);
107-
return array_map([$this, 'formatCacheEntry'], $results);
105+
public function getFolderContentsById(int $fileId, ?string $mimeTypeFilter = null) {
106+
$results = $this->getCache()->getFolderContentsById($fileId, $mimeTypeFilter);
107+
return array_map($this->formatCacheEntry(...), $results);
108108
}
109109

110110
/**

lib/private/Files/Filesystem.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,14 @@ public static function putFileInfo($path, $data) {
663663
}
664664

665665
/**
666-
* get the content of a directory
666+
* Get the content of a directory.
667667
*
668668
* @param string $directory path under datadirectory
669-
* @param string $mimetype_filter limit returned content to this mimetype or mimepart
669+
* @param ?non-empty-string $mimeTypeFilter limit returned content to this mimetype or mimepart
670670
* @return FileInfo[]
671671
*/
672-
public static function getDirectoryContent($directory, $mimetype_filter = '') {
673-
return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter);
672+
public static function getDirectoryContent($directory, ?string $mimeTypeFilter = null): array {
673+
return self::$defaultInstance->getDirectoryContent($directory, $mimeTypeFilter);
674674
}
675675

676676
/**

lib/private/Files/Node/Folder.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,11 @@ public function isSubNode($node) {
7676
return str_starts_with($node->getPath(), $this->path . '/');
7777
}
7878

79-
/**
80-
* get the content of this directory
81-
*
82-
* @return Node[]
83-
* @throws \OCP\Files\NotFoundException
84-
*/
85-
public function getDirectoryListing() {
86-
$folderContent = $this->view->getDirectoryContent($this->path, '', $this->getFileInfo(false));
79+
#[Override]
80+
public function getDirectoryListing(?string $mimetypeFilter = null): array {
81+
$folderContent = $this->view->getDirectoryContent($this->path, $mimetypeFilter, $this->getFileInfo(false));
8782

88-
return array_map(function (FileInfo $info) {
83+
return array_map(function (FileInfo $info): Node {
8984
if ($info->getMimetype() === FileInfo::MIMETYPE_FOLDER) {
9085
return new Folder($this->root, $this->view, $info->getPath(), $info, $this);
9186
} else {

lib/private/Files/Node/LazyFolder.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,8 @@ public function isSubNode($node) {
415415
return $this->__call(__FUNCTION__, func_get_args());
416416
}
417417

418-
/**
419-
* @inheritDoc
420-
*/
421-
public function getDirectoryListing() {
418+
#[Override]
419+
public function getDirectoryListing(?string $mimetypeFilter = null): array {
422420
return $this->__call(__FUNCTION__, func_get_args());
423421
}
424422

lib/private/Files/Node/NonExistingFolder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace OC\Files\Node;
99

1010
use OCP\Files\NotFoundException;
11+
use Override;
1112

1213
class NonExistingFolder extends Folder {
1314
/**
@@ -118,7 +119,8 @@ public function get($path) {
118119
throw new NotFoundException();
119120
}
120121

121-
public function getDirectoryListing() {
122+
#[Override]
123+
public function getDirectoryListing(?string $mimetypeFilter = null): never {
122124
throw new NotFoundException();
123125
}
124126

0 commit comments

Comments
 (0)