Skip to content

Commit 9741f5f

Browse files
committed
perf: Allow filtering the directory content by mimetype
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 935cd29 commit 9741f5f

15 files changed

Lines changed: 125 additions & 114 deletions

File tree

apps/files/lib/Controller/ApiController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public function getRecentFiles() {
249249
* @param \OCP\Files\Node[] $nodes
250250
* @param int $depth The depth to traverse into the contents of each node
251251
*/
252-
private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0): array {
252+
private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0, string $mimeTypeFilter = ''): array {
253253
if ($currentDepth >= $depth) {
254254
return [];
255255
}
@@ -264,7 +264,7 @@ private function getChildren(array $nodes, int $depth = 1, int $currentDepth = 0
264264
$entry = [
265265
'id' => $node->getId(),
266266
'basename' => $basename,
267-
'children' => $this->getChildren($node->getDirectoryListing(), $depth, $currentDepth + 1),
267+
'children' => $this->getChildren($node->getDirectoryListing($mimeTypeFilter), $depth, $currentDepth + 1),
268268
];
269269
$displayName = $node->getName();
270270
if ($basename !== $displayName) {
@@ -308,8 +308,8 @@ public function getFolderTree(string $path = '/', int $depth = 1): JSONResponse
308308
'message' => $this->l10n->t('Invalid folder path'),
309309
], Http::STATUS_BAD_REQUEST);
310310
}
311-
$nodes = $node->getDirectoryListing();
312-
$tree = $this->getChildren($nodes, $depth);
311+
$nodes = $node->getDirectoryListing('httpd/unix-directory');
312+
$tree = $this->getChildren($nodes, $depth, 0, 'httpd/unix-directory');
313313
} catch (NotFoundException $e) {
314314
return new JSONResponse([
315315
'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
}

autotest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ function execute_tests {
309309
if [ ! -z "$USEDOCKER" ] ; then
310310
echo "Fire up the postgres docker"
311311
DOCKER_CONTAINER_ID=$(docker run -e POSTGRES_DB="$DATABASENAME" -e POSTGRES_USER="$DATABASEUSER" -e POSTGRES_PASSWORD=owncloud -d postgres)
312-
DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
312+
DATABASEHOST=$(docker inspect --format="{{ range .NetworkSettings.Networks }}{{ .IPAddress }}{{ end }}" "$DOCKER_CONTAINER_ID")
313313

314314
echo "Waiting for Postgres initialisation ..."
315315

lib/private/Files/Cache/Cache.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,30 @@ public function getFolderContents($folder) {
218218
* @param int $fileId the file id of the folder
219219
* @return ICacheEntry[]
220220
*/
221-
public function getFolderContentsById($fileId) {
221+
public function getFolderContentsById(int $fileId, ?string $mimeTypeFilter = null) {
222222
if ($fileId > -1) {
223223
$query = $this->getQueryBuilder();
224224
$query->selectFileCache()
225225
->whereParent($fileId)
226226
->whereStorageId($this->getNumericStorageId())
227227
->orderBy('name', 'ASC');
228228

229+
if ($mimeTypeFilter !== null) {
230+
$mimetype = $this->mimetypeLoader->getId($mimeTypeFilter);
231+
if (str_contains($mimeTypeFilter, '/')) {
232+
$query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimetype)));
233+
} else {
234+
$query->andWhere($query->expr()->eq('mimepart', $query->createNamedParameter($mimetype)));
235+
}
236+
}
237+
229238
$metadataQuery = $query->selectMetadata();
230239

231240
$result = $query->executeQuery();
232241
$files = $result->fetchAll();
233242
$result->closeCursor();
234243

235-
return array_map(function (array $data) use ($metadataQuery) {
244+
return array_map(function (array $data) use ($metadataQuery): ICacheEntry {
236245
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
237246
return self::cacheEntryFromData($data, $this->mimetypeLoader);
238247
}, $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($folder): 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 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 = '') {
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)