Fix search: add username param, fix scope path, add sortBy property mapping

- buildSearchRequest now accepts username parameter for correct scope
- Fix double-slash in scope path when path='/'
- Map sortBy names to correct XML namespaces (size→oc:size, etc.)
- Sort property mapping for: name, size, lastModified, created, favorite
This commit is contained in:
2026-05-11 14:54:47 +02:00
parent 73d96b9902
commit ce477546a1
+23 -8
View File
@@ -136,7 +136,7 @@ export function buildQuotaBody(): string {
} }
/** SEARCH request generica (rfc5323) — costruisce dinamicamente il where-clause */ /** SEARCH request generica (rfc5323) — costruisce dinamicamente il where-clause */
export function buildSearchRequest(options: SearchOptions): string { export function buildSearchRequest(options: SearchOptions, username: string): string {
const filters: string[] = []; const filters: string[] = [];
if (options.query) { if (options.query) {
@@ -193,13 +193,21 @@ export function buildSearchRequest(options: SearchOptions): string {
whereClause = ` <d:where>\n <d:and>\n${filters.join("\n")}\n </d:and>\n </d:where>`; whereClause = ` <d:where>\n <d:and>\n${filters.join("\n")}\n </d:and>\n </d:where>`;
} }
const scopePath = normalizePath(options.path || "/"); const scopePath = normalizePath(options.path || "/").replace(/^\//, "");
const scope = `/files/${escapeXml(scopePath)}`; const scope = `/files/${escapeXml(username)}/${escapeXml(scopePath)}`;
const orderby = options.sortBy const SORT_PROPERTY_MAP: Record<string, string> = {
name: "displayname",
size: "oc:size",
lastModified: "getlastmodified",
created: "nc:creation_time",
favorite: "oc:favorite",
};
const sortProp = options.sortBy ? SORT_PROPERTY_MAP[options.sortBy] || `d:${escapeXml(options.sortBy)}` : null;
const orderby = sortProp
? ` <d:orderby> ? ` <d:orderby>
<d:order> <d:order>
<d:prop><d:${escapeXml(options.sortBy)} /></d:prop> <d:prop><${sortProp} /></d:prop>
<d:${options.sortOrder === "desc" ? "descending" : "ascending"} /> <d:${options.sortOrder === "desc" ? "descending" : "ascending"} />
</d:order> </d:order>
</d:orderby>` </d:orderby>`
@@ -427,9 +435,16 @@ function decodeXmlText(value: string): string {
function resolveRelativePathFromHref(href: string, basePath: string): string { function resolveRelativePathFromHref(href: string, basePath: string): string {
const decoded = decodeURIComponent(href); const decoded = decodeURIComponent(href);
// basePath is like /remote.php/dav/files/user/
if (decoded.startsWith(basePath)) { // Extract username from href: /remote.php/dav/files/{username}/...
return decoded.slice(basePath.length) || "/"; const match = decoded.match(/^\/remote\.php\/dav\/files\/([^\/]+)/);
if (!match) return decoded;
const username = match[1];
const davBase = `/remote.php/dav/files/${username}`;
if (decoded.startsWith(davBase)) {
return decoded.slice(davBase.length) || "/";
} }
return decoded; return decoded;
} }