mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-09 09:04:18 +01:00
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { ClassicPreset } from 'rete';
|
|
import { BaseWorkflowNode } from './base-node';
|
|
import { pdfSocket } from '../sockets';
|
|
import type { SocketData } from '../types';
|
|
import { requirePdfInput, processBatch } from '../types';
|
|
import { wfError } from '../errors';
|
|
import {
|
|
TIMESTAMP_TSA_PRESETS,
|
|
isAllowedTsaUrl,
|
|
} from '../../config/timestamp-tsa.js';
|
|
import { timestampPdf } from '../../logic/digital-sign-pdf.js';
|
|
import { loadPdfDocument } from '../../utils/load-pdf-document.js';
|
|
|
|
export class TimestampNode extends BaseWorkflowNode {
|
|
readonly category = 'Secure PDF' as const;
|
|
readonly icon = 'ph-clock';
|
|
readonly description = 'Add an RFC 3161 document timestamp';
|
|
|
|
constructor() {
|
|
super('Timestamp');
|
|
this.addInput('pdf', new ClassicPreset.Input(pdfSocket, 'PDF'));
|
|
this.addOutput(
|
|
'pdf',
|
|
new ClassicPreset.Output(pdfSocket, 'Timestamped PDF')
|
|
);
|
|
this.addControl(
|
|
'tsaUrl',
|
|
new ClassicPreset.InputControl('text', {
|
|
initial: TIMESTAMP_TSA_PRESETS[0].url,
|
|
})
|
|
);
|
|
}
|
|
|
|
getTsaPresets(): { label: string; url: string }[] {
|
|
return TIMESTAMP_TSA_PRESETS;
|
|
}
|
|
|
|
sanitizeControlValue(key: string, value: unknown): unknown {
|
|
const sanitized = super.sanitizeControlValue(key, value);
|
|
if (key === 'tsaUrl' && !isAllowedTsaUrl(sanitized)) {
|
|
return TIMESTAMP_TSA_PRESETS[0].url;
|
|
}
|
|
return sanitized;
|
|
}
|
|
|
|
async data(
|
|
inputs: Record<string, SocketData[]>
|
|
): Promise<Record<string, SocketData>> {
|
|
const pdfInputs = requirePdfInput(inputs, 'Timestamp');
|
|
|
|
const tsaUrlCtrl = this.controls['tsaUrl'] as
|
|
| ClassicPreset.InputControl<'text'>
|
|
| undefined;
|
|
const tsaUrl = tsaUrlCtrl?.value || TIMESTAMP_TSA_PRESETS[0].url;
|
|
|
|
return {
|
|
pdf: await processBatch(pdfInputs, async (input) => {
|
|
let bytes: Uint8Array;
|
|
try {
|
|
bytes = await timestampPdf(input.bytes, tsaUrl);
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
throw new Error(
|
|
wfError('timestampFailed', { tsa: tsaUrl, message }),
|
|
{ cause: err }
|
|
);
|
|
}
|
|
const document = await loadPdfDocument(bytes);
|
|
|
|
return {
|
|
type: 'pdf',
|
|
document,
|
|
bytes,
|
|
filename: input.filename.replace(/\.pdf$/i, '_timestamped.pdf'),
|
|
};
|
|
}),
|
|
};
|
|
}
|
|
}
|