@angloqq03/gpv
Version:
OCR-powered GCash screenshot proof validation for Node.js using Tesseract.js and Brain.js.
72 lines (58 loc) • 1.86 kB
JavaScript
import { fullAnalysis, validateProof } from "../core/validation.js";
export function resolveImageInput(input) {
if (!input) {
throw new Error("No proof image was provided.");
}
if (
typeof input === "string" ||
input instanceof Uint8Array ||
(typeof Buffer !== "undefined" && Buffer.isBuffer(input))
) {
return input;
}
if (input.buffer) {
return input.buffer;
}
if (input.path) {
return input.path;
}
throw new Error("Unsupported image input. Use a file path, Buffer, Uint8Array, or upload object.");
}
export async function validateUploadedProof(upload, expected = {}, options = {}) {
const image = resolveImageInput(upload);
return validateProof(image, expected, options);
}
export async function analyzeUploadedProof(upload, expected = {}, options = {}) {
const image = resolveImageInput(upload);
return fullAnalysis(image, expected, options);
}
export function createExpressProofValidator(config = {}) {
const {
attachToRequestAs = "proofValidation",
getExpected = async () => ({}),
getImage = async (req) => req.file ?? req.files?.proof ?? req.body?.proof,
options = {},
run = "fullAnalysis"
} = config;
return async function gcashProofValidatorMiddleware(req, res, next) {
try {
const upload = await getImage(req, res);
const expected = await getExpected(req, res);
const image = resolveImageInput(upload);
const result =
run === "validateProof"
? await validateProof(image, expected, options)
: await fullAnalysis(image, expected, options);
req[attachToRequestAs] = result;
if (typeof next === "function") {
return next();
}
return result;
} catch (error) {
if (typeof next === "function") {
return next(error);
}
throw error;
}
};
}