Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
'OCA\\DAV\\Connector\\Sabre\\File' => $baseDir . '/../lib/Connector/Sabre/File.php',
'OCA\\DAV\\Connector\\Sabre\\FilesPlugin' => $baseDir . '/../lib/Connector/Sabre/FilesPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\FilesReportPlugin' => $baseDir . '/../lib/Connector/Sabre/FilesReportPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\GroupableFile' => $baseDir . '/../lib/Connector/Sabre/GroupableFile.php',
'OCA\\DAV\\Connector\\Sabre\\LockPlugin' => $baseDir . '/../lib/Connector/Sabre/LockPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MaintenancePlugin' => $baseDir . '/../lib/Connector/Sabre/MaintenancePlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MtimeSanitizer' => $baseDir . '/../lib/Connector/Sabre/MtimeSanitizer.php',
Expand Down Expand Up @@ -416,6 +417,7 @@
'OCA\\DAV\\Service\\AbsenceService' => $baseDir . '/../lib/Service/AbsenceService.php',
'OCA\\DAV\\Service\\ExampleContactService' => $baseDir . '/../lib/Service/ExampleContactService.php',
'OCA\\DAV\\Service\\ExampleEventService' => $baseDir . '/../lib/Service/ExampleEventService.php',
'OCA\\DAV\\Service\\FileGroupingService' => $baseDir . '/../lib/Service/FileGroupingService.php',
'OCA\\DAV\\Settings\\Admin\\SystemAddressBookSettings' => $baseDir . '/../lib/Settings/Admin/SystemAddressBookSettings.php',
'OCA\\DAV\\Settings\\AvailabilitySettings' => $baseDir . '/../lib/Settings/AvailabilitySettings.php',
'OCA\\DAV\\Settings\\CalDAVSettings' => $baseDir . '/../lib/Settings/CalDAVSettings.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Connector\\Sabre\\File' => __DIR__ . '/..' . '/../lib/Connector/Sabre/File.php',
'OCA\\DAV\\Connector\\Sabre\\FilesPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/FilesPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\FilesReportPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/FilesReportPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\GroupableFile' => __DIR__ . '/..' . '/../lib/Connector/Sabre/GroupableFile.php',
'OCA\\DAV\\Connector\\Sabre\\LockPlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/LockPlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MaintenancePlugin' => __DIR__ . '/..' . '/../lib/Connector/Sabre/MaintenancePlugin.php',
'OCA\\DAV\\Connector\\Sabre\\MtimeSanitizer' => __DIR__ . '/..' . '/../lib/Connector/Sabre/MtimeSanitizer.php',
Expand Down Expand Up @@ -431,6 +432,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Service\\AbsenceService' => __DIR__ . '/..' . '/../lib/Service/AbsenceService.php',
'OCA\\DAV\\Service\\ExampleContactService' => __DIR__ . '/..' . '/../lib/Service/ExampleContactService.php',
'OCA\\DAV\\Service\\ExampleEventService' => __DIR__ . '/..' . '/../lib/Service/ExampleEventService.php',
'OCA\\DAV\\Service\\FileGroupingService' => __DIR__ . '/..' . '/../lib/Service/FileGroupingService.php',
'OCA\\DAV\\Settings\\Admin\\SystemAddressBookSettings' => __DIR__ . '/..' . '/../lib/Settings/Admin/SystemAddressBookSettings.php',
'OCA\\DAV\\Settings\\AvailabilitySettings' => __DIR__ . '/..' . '/../lib/Settings/AvailabilitySettings.php',
'OCA\\DAV\\Settings\\CalDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CalDAVSettings.php',
Expand Down
5 changes: 5 additions & 0 deletions apps/dav/lib/Connector/Sabre/FilesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class FilesPlugin extends ServerPlugin {
public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
public const LAST_ACTIVITY_PROPERTYNAME = '{http://nextcloud.org/ns}last_activity';
public const MIME_TYPE_GROUP = '{http://nextcloud.org/ns}mime_type_group';
public const SHARE_NOTE = '{http://nextcloud.org/ns}note';
public const SHARE_HIDE_DOWNLOAD_PROPERTYNAME = '{http://nextcloud.org/ns}hide-download';
public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count';
Expand Down Expand Up @@ -453,6 +454,10 @@ public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node)
return $node->getFileInfo()->getLastActivity();
});

$propFind->handle(self::MIME_TYPE_GROUP, function () use ($node) {
return $node instanceof GroupableFile ? $node->getGroup() : null;
});

foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) {
$propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, fn () => $metadataValue);
}
Expand Down
37 changes: 37 additions & 0 deletions apps/dav/lib/Connector/Sabre/GroupableFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\DAV\Connector\Sabre;

use OC\Files\View;
use OCA\DAV\Connector\Sabre\File;
use OCP\Files\FileInfo;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Share\IManager;

