mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-12 01:33:36 +01:00
Add visual workflow builder, fix critical bugs, and add Arabic i18n support
This commit is contained in:
1 parent
36ebb3b429
commit
5d8b83e105
118 files changed
+14151
-2357
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { ClassicPreset } from 'rete';
|
||||
import { BaseWorkflowNode } from './base-node';
|
||||
import { pdfSocket } from '../sockets';
|
||||
import type { SocketData } from '../types';
|
||||
import { extractAllPdfs } from '../types';
|
||||
import { downloadFile } from '../../utils/helpers.js';
|
||||
|
||||
export class DownloadNode extends BaseWorkflowNode {
|
||||
readonly category = 'Output' as const;
|
||||
readonly icon = 'ph-download-simple';
|
||||
readonly description = 'Download as PDF or ZIP automatically';
|
||||
|
||||
constructor() {
|
||||
super('Download');
|
||||
this.addInput('pdf', new ClassicPreset.Input(pdfSocket, 'PDF', true));
|
||||
this.addControl(
|
||||
'filename',
|
||||
new ClassicPreset.InputControl('text', {
|
||||
initial: 'output',
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async data(
|
||||
inputs: Record<string, SocketData[]>
|
||||
): Promise<Record<string, SocketData>> {
|
||||
const allInputs = Object.values(inputs).flat();
|
||||
const allPdfs = extractAllPdfs(allInputs);
|
||||
if (allPdfs.length === 0)
|
||||
throw new Error('No PDFs connected to Download node');
|
||||
|
||||
const filenameControl = this.controls['filename'] as
|
||||
| ClassicPreset.InputControl<'text'>
|
||||
| undefined;
|
||||
const baseName = filenameControl?.value || 'output';
|
||||
|
||||
if (allPdfs.length === 1) {
|
||||
const filename = baseName.toLowerCase().endsWith('.pdf')
|
||||
? baseName
|
||||
: `${baseName}.pdf`;
|
||||
const blob = new Blob([new Uint8Array(allPdfs[0].bytes)], {
|
||||
type: 'application/pdf',
|
||||
});
|
||||
downloadFile(blob, filename);
|
||||
} else {
|
||||
const JSZip = (await import('jszip')).default;
|
||||
const zip = new JSZip();
|
||||
const usedNames = new Set<string>();
|
||||
for (const pdf of allPdfs) {
|
||||
let name = pdf.filename || 'document.pdf';
|
||||
if (!name.toLowerCase().endsWith('.pdf')) name += '.pdf';
|
||||
let uniqueName = name;
|
||||
let counter = 1;
|
||||
while (usedNames.has(uniqueName)) {
|
||||
uniqueName = name.replace(/\.pdf$/i, `_${counter}.pdf`);
|
||||
counter++;
|
||||
}
|
||||
usedNames.add(uniqueName);
|
||||
zip.file(uniqueName, pdf.bytes);
|
||||
}
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
const zipFilename = baseName.toLowerCase().endsWith('.zip')
|
||||
? baseName
|
||||
: `${baseName}.zip`;
|
||||
downloadFile(zipBlob, zipFilename);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user