Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add PDF repair check functionality
  • Loading branch information
microshine committed Jan 23, 2024
commit fc647725c7415cb34870ff05d4610f149a353fc3
38 changes: 37 additions & 1 deletion packages/repair/src/PDFRepair.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { PDFDocument } from "@peculiarventures/pdf-doc";
import { PDFRepairRegistry, IRepairRule, globalRepairRegistry } from "./PDFRepairRegistry";
import { PDFRepairStatus } from "./PDFRepairStatus";

/**
* An object that records repair notes for a PDF file. The keys are rule IDs and the values are arrays of strings
* containing the repair notes.
*/
interface RepairNotes {
export interface RepairNotes {
[key: string]: string[];
}

export interface RepairCheckRule {
status: PDFRepairStatus;
description: string;
}

export interface RepairCheck {
status: PDFRepairStatus;
rules: Record<string, RepairCheckRule>;
}

/**
* A class for repairing PDF documents by applying a set of rules to them.
*/
Expand Down Expand Up @@ -39,4 +50,29 @@ export class PDFRepair {

return repairNotes;
}

/**
* Checks if a PDF document needs to be repaired.
* @param doc - The PDF document to check.
* @returns
*/
async checkDocument(doc: PDFDocument): Promise<RepairCheck> {
const check: RepairCheck = {
status: PDFRepairStatus.notNeeded,
rules: {},
};

for (const rule of this.rules) {
const status = await rule.check(doc);
if (check.status !== PDFRepairStatus.requireClone && status !== PDFRepairStatus.notNeeded) {
check.status = status;
}
check.rules[rule.id] = {
status,
description: rule.description,
};
}

return check;
}
}
7 changes: 7 additions & 0 deletions packages/repair/src/PDFRepairRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PDFDocument } from "@peculiarventures/pdf-doc";
import { PDFRepairStatus } from "./PDFRepairStatus";

/**
* An interface representing a repair rule for a PDF document.
Expand All @@ -18,6 +19,12 @@ export interface IRepairRule {
* @returns A string representing the repair note generated by the rule, or null if no repair was performed.
*/
apply: (doc: PDFDocument) => Promise<string[]>;
/**
* Checks if the repair rule is applicable to a PDF document.
* @param doc
* @returns An enum value indicating the repair status of the PDF document.
*/
check: (doc: PDFDocument) => Promise<PDFRepairStatus>;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions packages/repair/src/PDFRepairStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

/**
* Represents the repair status of a PDF document.
*/
export enum PDFRepairStatus {
/**
* The PDF document is not repairable. It is either corrupt or malformed.
* The document should be fixed via the `PDFDocument.clone()` method.
*/
requireClone = "RequireClone",
/**
* The PDF document is repairable and doesn't need to be rewritten.
*/
repairable = "Repairable",
/**
* The PDF document is correctly formatted and does not need to be repaired.
*/
notNeeded = "NotNeeded",
}