Compare commits

...

1 Commits

Author SHA1 Message Date
Benjamin Gaussorgues
d7e81e7a2b feat(cache): add iterate method to list files from cache
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
2026-01-29 16:45:31 +01:00
10 changed files with 70 additions and 5 deletions

View File

@@ -48,4 +48,13 @@ class Cache extends \OC\Files\Cache\Cache {
}
return $results;
}
#[Override]
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
$displayId = $this->cloudId->getDisplayId();
foreach (parent::iterateFolderContentsById($fileI, $includeMetadata, $sortByName) as $entry) {
$entry['displayname_owner'] = $displayId;
yield $entry;
}
}
}

View File

@@ -41,7 +41,8 @@ class Helper {
$internalPath = $mount->getInternalPath($absoluteDir);
$extraData = Trashbin::getExtraData($user);
$dirContent = $storage->getCache()->getFolderContents($mount->getInternalPath($view->getAbsolutePath($dir)));
$dirId = $storage->getCache()->getId($mount->getInternalPath($view->getAbsolutePath($dir)));
$dirContent = $storage->getCache()->iterateFolderContentsById($dirId);
foreach ($dirContent as $entry) {
$entryName = $entry->getName();
$name = $entryName;

View File

@@ -35,6 +35,7 @@ use OCP\Files\Storage\IStorage;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\IDBConnection;
use OCP\Util;
use Override;
use Psr\Log\LoggerInterface;
/**
@@ -238,6 +239,33 @@ class Cache implements ICache {
return [];
}
#[Override]
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
if ($fileId < 1) {
return [];
}
$query = $this->getQueryBuilder()
->selectFileCache()
->whereParent($fileId)
->whereStorageId($this->getNumericStorageId());
if ($includeMetadata === true) {
$metadataQuery = $query->selectMetadata();
}
if ($sortByName) {
$query->orderBy('name', 'ASC');
}
$result = $query->executeQuery();
foreach ($result->iterateAssociative() as $row) {
if ($includeMetadata === true) {
$row['metadata'] = $metadataQuery->extractMetadata($row)->asArray();
}
yield self::cacheEntryFromData($row, $this->mimetypeLoader);
}
$result->closeCursor();
}
/**
* insert or update meta data for a file or folder
*

View File

@@ -59,6 +59,11 @@ class FailedCache implements ICache {
return [];
}
#[Override]
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
return [];
}
public function put($file, array $data) {
}

View File

@@ -141,7 +141,7 @@ class Watcher implements IWatcher {
* @param string $path
*/
public function cleanFolder($path) {
$cachedContent = $this->cache->getFolderContents($path);
$cachedContent = $this->cache->iterateFolderContentsById($this->cache->getId($path));
foreach ($cachedContent as $entry) {
if (!$this->storage->file_exists($entry['path'])) {
$this->cache->remove($entry['path']);

View File

@@ -110,6 +110,13 @@ class CacheWrapper extends Cache {
return array_map([$this, 'formatCacheEntry'], $results);
}
#[Override]
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
foreach ($this->getCache()->iterateFolderContentsById($fileId, $includeMetadata, $sortByName) as $entry) {
yield $this->formatCacheEntry($entry);
}
}
/**
* insert or update meta data for a file or folder
*

View File

@@ -164,7 +164,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil
}
private function rmObjects(ICacheEntry $entry): bool {
$children = $this->getCache()->getFolderContentsById($entry->getId());
$children = $this->getCache()->iterateFolderContentsById($entry->getId());
foreach ($children as $child) {
if ($child->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
if (!$this->rmObjects($child)) {
@@ -263,7 +263,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil
try {
$files = [];
$folderContents = $this->getCache()->getFolderContents($path);
$folderContents = $this->getCache()->iterateFolderContentsById($this->getCache()->getId($path), false, true);
foreach ($folderContents as $file) {
$files[] = $file['name'];
}

View File

@@ -1507,7 +1507,7 @@ class View {
}
$folderId = $data->getId();
$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
$contents = $cache->iterateFolderContentsById($folderId, false, true); // TODO Mimetype filter
$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
$permissionsMask = ~\OCP\Constants::PERMISSION_SHARE;

View File

@@ -49,6 +49,11 @@ class NullCache implements ICache {
return [];
}
#[Override]
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable {
return [];
}
public function put($file, array $data) {
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
}

View File

@@ -82,6 +82,16 @@ interface ICache {
*/
public function getFolderContentsById($fileId);
/**
* Get an iterator on files stored in folder
*
* Only returns files one level deep, no recursion
*
* @return iterable<ICacheEntry>
* @since 34.0.0
*/
public function iterateFolderContentsById(int $fileId, bool $includeMetadata = false, bool $sortByName = false): iterable;
/**
* store meta data for a file or folder
* This will automatically call either insert or update depending on if the file exists