Skip to content

Commit ec2a77e

Browse files
committed
perf: Allow filtering the directory content by mimetype
Signed-off-by: Carl Schwan <[email protected]>
1 parent c6c11d4 commit ec2a77e

File tree

12 files changed

+60
-57
lines changed

12 files changed

+60
-57
lines changed

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 = '') {
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,21 +216,30 @@ public function getFolderContents($folder) {
216216
* @param int $fileId the file id of the folder
217217
* @return ICacheEntry[]
218218
*/
219-
public function getFolderContentsById($fileId) {
219+
public function getFolderContentsById(int $fileId, string $mimeTypeFilter = '') {
220220
if ($fileId > -1) {
221221
$query = $this->getQueryBuilder();
222222
$query->selectFileCache()
223223
->whereParent($fileId)
224224
->whereStorageId($this->getNumericStorageId())
225225
->orderBy('name', 'ASC');
226226

227+
if (!empty($mimeTypeFilter)) {
228+
$mimetype = $this->mimetypeLoader->getId($mimeTypeFilter);
229+
if (str_contains($mimeTypeFilter, '/')) {
230+
$query->andWhere($query->expr()->eq('mimetype', $query->createNamedParameter($mimetype)));
231+
} else {
232+
$query->andWhere($query->expr()->eq('mimepart', $query->createNamedParameter($mimetype)));
233+
}
234+
}
235+
227236
$metadataQuery = $query->selectMetadata();
228237

229238
$result = $query->executeQuery();
230239
$files = $result->fetchAll();
231240
$result->closeCursor();
232241

233-
return array_map(function (array $data) use ($metadataQuery) {
242+
return array_map(function (array $data) use ($metadataQuery): ICacheEntry {
234243
$data['metadata'] = $metadataQuery->extractMetadata($data)->asArray();
235244
return self::cacheEntryFromData($data, $this->mimetypeLoader);
236245
}, $files);

lib/private/Files/Cache/FailedCache.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,20 @@
1919
* Storage placeholder to represent a missing precondition, storage unavailable
2020
*/
2121
class FailedCache implements ICache {
22-
/** @var bool whether to show the failed storage in the ui */
23-
private $visible;
2422

2523
/**
26-
* FailedCache constructor.
27-
*
28-
* @param bool $visible
24+
* @param bool $visible Whether to show the failed storage in the ui
2925
*/
30-
public function __construct($visible = true) {
31-
$this->visible = $visible;
26+
public function __construct(
27+
private bool $visible = true,
28+
) {
3229
}
3330

34-
35-
public function getNumericStorageId() {
31+
public function getNumericStorageId(): int {
3632
return -1;
3733
}
3834

39-
public function get($file) {
35+
public function get($file): false|ICacheEntry {
4036
if ($file === '') {
4137
return new CacheEntry([
4238
'fileid' => -1,
@@ -51,11 +47,11 @@ public function get($file) {
5147
}
5248
}
5349

54-
public function getFolderContents($folder) {
50+
public function getFolderContents($folder): array {
5551
return [];
5652
}
5753

58-
public function getFolderContentsById($fileId) {
54+
public function getFolderContentsById(int $fileId, string $mimeTypeFilter = ''): array {
5955
return [];
6056
}
6157

@@ -68,15 +64,15 @@ public function insert($file, array $data) {
6864
public function update($id, array $data) {
6965
}
7066

71-
public function getId($file) {
67+
public function getId($file): int {
7268
return -1;
7369
}
7470

75-
public function getParentId($file) {
71+
public function getParentId($file): int {
7672
return -1;
7773
}
7874

79-
public function inCache($file) {
75+
public function inCache($file): bool {
8076
return false;
8177
}
8278

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ public function getFolderContents($folder) {
105105
* @param int $fileId the file id of the folder
106106
* @return array
107107
*/
108-
public function getFolderContentsById($fileId) {
109-
$results = $this->getCache()->getFolderContentsById($fileId);
110-
return array_map([$this, 'formatCacheEntry'], $results);
108+
public function getFolderContentsById(int $fileId, string $mimeTypeFilter = '') {
109+
$results = $this->getCache()->getFolderContentsById($fileId, $mimeTypeFilter);
110+
return array_map($this->formatCacheEntry(...), $results);
111111
}
112112

113113
/**

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 = ''): 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 = ''): 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 = ''): array {
122124
throw new NotFoundException();
123125
}
124126

lib/private/Files/View.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,10 +1471,10 @@ public function addSubMounts(FileInfo $info, $extOnly = false): void {
14711471
* get the content of a directory
14721472
*
14731473
* @param string $directory path under datadirectory
1474-
* @param string $mimetype_filter limit returned content to this mimetype or mimepart
1474+
* @param string $mimeTypeFilter limit returned content to this mimetype or mimepart
14751475
* @return FileInfo[]
14761476
*/
1477-
public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Files\FileInfo $directoryInfo = null) {
1477+
public function getDirectoryContent(string $directory, string $mimeTypeFilter = '', ?\OCP\Files\FileInfo $directoryInfo = null) {
14781478
$this->assertPathLength($directory);
14791479
if (!Filesystem::isValidPath($directory)) {
14801480
return [];
@@ -1506,7 +1506,7 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
15061506
}
15071507

15081508
$folderId = $data->getId();
1509-
$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
1509+
$contents = $cache->getFolderContentsById($folderId, $mimeTypeFilter);
15101510

15111511
$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
15121512
$permissionsMask = ~\OCP\Constants::PERMISSION_SHARE;
@@ -1632,12 +1632,12 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
16321632
}
16331633
}
16341634

1635-
if ($mimetype_filter) {
1636-
$files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) {
1637-
if (strpos($mimetype_filter, '/')) {
1638-
return $file->getMimetype() === $mimetype_filter;
1635+
if ($mimeTypeFilter) {
1636+
$files = array_filter($files, function (FileInfo $file) use ($mimeTypeFilter) {
1637+
if (strpos($mimeTypeFilter, '/')) {
1638+
return $file->getMimetype() === $mimeTypeFilter;
16391639
} else {
1640-
return $file->getMimePart() === $mimetype_filter;
1640+
return $file->getMimePart() === $mimeTypeFilter;
16411641
}
16421642
});
16431643
}

lib/private/Lockdown/Filesystem/NullCache.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
use OCP\Files\Search\ISearchQuery;
1818

1919
class NullCache implements ICache {
20-
public function getNumericStorageId() {
20+
public function getNumericStorageId(): int {
2121
return -1;
2222
}
2323

24-
public function get($file) {
24+
public function get($file): false|ICacheEntry {
2525
if ($file !== '') {
2626
return false;
2727
}
@@ -41,23 +41,23 @@ public function get($file) {
4141
]);
4242
}
4343

44-
public function getFolderContents($folder) {
44+
public function getFolderContents($folder): array {
4545
return [];
4646
}
4747

48-
public function getFolderContentsById($fileId) {
48+
public function getFolderContentsById(int $fileId, string $mimeTypeFilter = ''): array {
4949
return [];
5050
}
5151

52-
public function put($file, array $data) {
52+
public function put($file, array $data): never {
5353
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
5454
}
5555

56-
public function insert($file, array $data) {
56+
public function insert($file, array $data): never {
5757
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
5858
}
5959

60-
public function update($id, array $data) {
60+
public function update($id, array $data): never {
6161
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
6262
}
6363

0 commit comments

Comments
 (0)