Projektordner in der Nextcloud anlegen und je nach Status verschieben (P10)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
103
src/server/nextcloud.ts
Normal file
103
src/server/nextcloud.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
// P10: Projektordner in Hennings Nextcloud anlegen und je nach Status verschieben (WebDAV, App-Passwort).
|
||||
// Statusordner und Unterordner wie in seiner Ablage (Henning, 18./19.10.2026).
|
||||
|
||||
export interface NextcloudZugang {
|
||||
/** WebDAV-Adresse des Ordners, in dem „01 Angebot“ … „04 Archiv“ liegen */
|
||||
url: string;
|
||||
benutzer: string;
|
||||
passwort: string;
|
||||
}
|
||||
|
||||
export const STATUSORDNER: Record<string, string> = {
|
||||
anfrage: "01 Angebot",
|
||||
angebot: "01 Angebot",
|
||||
auftrag: "02 laufende Projekte",
|
||||
ausfuehrung: "02 laufende Projekte",
|
||||
abgerechnet: "02 laufende Projekte",
|
||||
gewaehrleistung: "03 Gewährl.-Projekte",
|
||||
abgeschlossen: "04 Archiv",
|
||||
};
|
||||
|
||||
export const UNTERORDNER = ["01 Auftrag", "01 Auftrag/03 Stundenlohnarb.", "02 Umsetzung", "03 Bilder"];
|
||||
|
||||
/** „P-26-020 Terrassendachsanierung“; Schrägstriche und Steuerzeichen sind in Ordnernamen nicht erlaubt. */
|
||||
export function ordnerName(nummer: string, bezeichnung: string): string {
|
||||
return `${nummer} ${bezeichnung}`.replace(/[\\/:*?"<>|\u0000-\u001f]/g, "-").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
export function zielPfad(p: { nummer: string; bezeichnung: string; status: string }): string {
|
||||
return `${STATUSORDNER[p.status] ?? STATUSORDNER.anfrage}/${ordnerName(p.nummer, p.bezeichnung)}`;
|
||||
}
|
||||
|
||||
const kodiert = (pfad: string) => pfad.split("/").map(encodeURIComponent).join("/");
|
||||
|
||||
export class Nextcloud {
|
||||
constructor(private z: NextcloudZugang, private abruf: typeof fetch = fetch) {}
|
||||
|
||||
private adresse(pfad: string): string {
|
||||
return `${this.z.url.replace(/\/+$/, "")}/${kodiert(pfad)}`;
|
||||
}
|
||||
|
||||
private async anfrage(methode: string, pfad: string, kopf: Record<string, string> = {}, body?: string): Promise<Response> {
|
||||
return this.abruf(this.adresse(pfad), {
|
||||
method: methode,
|
||||
headers: { authorization: `Basic ${btoa(`${this.z.benutzer}:${this.z.passwort}`)}`, ...kopf },
|
||||
body,
|
||||
});
|
||||
}
|
||||
|
||||
async gibtEs(pfad: string): Promise<boolean> {
|
||||
const r = await this.anfrage("PROPFIND", pfad, { depth: "0" });
|
||||
if (r.status === 404) return false;
|
||||
if (r.ok || r.status === 207) return true;
|
||||
throw new Error(`Nextcloud antwortet mit ${r.status} für ${pfad}`);
|
||||
}
|
||||
|
||||
/** Namen der Unterordner (eine Ebene) */
|
||||
async liste(pfad: string): Promise<string[]> {
|
||||
const r = await this.anfrage("PROPFIND", pfad, { depth: "1" });
|
||||
if (r.status === 404) return [];
|
||||
if (r.status !== 207) throw new Error(`Nextcloud antwortet mit ${r.status} für ${pfad}`);
|
||||
const xml = await r.text();
|
||||
const basis = new URL(this.adresse(pfad)).pathname.replace(/\/+$/, "");
|
||||
return [...xml.matchAll(/<d:href>([^<]+)<\/d:href>/gi)]
|
||||
.map((m) => decodeURIComponent(m[1]!).replace(/\/+$/, ""))
|
||||
.filter((h) => h !== decodeURIComponent(basis) && h.startsWith(decodeURIComponent(basis)))
|
||||
.map((h) => h.slice(decodeURIComponent(basis).length + 1));
|
||||
}
|
||||
|
||||
async ordner(pfad: string): Promise<void> {
|
||||
const r = await this.anfrage("MKCOL", pfad);
|
||||
if (!(r.status === 201 || r.status === 405)) throw new Error(`Ordner ${pfad} ließ sich nicht anlegen (${r.status})`);
|
||||
}
|
||||
|
||||
async verschiebe(von: string, nach: string): Promise<void> {
|
||||
const r = await this.anfrage("MOVE", von, { destination: this.adresse(nach), overwrite: "F" });
|
||||
if (!(r.status === 201 || r.status === 204)) throw new Error(`${von} ließ sich nicht nach ${nach} verschieben (${r.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bringt den Ordner eines Projekts an die richtige Stelle. `bisher` ist der zuletzt bekannte Pfad
|
||||
* (projekt.nextcloud_link). Ohne bekannten Pfad wird ein vorhandener Ordner mit derselben Nummer in
|
||||
* den Statusordnern gesucht und übernommen, sonst ein neuer mit Unterordnern angelegt.
|
||||
* Gibt den neuen Pfad zurück.
|
||||
*/
|
||||
export async function ordnerAbgleichen(nc: Nextcloud, p: { nummer: string; bezeichnung: string; status: string }, bisher: string | null): Promise<string> {
|
||||
const ziel = zielPfad(p);
|
||||
if (bisher === ziel && (await nc.gibtEs(ziel))) return ziel;
|
||||
let quelle = bisher && (await nc.gibtEs(bisher)) ? bisher : null;
|
||||
if (!quelle) {
|
||||
for (const s of new Set(Object.values(STATUSORDNER))) {
|
||||
const treffer = (await nc.liste(s)).find((n) => n === p.nummer || n.startsWith(`${p.nummer} `));
|
||||
if (treffer) { quelle = `${s}/${treffer}`; break; }
|
||||
}
|
||||
}
|
||||
await nc.ordner(ziel.split("/")[0]!);
|
||||
if (quelle && quelle !== ziel) await nc.verschiebe(quelle, ziel);
|
||||
if (!quelle) {
|
||||
await nc.ordner(ziel);
|
||||
for (const u of UNTERORDNER) await nc.ordner(`${ziel}/${u}`);
|
||||
}
|
||||
return ziel;
|
||||
}
|
||||
Reference in New Issue
Block a user