Read Duplicati's form-encoded reports
Duplicati posts its message as a form body by default (message=<URL-encoded text>). Unwrap it (any field name, other fields beside it), take the job and operation from the 'Duplicati Backup report for <job>' line, read JSON under that line, and read the Warnings and Errors lists of the text form. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e517de35af
commit
4d653f5a80
3 files changed
+134
-16
No files matched your search
@@ -96,21 +96,60 @@ export function unpackMessages(stored: string | null): string[] {
|
||||
}
|
||||
}
|
||||
|
||||
/** The first line of Duplicati's default message — "Duplicati Backup report for Documents" — which says the operation and the job when the result itself does not. */
|
||||
export interface MessageHeader {
|
||||
operation: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure: the message itself, whichever way Duplicati wrapped it. By default Duplicati posts a **form-encoded** body (`message=<the text, URL-encoded>`, the parameter being called `message` unless
|
||||
* `send-http-message-parameter-name` says otherwise, possibly with others beside it), and the text is "Duplicati Backup report for <job>" and the result under it; `send-http-json-urls` and
|
||||
* a message set by hand may post the text or the JSON as the body as it is. Returns the text, and the header line when there is one.
|
||||
*/
|
||||
export function unwrapMessage(body: string): { text: string; header: MessageHeader | null } {
|
||||
let text = body.trim();
|
||||
// A form body is one line of key=value pairs; a result in text form starts "Key: value" and one in JSON with a brace, so neither is mistaken for it.
|
||||
if (/^[A-Za-z0-9_.-]+=/.test(text) && !/[\r\n]/.test(text)) {
|
||||
try {
|
||||
const params = new URLSearchParams(text);
|
||||
const values = [...params.values()];
|
||||
const chosen = params.get("message") ?? values.sort((a, b) => b.length - a.length)[0];
|
||||
if (chosen && chosen.trim() !== "") text = chosen.trim();
|
||||
} catch {
|
||||
// not a form after all: it is read as it came
|
||||
}
|
||||
}
|
||||
const m = /^\s*Duplicati\s+(.+?)\s+report\s+for\s+(.+?)\s*$/im.exec(text.slice(0, 400));
|
||||
return { text, header: m ? { operation: m[1].slice(0, 60), name: m[2].slice(0, 100) } : null };
|
||||
}
|
||||
|
||||
/** Pure: the lines of a list in Duplicati's text form ("Warnings: [", one message to a line, "]"), one line each. */
|
||||
function textList(text: string, key: string): string[] {
|
||||
const m = new RegExp(`^\\s*${key}\\s*:\\s*\\[\\s*\\r?\\n([\\s\\S]*?)^\\s*\\]`, "m").exec(text);
|
||||
return m ? messagesOf(m[1].split(/\r?\n/).map((l) => l.trim().replace(/,$/, "")).filter(Boolean)) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure: the report Duplicati sent — JSON (`send-http-result-output-format=Json`: the results at the top, or under `Data`, with the job's name among `Extra`), or the plain text it sends
|
||||
* by default ("ParsedResult: Success" lines). Null when it is neither. Read defensively; the exact fields have not been checked against every Duplicati version.
|
||||
* by default ("ParsedResult: Success" lines); in either case possibly wrapped as a form body, or under the "Duplicati Backup report for …" line of its default message. Null when it is none of
|
||||
* these. Read defensively; the exact fields have not been checked against every Duplicati version.
|
||||
*/
|
||||
export function parseReport(body: unknown): ParsedReport | null {
|
||||
let payload: Record<string, unknown> | null = null;
|
||||
let header: MessageHeader | null = null;
|
||||
if (typeof body === "string") {
|
||||
const trimmed = body.trim();
|
||||
if (trimmed.startsWith("{")) {
|
||||
const un = unwrapMessage(body);
|
||||
header = un.header;
|
||||
const at = un.text.startsWith("{") ? 0 : un.text.search(/^\{/m);
|
||||
if (at >= 0) {
|
||||
try {
|
||||
payload = obj(JSON.parse(trimmed));
|
||||
payload = obj(JSON.parse(un.text.slice(at)));
|
||||
} catch {
|
||||
payload = null;
|
||||
if (at === 0) return null;
|
||||
return parseText(un.text, header);
|
||||
}
|
||||
} else return parseText(trimmed);
|
||||
} else return parseText(un.text, header);
|
||||
} else payload = obj(body);
|
||||
if (!payload || Object.keys(payload).length === 0) return null;
|
||||
const data = Object.keys(obj(payload.Data)).length > 0 ? obj(payload.Data) : payload;
|
||||
@@ -122,9 +161,9 @@ export function parseReport(body: unknown): ParsedReport | null {
|
||||
const errors = list(data.Errors);
|
||||
const named = [extra["backup-name"], extra.backupName, extra.BackupName, data.BackupName].find((n) => typeof n === "string" && n.trim() !== "");
|
||||
return {
|
||||
operation: String(data.MainOperation ?? extra.OperationName ?? extra.operationname ?? extra.operation ?? "Backup"),
|
||||
operation: String(data.MainOperation ?? extra.OperationName ?? extra.operationname ?? extra.operation ?? header?.operation ?? "Backup"),
|
||||
result,
|
||||
backupName: typeof named === "string" ? named.trim().slice(0, 100) : typeof extra["backup-id"] === "string" ? `Backup ${extra["backup-id"]}`.slice(0, 100) : "Unnamed backup",
|
||||
backupName: typeof named === "string" ? named.trim().slice(0, 100) : header ? header.name : typeof extra["backup-id"] === "string" ? `Backup ${extra["backup-id"]}`.slice(0, 100) : "Unnamed backup",
|
||||
begin: asIso(data.BeginTime),
|
||||
end: asIso(data.EndTime),
|
||||
durationSeconds: parseDuration(data.Duration),
|
||||
@@ -140,13 +179,15 @@ export function parseReport(body: unknown): ParsedReport | null {
|
||||
};
|
||||
}
|
||||
|
||||
function parseText(text: string): ParsedReport | null {
|
||||
function parseText(text: string, header: MessageHeader | null = null): ParsedReport | null {
|
||||
const value = (key: string) => new RegExp(`^\\s*${key}\\s*:\\s*(.+?)\\s*$`, "im").exec(text)?.[1] ?? null;
|
||||
const result = value("ParsedResult");
|
||||
if (!result) return null;
|
||||
const name = value("backup-name");
|
||||
const name = value("backup-name") ?? header?.name ?? null;
|
||||
const warningLines = textList(text, "Warnings");
|
||||
const errorLines = textList(text, "Errors");
|
||||
return {
|
||||
operation: value("MainOperation") ?? value("OperationName") ?? "Backup",
|
||||
operation: value("MainOperation") ?? value("OperationName") ?? header?.operation ?? "Backup",
|
||||
result,
|
||||
backupName: name ? name.slice(0, 100) : "Unnamed backup",
|
||||
begin: null,
|
||||
@@ -157,10 +198,10 @@ function parseText(text: string): ParsedReport | null {
|
||||
addedFiles: asInt(value("AddedFiles")?.match(/^[\d,]+/)?.[0]),
|
||||
modifiedFiles: asInt(value("ModifiedFiles")?.match(/^[\d,]+/)?.[0]),
|
||||
deletedFiles: asInt(value("DeletedFiles")?.match(/^[\d,]+/)?.[0]),
|
||||
warnings: asInt(value("WarningsActualLength")?.match(/^[\d,]+/)?.[0]) ?? 0,
|
||||
warnings: asInt(value("WarningsActualLength")?.match(/^[\d,]+/)?.[0]) ?? warningLines.length,
|
||||
errors: asInt(value("ErrorsActualLength")?.match(/^[\d,]+/)?.[0]) ?? 0,
|
||||
message: null,
|
||||
warningMessages: [],
|
||||
message: lineOf(errorLines[0]) ?? lineOf(warningLines[0]),
|
||||
warningMessages: warningLines,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -204,9 +245,10 @@ export const MAX_SAMPLE = 600;
|
||||
|
||||
/** Pure: why a body is not a report this app can use, in a sentence, and the start of it. Read for the same two forms parseReport reads. */
|
||||
export function explainUnreadable(body: unknown): { reason: string; sample: string | null } {
|
||||
const text = typeof body === "string" ? body.trim() : "";
|
||||
const raw = typeof body === "string" ? body.trim() : "";
|
||||
const text = raw === "" ? "" : unwrapMessage(raw).text;
|
||||
if (text === "") return { reason: "the body was empty (or not text) — Duplicati should post its result as text or JSON", sample: null };
|
||||
const sample = text.slice(0, MAX_SAMPLE);
|
||||
const sample = raw.slice(0, MAX_SAMPLE);
|
||||
if (text.startsWith("{")) {
|
||||
let payload: Record<string, unknown> | null = null;
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user