class GroupableFile extends File {

public function __construct(
View $view,
FileInfo $info,
?IManager $shareManager = null,
?IRequest $request = null,
?IL10N $l10n = null,
protected ?int $group = null,
) {
parent::__construct($view, $info, $shareManager, $request, $l10n);
}

public function getGroup(): ?int {
return $this->group;
}

public function setGroup(int $group): void {
$this->group = $group;
}
}
139 changes: 123 additions & 16 deletions apps/dav/lib/Files/FileSearchBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\View;
use OCA\Files\AppInfo\Application;
use OCA\Files\ConfigLexicon;
use OCA\DAV\Connector\Sabre\CachingTree;
use OCA\DAV\Connector\Sabre\Directory;
use OCA\DAV\Connector\Sabre\File;
use OCA\DAV\Connector\Sabre\FilesPlugin;
use OCA\DAV\Connector\Sabre\GroupableFile;
use OCA\DAV\Connector\Sabre\Server;
use OCA\DAV\Connector\Sabre\TagsPlugin;
use OCA\DAV\Service\FileGroupingService;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
Expand All @@ -31,6 +35,7 @@
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\FilesMetadata\IMetadataQuery;
use OCP\FilesMetadata\Model\IMetadataValueWrapper;
use OCP\IAppConfig;
use OCP\IUser;
use OCP\Share\IManager;
use Sabre\DAV\Exception\NotFound;
Expand All @@ -54,6 +59,8 @@ public function __construct(
private IManager $shareManager,
private View $view,
private IFilesMetadataManager $filesMetadataManager,
private FileGroupingService $fileGroupingService,
private IAppConfig $appConfig,
) {
}

