2024-02-27 21:51:03 +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 {
|
2024-07-13 21:20:47 +02:00
|
|
|
actionValues: any = undefined;
|
2023-11-20 21:04:49 +01:00
|
|
|
|
|
|
|
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[]> {
|
2024-08-10 00:17:38 +02:00
|
|
|
throw new Error("Operator.run() was called directly. This is not the desired behavior; call the Subclass's run function instead.");
|
|
|
|
// For reference:
|
|
|
|
progressCallback({ curFileProgress: 1, operationProgress: 1 });
|
2024-05-18 00:09:46 +02:00
|
|
|
return input;
|
2023-11-20 21:04:49 +01:00
|
|
|
}
|
2023-11-27 23:35:18 +01:00
|
|
|
}
|
2023-11-20 21:04:49 +01:00
|
|
|
|
2024-07-13 21:20:47 +02:00
|
|
|
|
|
|
|
export class OperatorSchema {
|
|
|
|
schema: Joi.ObjectSchema<any>;
|
|
|
|
|
|
|
|
constructor(label: string, description: string, inputSchema: Joi.Schema, valueSchema: Joi.Schema, outputSchema: Joi.Schema) {
|
|
|
|
this.schema = Joi.object({
|
|
|
|
input: inputSchema,
|
|
|
|
values: valueSchema.required(),
|
|
|
|
output: outputSchema
|
|
|
|
}).label(label).description(description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|