Spaces:
Sleeping
Sleeping
File size: 2,249 Bytes
60334f4 ab4a99c 9a9e7ec 60334f4 9a9e7ec 60334f4 ab4a99c 60334f4 9a9e7ec 60334f4 9a9e7ec 60334f4 9a9e7ec 60334f4 ab4a99c 60334f4 ab4a99c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | /**
* Pini Print Bot - Deterministic Image Processor
* AI Governance Law v1.0: CODE IS JUDGE.
* * module: engine/imageProcessor.js
* purpose: Physical measurement of image buffers, bypassing all LLM hallucinations.
*/
const sharp = require('sharp');
const MAX_FILE_SIZE_MB = 10;
const MAX_FILE_SIZE_BYTES = MAX_FILE_SIZE_MB * 1024 * 1024;
/**
* Validates file size and extracts physical dimensions deterministically.
* @param {Buffer} fileBuffer - The image file buffer.
* @returns {Promise<Object>} - The deterministic technical payload.
*/
async function processImageUpload(fileBuffer) {
// 1. Gatekeeper: File Size Limits
if (fileBuffer.length > MAX_FILE_SIZE_BYTES) {
throw new Error(`FILE_TOO_LARGE: ืืงืืืฅ ืืืจื ืืืืืืช ${MAX_FILE_SIZE_MB}MB.`);
}
try {
// 2. The Physical Judge: Read metadata headers via sharp
const metadata = await sharp(fileBuffer).metadata();
// Extract raw physical properties
const widthPx = metadata.width;
const heightPx = metadata.height;
const format = metadata.format;
// Extract DPI (Density). If missing in EXIF, assume standard web 72 DPI.
const dpi = metadata.density || 72;
// 3. Mathematical Conversion: Pixels to Millimeters
// Formula: (pixels * 25.4) / DPI
const widthMm = Math.round((widthPx * 25.4) / dpi);
const heightMm = Math.round((heightPx * 25.4) / dpi);
// 4. Construct the Governance Payload
// This object goes STRAIGHT to the Decision Kernel.
const technicalPayload = {
dpi: dpi,
width_mm: widthMm,
height_mm: heightMm,
format: format,
is_deterministic: true, // Audit flag
status: "ANALYZED_BY_CODE"
};
console.log(`[GOVERNANCE] Deterministic extraction success: ${widthMm}x${heightMm}mm @ ${dpi}DPI`);
return technicalPayload;
} catch (error) {
console.error("[IMAGE PROCESSOR] Fatal error analyzing physical file:", error);
throw new Error("INVALID_IMAGE_FORMAT: ืื ื ืืชื ืืคืขื ื ืืช ืื ืชืื ืื ืืืื ืืื ืฉื ืืชืืื ื.");
}
}
module.exports = {
processImageUpload
};
|