From fae524f8dadec0c2852d444d727f3d22b46024ed Mon Sep 17 00:00:00 2001 From: Felix Kaspar Date: Sun, 19 May 2024 00:19:12 +0200 Subject: [PATCH] arrangePages --- .../src/functions/arrangePages.ts | 78 ++++++++++++++----- .../src/functions/common/detectQRCodePages.ts | 2 +- 2 files changed, 59 insertions(+), 21 deletions(-) diff --git a/shared-operations/src/functions/arrangePages.ts b/shared-operations/src/functions/arrangePages.ts index 2c7f28c68..edbbb3349 100644 --- a/shared-operations/src/functions/arrangePages.ts +++ b/shared-operations/src/functions/arrangePages.ts @@ -1,26 +1,64 @@ +import { Operator, Progress, oneToOne } from "."; + +import Joi from "@stirling-tools/joi"; +import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi"; + +import i18next from "i18next"; + import { PdfFile } from "../wrappers/PdfFile"; import { Sorts } from "./common/pageIndexesSorting"; import { getPages } from "./common/getPagesByIndex"; -import { parsePageIndexSpecification } from "./common/pageIndexesUtils"; -export interface ArrangePagesParamsType { - file: PdfFile; - arrangementConfig: string; // a member of Sorts, or a page index specification -} -export async function arrangePages(params: ArrangePagesParamsType) { - const { file, arrangementConfig } = params; - const pdfLibDocument = await file.pdfLibDocument; - const pageCount = pdfLibDocument.getPageCount(); +export class ArrangePages extends Operator { + static type = "arrangePages"; - let sortIndexes: number[]; - if (arrangementConfig in Sorts) { - const sortFunction = Sorts[arrangementConfig]; - sortIndexes = sortFunction(pageCount); - } else { - sortIndexes = parsePageIndexSpecification(arrangementConfig, pageCount); + /** + * Validation & Localisation + */ + + protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description")); + protected static valueSchema = Joi.object({ + arrangementConfig: Joi.string().valid(...[ + "REVERSE_ORDER", + "DUPLEX_SORT", + "BOOKLET_SORT", + "SIDE_STITCH_BOOKLET_SORT", + "ODD_EVEN_SPLIT", + "REMOVE_FIRST", + "REMOVE_LAST", + "REMOVE_FIRST_AND_LAST" + ]).required() + .label(i18next.t("values.arrangementConfig.friendlyName", { ns: "arrangePages" })).description(i18next.t("values.arrangementConfig.description", { ns: "arrangePages" })) + .example("REVERSE_ORDER").example("DUPLEX_SORT").example("BOOKLET_SORT").required() + }); + protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description")); + + static schema = Joi.object({ + input: ArrangePages.inputSchema, + values: ArrangePages.valueSchema.required(), + output: ArrangePages.outputSchema + }).label(i18next.t("friendlyName", { ns: "arrangePages" })).description(i18next.t("description", { ns: "arrangePages" })); + + + /** + * Logic + */ + + /** Detect and remove white pages */ + async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise { + return oneToOne(input, async (input, index, max) => { + const pdfLibDocument = await input.pdfLibDocument; + const pageCount = pdfLibDocument.getPageCount(); + + const sortFunction = Sorts[this.actionValues.arrangementConfig]; + let sortIndexes = sortFunction(pageCount); + + const newFile = await getPages(input, sortIndexes); + newFile.filename += "arrangedPages"; + + progressCallback({ curFileProgress: 1, operationProgress: index/max }); + + return newFile; + }); } - - const newFile = await getPages(file, sortIndexes); - newFile.filename += "arrangedPages"; - return newFile; -} +} \ No newline at end of file diff --git a/shared-operations/src/functions/common/detectQRCodePages.ts b/shared-operations/src/functions/common/detectQRCodePages.ts index d7db0b477..caa020f82 100644 --- a/shared-operations/src/functions/common/detectQRCodePages.ts +++ b/shared-operations/src/functions/common/detectQRCodePages.ts @@ -16,7 +16,7 @@ export async function detectQRCodePages(file: PdfFile) { // console.log("images:", images); for (const image of images) { const data = await checkForQROnImage(image); - if(data == "https://github.com/Frooodle/Stirling-PDF") { + if(["https://github.com/Stirling-Tools/Stirling-PDF", "https://github.com/Frooodle/Stirling-PDF"].includes(data)) { pagesWithQR.push(i); } }