Expand Down Expand Up @@ -93,6 +100,7 @@ public function getPropertyDefinitionsForScope(string $href, ?string $path): arr
new SearchPropertyDefinition('{DAV:}creationdate', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
new SearchPropertyDefinition('{http://nextcloud.org/ns}upload_time', true, true, true, SearchPropertyDefinition::DATATYPE_DATETIME),
new SearchPropertyDefinition('{http://nextcloud.org/ns}last_activity', true, false, true, SearchPropertyDefinition::DATATYPE_DATETIME),
new SearchPropertyDefinition(FilesPlugin::MIME_TYPE_GROUP, true, false, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
new SearchPropertyDefinition(FilesPlugin::SIZE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
new SearchPropertyDefinition(TagsPlugin::FAVORITE_PROPERTYNAME, true, true, true, SearchPropertyDefinition::DATATYPE_BOOLEAN),
new SearchPropertyDefinition(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, true, true, false, SearchPropertyDefinition::DATATYPE_NONNEGATIVE_INTEGER),
Expand Down Expand Up @@ -175,9 +183,9 @@ public function search(Query $search): array {
break;
case 1:
$scope = $search->from[0];
$folder = $this->getFolderForPath($scope->path);
$searchTarget = $this->getFolderForPath($scope->path);
$query = $this->transformQuery($search);
$results = $folder->search($query);
$results = $searchTarget->search($query);
break;
default:
$scopes = [];
Expand Down Expand Up @@ -212,37 +220,58 @@ public function search(Query $search): array {

$scopeOperators = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_OR, $scopes);
$query = $this->transformQuery($search, $scopeOperators);
$userFolder = $this->rootFolder->getUserFolder($this->user->getUID());
$results = $userFolder->search($query);
$searchTarget = $this->rootFolder->getUserFolder($this->user->getUID());
$results = $searchTarget->search($query);
}

$groupRecentFilesEnabled = $this->appConfig->getValueBool(Application::APP_ID, ConfigLexicon::GROUP_RECENT_FILES, false);
$mimeTypeGroupRequested = false;
if ($groupRecentFilesEnabled) {
$mimeTypeGroupRequested = $this->isPropertyRequested($search, FilesPlugin::MIME_TYPE_GROUP);
}
$shouldGroupFiles = $groupRecentFilesEnabled && $mimeTypeGroupRequested;

/** @var SearchResult[] $nodes */
$nodes = array_map(function (Node $node) {
if ($node instanceof Folder) {
$davNode = new Directory($this->view, $node, $this->tree, $this->shareManager);
} else {
$davNode = new File($this->view, $node, $this->shareManager);
}
$path = $this->getHrefForNode($node);
$this->tree->cacheNode($davNode, $path);
return new SearchResult($davNode, $path);
}, $results);
$nodes = $this->mapNodesToSearchResults($results, $shouldGroupFiles);

if ($shouldGroupFiles) {
$nodes = $this->groupNodesAndFetchMoreIfNeeded($search, $query, $results, $nodes, $searchTarget);
}

if (!$query->limitToHome()) {
if (!$query->limitToHome() && !$shouldGroupFiles) {
// Sort again, since the result from multiple storages is appended and not sorted
usort($nodes, function (SearchResult $a, SearchResult $b) use ($search) {
return $this->sort($a, $b, $search->orderBy);
});
}

// If a limit is provided use only return that number of files
if ($search->limit->maxResults !== 0) {
if ($search->limit->maxResults !== 0 && !$shouldGroupFiles) {
$nodes = \array_slice($nodes, 0, $search->limit->maxResults);
}

return $nodes;
}

/**
* @param Node[] $nodes
* @return SearchResult[]
*/
private function mapNodesToSearchResults(array $nodes, bool $shouldGroupFiles): array {
return array_map(function (Node $node) use ($shouldGroupFiles) {
if ($node instanceof Folder) {
$davNode = new Directory($this->view, $node, $this->tree, $this->shareManager);
} elseif ($shouldGroupFiles) {
$davNode = new GroupableFile($this->view, $node, $this->shareManager);
} else {
$davNode = new File($this->view, $node, $this->shareManager);
}
$path = $this->getHrefForNode($node);
$this->tree->cacheNode($davNode, $path);
return new SearchResult($davNode, $path);
}, $nodes);
}

private function sort(SearchResult $a, SearchResult $b, array $orders) {
/** @var Order $order */
foreach ($orders as $order) {
Expand Down Expand Up @@ -572,4 +601,82 @@ private function extractWhereValue(Operator &$operator, string $propertyName, st
return null;
}
}

private function isPropertyRequested(Query $search, string $propertyName): bool {
foreach ($search->select as $property) {
if ($property->name === $propertyName) {
return true;
}
}
return false;
}

/**
* @param Node[] $results
* @param SearchResult[] $nodes
* @return SearchResult[]
*/
private function groupNodesAndFetchMoreIfNeeded(Query $search, ISearchQuery $query, array $results, array $nodes, Folder $searchTarget): array {
$mimeTypes = $this->appConfig->getValueArray(Application::APP_ID, ConfigLexicon::RECENT_FILES_GROUP_MIME_TYPES, []);
$minGroupSize = $this->appConfig->getValueInt(Application::APP_ID, ConfigLexicon::RECENT_FILES_GROUP_MIN_GROUP_SIZE, 2);
$timespanMinutes = $this->appConfig->getValueInt(Application::APP_ID, ConfigLexicon::RECENT_FILES_GROUP_TIMESPAN_MINUTES, 2);
$collapsedItemsLimit = $this->appConfig->getValueInt(Application::APP_ID, ConfigLexicon::RECENT_FILES_GROUP_COLLAPSED_ITEMS_LIMIT, 25);

[$nodes, $collapsedCount] = $this->fileGroupingService->setGroupOnNodes($nodes, $mimeTypes, $minGroupSize, $timespanMinutes);

$queryLimit = $query->getLimit();
$queryOffset = $query->getOffset();
$maxExtraFetches = 5;

for ($i = 0; $i < $maxExtraFetches; $i++) {
$lastNode = $nodes[array_key_last($nodes)] ?? null;
$lastGroupMightContinue = $lastNode !== null && $this->fileGroupingService->isNodeGroupable($lastNode, $mimeTypes);

$needsMore = count($results) === $queryLimit && ($collapsedCount < $collapsedItemsLimit || $lastGroupMightContinue);
if (!$needsMore) {
break;
}

$queryOffset += $queryLimit;
$search->limit->firstResult = $queryOffset;
$query = $this->transformQuery($search);
$results = $searchTarget->search($query);

if (empty($results)) {
break;
}

$extraNodes = $this->mapNodesToSearchResults($results, true);
$nodes = array_merge($nodes, $extraNodes);
[$nodes, $collapsedCount] = $this->fileGroupingService->setGroupOnNodes($nodes, $mimeTypes, $minGroupSize, $timespanMinutes);
}

return $this->sliceGroupableFilesSearchResult($nodes, $collapsedItemsLimit);
}

private function sliceGroupableFilesSearchResult(array $nodes, int $limit): array {
$result = [];
$count = 0;
$seenGroups = [];

foreach ($nodes as $searchResult) {
$node = $searchResult->node;
$group = ($node instanceof GroupableFile) ? $node->getGroup() : null;

if ($group === null) {
$count++;
} elseif (!isset($seenGroups[$group])) {
$seenGroups[$group] = true;
$count++;
}

if ($count > $limit) {
return $result;
}

$result[] = $searchResult;
}

return $result;
}
}
5 changes: 4 additions & 1 deletion apps/dav/lib/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
use OCA\DAV\Upload\ChunkingPlugin;
use OCA\DAV\Upload\ChunkingV2Plugin;
use OCA\DAV\Upload\UploadAutoMkcolPlugin;
use OCA\DAV\Service\FileGroupingService;
use OCA\Theming\ThemingDefaults;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
Expand Down Expand Up @@ -381,7 +382,9 @@ public function __construct(
\OCP\Server::get(IRootFolder::class),
$shareManager,
$view,
\OCP\Server::get(IFilesMetadataManager::class)
\OCP\Server::get(IFilesMetadataManager::class),
\OCP\Server::get(FileGroupingService::class),
\OCP\Server::get(IAppConfig::class),
));
$this->server->addPlugin(
new BulkUploadPlugin(
Expand Down
Loading
Loading