224 lines
7.0 KiB
JavaScript
224 lines
7.0 KiB
JavaScript
let csrf = "";
|
|
let scope = null;
|
|
let epoch = 0;
|
|
let dirty = false;
|
|
|
|
export function sessionState() {
|
|
return { csrf, scope, epoch };
|
|
}
|
|
|
|
export function applySession(data) {
|
|
csrf = data.csrf_token || "";
|
|
scope = data.scope || null;
|
|
epoch = data.epoch || 0;
|
|
dirty = false;
|
|
return data;
|
|
}
|
|
|
|
export function clearSession() {
|
|
csrf = "";
|
|
scope = null;
|
|
epoch = 0;
|
|
dirty = false;
|
|
}
|
|
|
|
export function markDirty(value) {
|
|
dirty = value;
|
|
}
|
|
|
|
export function isDirty() {
|
|
return dirty;
|
|
}
|
|
|
|
function scopeHeaders() {
|
|
if (!scope) return {};
|
|
return { "X-Scope-Kind": scope.kind, "X-Scope-Id": scope.id };
|
|
}
|
|
|
|
export async function api(path, { method = "GET", body, form } = {}) {
|
|
const started = epoch;
|
|
const headers = { ...scopeHeaders() };
|
|
if (csrf && method !== "GET" && method !== "HEAD") {
|
|
headers["X-CSRF-Token"] = csrf;
|
|
}
|
|
let payload;
|
|
if (form) {
|
|
payload = form;
|
|
} else if (body !== undefined) {
|
|
headers["Content-Type"] = "application/json";
|
|
payload = JSON.stringify(body);
|
|
}
|
|
const response = await fetch(path, { method, headers, body: payload, credentials: "include" });
|
|
if (started !== epoch) {
|
|
const error = new Error("Der Datenraum wurde gewechselt.");
|
|
error.status = 0;
|
|
throw error;
|
|
}
|
|
if (response.status === 204) return null;
|
|
const text = await response.text();
|
|
const data = text ? JSON.parse(text) : null;
|
|
if (!response.ok) {
|
|
const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen.");
|
|
error.status = response.status;
|
|
error.code = data?.code;
|
|
if (response.status === 401) clearSession();
|
|
if (data?.code === "scope_changed" && typeof window !== "undefined") {
|
|
window.dispatchEvent(new Event("tsumugu:scope-changed"));
|
|
}
|
|
throw error;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
function mailQuery(context) {
|
|
const params = new URLSearchParams();
|
|
if (context?.kind && context?.id) {
|
|
params.set("context_kind", context.kind);
|
|
params.set("context_id", context.id);
|
|
}
|
|
return params;
|
|
}
|
|
|
|
export async function fetchMailPart(elementId, context, suffix) {
|
|
const response = await fetch(`/api/v1/external-refs/${elementId}/mail/${suffix}?${mailQuery(context)}`, {
|
|
credentials: "include",
|
|
headers: scopeHeaders(),
|
|
});
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => null);
|
|
const fallback = suffix === "original" ? "Die vollständige E-Mail ist nicht verfügbar." : "Die Anlage ist nicht verfügbar.";
|
|
throw new Error(data?.detail || fallback);
|
|
}
|
|
return response.blob();
|
|
}
|
|
|
|
// The name the server gave the file (RFC 5987 form first), so a saved file keeps its real name.
|
|
export function nameFromDisposition(header, fallback) {
|
|
const text = header || "";
|
|
const star = /filename\*\s*=\s*UTF-8''([^;]+)/i.exec(text);
|
|
if (star) {
|
|
try {
|
|
return decodeURIComponent(star[1].trim());
|
|
} catch {
|
|
/* fall through to the plain name */
|
|
}
|
|
}
|
|
const plain = /filename\s*=\s*"([^"]+)"/i.exec(text) || /filename\s*=\s*([^;]+)/i.exec(text);
|
|
return plain ? plain[1].trim() : fallback;
|
|
}
|
|
|
|
export function saveBlob(blob, filename) {
|
|
const url = URL.createObjectURL(blob);
|
|
const link = document.createElement("a");
|
|
link.href = url;
|
|
link.download = filename;
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
link.remove();
|
|
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
|
}
|
|
|
|
// Saves a document to the device. The server checks the download right of the role.
|
|
export async function saveDocument(path, context, fallbackName) {
|
|
const params = mailQuery(context);
|
|
params.set("save", "true");
|
|
const response = await fetch(`${path}?${params}`, { credentials: "include", headers: scopeHeaders() });
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => null);
|
|
throw new Error(data?.detail || "Die Datei ist nicht verfügbar.");
|
|
}
|
|
saveBlob(await response.blob(), nameFromDisposition(response.headers.get("Content-Disposition"), fallbackName));
|
|
}
|
|
|
|
export async function downloadMailPart(elementId, context, suffix, filename) {
|
|
await saveDocument(`/api/v1/external-refs/${elementId}/mail/${suffix}`, context, filename || "nachricht.eml");
|
|
}
|
|
|
|
export async function fetchExternal(elementId, context) {
|
|
const params = new URLSearchParams();
|
|
if (context?.kind && context?.id) {
|
|
params.set("context_kind", context.kind);
|
|
params.set("context_id", context.id);
|
|
}
|
|
const response = await fetch(`/api/v1/external-refs/${elementId}/content?${params}`, {
|
|
credentials: "include",
|
|
headers: scopeHeaders(),
|
|
});
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => null);
|
|
throw new Error(data?.detail || "Die Quelle stellt die Datei nicht bereit.");
|
|
}
|
|
return response.blob();
|
|
}
|
|
|
|
export async function fetchPaperless(elementId, context) {
|
|
return fetchExternal(elementId, context);
|
|
}
|
|
|
|
export async function fetchFile(elementId, context) {
|
|
const params = new URLSearchParams();
|
|
if (context?.kind && context?.id) {
|
|
params.set("context_kind", context.kind);
|
|
params.set("context_id", context.id);
|
|
}
|
|
const response = await fetch(`/api/v1/files/${elementId}/content?${params}`, {
|
|
credentials: "include",
|
|
headers: scopeHeaders(),
|
|
});
|
|
if (!response.ok) throw new Error("Die Datei ist nicht verfügbar.");
|
|
return response.blob();
|
|
}
|
|
|
|
export async function downloadFile(elementId, context, filename) {
|
|
await saveDocument(`/api/v1/files/${elementId}/content`, context, filename || "datei");
|
|
}
|
|
|
|
export async function fetchBlob(path) {
|
|
const response = await fetch(path, { credentials: "include", headers: scopeHeaders() });
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => null);
|
|
const error = new Error(data?.detail || "Die Vorschau ist nicht verfügbar.");
|
|
error.status = response.status;
|
|
throw error;
|
|
}
|
|
return response.blob();
|
|
}
|
|
|
|
export async function downloadPost(path, body, fallbackName) {
|
|
const headers = { ...scopeHeaders(), "Content-Type": "application/json" };
|
|
if (csrf) headers["X-CSRF-Token"] = csrf;
|
|
const response = await fetch(path, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify(body),
|
|
credentials: "include",
|
|
});
|
|
if (!response.ok) {
|
|
const data = await response.json().catch(() => null);
|
|
const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen.");
|
|
error.status = response.status;
|
|
throw error;
|
|
}
|
|
const blob = await response.blob();
|
|
const disposition = response.headers.get("Content-Disposition") || "";
|
|
const extended = disposition.match(/filename\*=UTF-8''([^;]+)/i);
|
|
const plain = disposition.match(/filename="([^"]+)"/);
|
|
const filename = extended ? decodeURIComponent(extended[1]) : (plain && plain[1]) || fallbackName || "uebergabe.zip";
|
|
const url = URL.createObjectURL(blob);
|
|
const link = document.createElement("a");
|
|
link.href = url;
|
|
link.download = filename;
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
link.remove();
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
|
|
export function textToDocument(text) {
|
|
return { version: 1, blocks: [{ type: "markdown", text: text || "" }] };
|
|
}
|
|
|
|
export function documentToText(document) {
|
|
return (document?.blocks || []).map((block) => block.text || "").join("\n");
|
|
}
|