2024-02-25 20:55:48 +01:00
|
|
|
import { PdfFile } from "wrappers/PdfFile";
|
2023-11-20 21:04:49 +01:00
|
|
|
import { Action } from "../../declarations/Action";
|
2024-02-05 12:15:01 -05:00
|
|
|
import Joi from "@stirling-tools/joi";
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2023-11-21 00:12:35 +01:00
|
|
|
export interface ValidationResult {
|
|
|
|
valid: boolean,
|
|
|
|
reason?: string
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:04:49 +01:00
|
|
|
export interface Progress {
|
2023-11-21 00:12:35 +01:00
|
|
|
/** A percentage between 0-1 describing the progress on the currently processed file */
|
2023-11-20 21:04:49 +01:00
|
|
|
curFileProgress: number,
|
2023-11-21 00:12:35 +01:00
|
|
|
/** A percentage between 0-1 describing the progress on all input files / operations */
|
2023-11-20 21:04:49 +01:00
|
|
|
operationProgress: number,
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Operator {
|
2023-11-27 23:35:18 +01:00
|
|
|
/** The internal name of the operator in camelCase (impose, merge, etc.) */
|
2023-11-20 21:04:49 +01:00
|
|
|
static type: string;
|
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** The Joi validators & decorators */
|
2023-12-21 15:57:51 +01:00
|
|
|
protected static inputSchema: Joi.Schema;
|
|
|
|
protected static valueSchema: Joi.Schema;
|
|
|
|
protected static outputSchema: Joi.Schema;
|
|
|
|
static schema: Joi.ObjectSchema<{
|
2023-12-21 16:42:00 +01:00
|
|
|
input: any;
|
|
|
|
values: any;
|
|
|
|
output: any;
|
2023-12-21 15:57:51 +01:00
|
|
|
}>;
|
2023-11-20 21:04:49 +01:00
|
|
|
|
|
|
|
actionValues: any;
|
|
|
|
|
|
|
|
constructor (action: Action) {
|
|
|
|
this.actionValues = action.values;
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:55:48 +01:00
|
|
|
async run(input: PdfFile[] | any[], progressCallback: (progress: Progress) => void): Promise<PdfFile[] | any[]> {
|
2023-11-20 21:04:49 +01:00
|
|
|
return [];
|
|
|
|
}
|
2023-11-27 23:35:18 +01:00
|
|
|
}
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** This function should be used if the Operation may take multiple files as inputs and only outputs one file */
|
|
|
|
export async function nToOne <I, O>(inputs: I[], callback: (input: I[]) => Promise<O>): Promise<O[]> {
|
|
|
|
return [await callback(inputs)];
|
|
|
|
}
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** This function should be used if the Operation takes one file as input and may output multiple files */
|
|
|
|
export async function oneToN <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O[]>): Promise<O[]> {
|
2024-01-04 20:17:54 -05:00
|
|
|
let output: O[] = [];
|
2023-11-27 23:35:18 +01:00
|
|
|
for (let i = 0; i < inputs.length; i++) {
|
|
|
|
output = output.concat(await callback(inputs[i], i, inputs.length));
|
2023-11-20 21:04:49 +01:00
|
|
|
}
|
2023-11-27 23:35:18 +01:00
|
|
|
return output;
|
|
|
|
}
|
2023-11-21 00:12:35 +01:00
|
|
|
|
2023-11-27 23:35:18 +01:00
|
|
|
/** This function should be used if the Operation takes one file as input and outputs only one file */
|
|
|
|
export async function oneToOne <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O>): Promise<O[]> {
|
|
|
|
return oneToN(inputs, async (input, index, max) => {
|
2024-01-04 20:17:54 -05:00
|
|
|
return [await callback(input, index, max)];
|
2023-11-27 23:35:18 +01:00
|
|
|
});
|
2023-11-20 22:12:03 +01:00
|
|
|
}
|