Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
44 lines
2.9 KiB
TypeScript
44 lines
2.9 KiB
TypeScript
import { Nextcloud } from "../src/server/nextcloud.js";
|
|
|
|
/** Kleine WebDAV-Nachbildung im Speicher: Ordner und Dateien, wie Nextcloud sie beantwortet */
|
|
export function falscheNextcloud(basis = "https://cloud.test/remote.php/dav/files/henning/Projekte") {
|
|
const ordner = new Set<string>();
|
|
const dateien = new Map<string, string>();
|
|
const pfad = (u: string) => decodeURIComponent(new URL(u).pathname).replace(/\/+$/, "");
|
|
const wurzel = pfad(basis);
|
|
ordner.add(wurzel);
|
|
const abruf = (async (u: string | URL, init?: RequestInit) => {
|
|
const p = pfad(String(u));
|
|
const kopf = (init?.headers ?? {}) as Record<string, string>;
|
|
const eltern = p.slice(0, p.lastIndexOf("/"));
|
|
switch (init?.method) {
|
|
case "PROPFIND": {
|
|
if (!ordner.has(p)) return new Response(null, { status: 404 });
|
|
const direkt = (o: string) => o.startsWith(p + "/") && !o.slice(p.length + 1).includes("/");
|
|
const kinder = kopf.depth === "1" ? [...ordner].filter(direkt) : [];
|
|
const href = (o: string) => o.split("/").map(encodeURIComponent).join("/");
|
|
const hrefs = [p, ...kinder].map((o) => `<d:response><d:href>${href(o)}/</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop></d:propstat></d:response>`).join("")
|
|
+ (kopf.depth === "1" ? [...dateien].filter(([d]) => direkt(d)).map(([d, inhalt]) =>
|
|
`<d:response><d:href>${href(d)}</d:href><d:propstat><d:prop><d:resourcetype/><d:getcontentlength>${inhalt.length}</d:getcontentlength><d:getlastmodified>Mon, 19 Oct 2026 08:00:00 GMT</d:getlastmodified></d:prop></d:propstat></d:response>`).join("") : "");
|
|
return new Response(`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:">${hrefs}</d:multistatus>`, { status: 207 });
|
|
}
|
|
case "GET":
|
|
return dateien.has(p) ? new Response(dateien.get(p)!, { status: 200, headers: { "content-type": "application/pdf" } }) : new Response(null, { status: 404 });
|
|
case "MKCOL":
|
|
if (ordner.has(p)) return new Response(null, { status: 405 });
|
|
if (!ordner.has(eltern)) return new Response(null, { status: 409 });
|
|
ordner.add(p);
|
|
return new Response(null, { status: 201 });
|
|
case "MOVE": {
|
|
const ziel = pfad(kopf.destination!);
|
|
if (!ordner.has(p) || ordner.has(ziel) || !ordner.has(ziel.slice(0, ziel.lastIndexOf("/")))) return new Response(null, { status: 409 });
|
|
for (const o of [...ordner]) if (o === p || o.startsWith(p + "/")) { ordner.delete(o); ordner.add(ziel + o.slice(p.length)); }
|
|
return new Response(null, { status: 201 });
|
|
}
|
|
}
|
|
return new Response(null, { status: 400 });
|
|
}) as typeof fetch;
|
|
const relativ = () => [...ordner].filter((o) => o !== wurzel).map((o) => o.slice(wurzel.length + 1)).sort();
|
|
return { nc: new Nextcloud({ url: basis, benutzer: "henning", passwort: "geheim" }, abruf), abruf, ordner, dateien, wurzel, relativ };
|
|
}
|