Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
328 lines (278 loc) · 11.4 KB
/
Copy pathserver.js
File metadata and controls
328 lines (278 loc) · 11.4 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
require('dotenv').config();
if (process.env.AWS_SESSION_TOKEN === '') {
delete process.env.AWS_SESSION_TOKEN;
}
const express = require('express');
const multer = require('multer');
const {
TextractClient,
StartDocumentAnalysisCommand,
GetDocumentAnalysisCommand
} = require("@aws-sdk/client-textract");
const { S3Client, PutObjectCommand, HeadBucketCommand, CreateBucketCommand } = require("@aws-sdk/client-s3");
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3001;
const BUCKET_NAME = process.env.BUCKET_NAME || `textract-demo-bucket-${Date.now()}`;
const REGION = process.env.AWS_REGION || 'us-east-1';
const upload = multer({ dest: 'uploads/' });
app.use(cors());
app.use(express.json());
// Credentials setup moved below logging
// Construct credentials only if Env Vars are provided
let credentials = undefined;
if (process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY) {
credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
};
if (process.env.AWS_SESSION_TOKEN && process.env.AWS_SESSION_TOKEN.trim() !== '') {
credentials.sessionToken = process.env.AWS_SESSION_TOKEN;
}
}
console.log('--- AWS Configuration ---');
console.log('Region:', REGION);
if (credentials) {
console.log('Using credentials from Environment (.env)');
} else {
console.log('Using Default Credential Provider Chain (~/.aws/credentials)');
}
console.log('-------------------------');
const textract = new TextractClient({ region: REGION, credentials });
const s3 = new S3Client({ region: REGION, credentials });
(async () => {
try {
console.log('Verifying AWS connection (ListBuckets)...');
await s3.send(new (require("@aws-sdk/client-s3").ListBucketsCommand)({}));
console.log('AWS Connection Verified: ListBuckets succeeded.');
} catch (e) {
console.error('Startup Connectivity Check Failed:', e.message);
}
})();
async function ensureBucketExists() {
try {
await s3.send(new HeadBucketCommand({ Bucket: BUCKET_NAME }));
} catch (error) {
if (error.name === 'NotFound' || error.$metadata?.httpStatusCode === 404) {
console.log(`Creating bucket ${BUCKET_NAME}...`);
await s3.send(new CreateBucketCommand({ Bucket: BUCKET_NAME }));
}
}
}
// Helper to poll for ANALYSIS completion (replaces waitForJobCompletion for text)
async function waitForAnalysisCompletion(jobId) {
let jobStatus = 'IN_PROGRESS';
let response;
while (jobStatus === 'IN_PROGRESS') {
process.stdout.write('.');
await new Promise(resolve => setTimeout(resolve, 1000));
response = await textract.send(new GetDocumentAnalysisCommand({ JobId: jobId }));
jobStatus = response.JobStatus;
}
console.log('\nAnalysis Job finished with status:', jobStatus);
if (jobStatus === 'SUCCEEDED') {
return response;
} else {
throw new Error(`Textract analysis failed with status: ${jobStatus}`);
}
}
// Fetch all pages of ANALYSIS results
async function getAllAnalysisResults(jobId, initialResponse) {
let blocks = initialResponse.Blocks || [];
let nextToken = initialResponse.NextToken;
while (nextToken) {
console.log('Fetching next page of results...');
const response = await textract.send(new GetDocumentAnalysisCommand({
JobId: jobId,
NextToken: nextToken
}));
blocks = blocks.concat(response.Blocks || []);
nextToken = response.NextToken;
}
return blocks;
}
// --- Helper Function for Textract Processing ---
async function processFileWithTextract(filePath, originalName) {
const fileKey = `uploads/${Date.now()}-${originalName}`;
let currentStep = 'INIT';
try {
currentStep = 'ENSURE_BUCKET';
console.log('[1/4] Ensuring Bucket Exists...');
await ensureBucketExists();
console.log('[1/4] Bucket OK.');
currentStep = 'UPLOAD_S3';
console.log('[2/4] Uploading to S3...');
const fileBuffer = fs.readFileSync(filePath);
await s3.send(new PutObjectCommand({
Bucket: BUCKET_NAME,
Key: fileKey,
Body: fileBuffer
}));
console.log('[2/4] Upload OK.');
currentStep = 'START_TEXTRACT';
console.log('[3/4] Starting Textract Analysis (FORMS)...');
const startCommand = new StartDocumentAnalysisCommand({
DocumentLocation: {
S3Object: {
Bucket: BUCKET_NAME,
Name: fileKey
}
},
FeatureTypes: ["FORMS"]
});
const startResponse = await textract.send(startCommand);
currentStep = 'POLL_RESULTS';
const jobId = startResponse.JobId;
console.log(`[3/4] Job Started: ${jobId}`);
console.log('[4/4] Polling for results...');
const completedResponse = await waitForAnalysisCompletion(jobId);
const allBlocks = await getAllAnalysisResults(jobId, completedResponse);
console.log(`[4/4] Done. Blocks: ${allBlocks.length}`);
return allBlocks;
} catch (error) {
throw { step: currentStep, error };
}
}
// --- Endpoints ---
// 1. New Endpoint: Returns Raw Blocks
app.post('/analyze-blocks', upload.single('file'), async (req, res) => {
if (!req.file) return res.status(400).send('No file uploaded.');
const filePath = req.file.path;
try {
const allBlocks = await processFileWithTextract(filePath, req.file.originalname);
res.json({ Blocks: allBlocks });
} catch (err) {
console.error("!!! PROCESSING ERROR (/analyze-blocks) !!!", err);
res.status(500).json({
error: 'Processing failed',
details: err.error?.message || err.message,
failedStep: err.step
});
} finally {
fs.unlink(filePath, () => { });
}
});
// 2. Refined Endpoint: Returns only Structured Data (with Booleans)
app.post('/analyze', upload.single('file'), async (req, res) => {
if (!req.file) return res.status(400).send('No file uploaded.');
const filePath = req.file.path;
try {
const allBlocks = await processFileWithTextract(filePath, req.file.originalname);
// --- Parsing Logic with Boolean Support ---
const blockMap = {};
allBlocks.forEach(b => blockMap[b.Id] = b);
const getTextOrBool = (block, includeSelection = true) => {
if (!block.Relationships) return '';
// NEW LOGIC: If there is exactly ONE selection element, return its boolean status.
// This handles cases where Textract accidentally includes label text (e.g. "(Less...") in the value block.
const allChildren = block.Relationships
.filter(r => r.Type === 'CHILD')
.flatMap(r => r.Ids)
.map(id => blockMap[id]);
const selectionChildren = allChildren.filter(c => c.BlockType === 'SELECTION_ELEMENT');
if (includeSelection && selectionChildren.length === 1) {
return selectionChildren[0].SelectionStatus === 'SELECTED';
}
let text = '';
block.Relationships.forEach(rel => {
if (rel.Type === 'CHILD') {
rel.Ids.forEach(childId => {
const child = blockMap[childId];
if (child.BlockType === 'WORD') {
text += child.Text + ' ';
} else if (includeSelection && child.BlockType === 'SELECTION_ELEMENT') {
// Keep string representation for mixed content or multiple boxes
text += child.SelectionStatus === 'SELECTED' ? '[X] ' : '[ ] ';
}
});
}
});
return text.trim();
};
const getSelectionStatusFromBlock = (block) => {
if (!block.Relationships) return null;
let status = null;
block.Relationships.forEach(rel => {
if (rel.Type === 'CHILD') {
rel.Ids.forEach(childId => {
const child = blockMap[childId];
if (child.BlockType === 'SELECTION_ELEMENT') {
status = child.SelectionStatus === 'SELECTED'; // Return boolean
}
});
}
});
return status;
};
const structuredData = {};
let keys = allBlocks.filter(b => b.BlockType === 'KEY_VALUE_SET' && b.EntityTypes.includes('KEY'));
keys.sort((a, b) => {
if ((a.Page || 1) !== (b.Page || 1)) return (a.Page || 1) - (b.Page || 1);
if (Math.abs(a.Geometry.BoundingBox.Top - b.Geometry.BoundingBox.Top) > 0.005) {
return a.Geometry.BoundingBox.Top - b.Geometry.BoundingBox.Top;
}
return a.Geometry.BoundingBox.Left - b.Geometry.BoundingBox.Left;
});
keys.forEach(keyBlock => {
const keyText = getTextOrBool(keyBlock, false)
.replace(/:$/, '')
.replace(/^[X]\s+/i, '')
.replace(/^\[ ?[xX]? ?\]\s+/, '');
let val = null;
const valueRel = keyBlock.Relationships?.find(r => r.Type === 'VALUE');
if (valueRel) {
const valueBlock = blockMap[valueRel.Ids[0]];
val = getTextOrBool(valueBlock, true);
}
if (val === null || val === '') {
const keySelection = getSelectionStatusFromBlock(keyBlock);
if (keySelection !== null) {
val = keySelection;
}
}
if (val === null) val = '';
if (Object.prototype.hasOwnProperty.call(structuredData, keyText)) {
const existing = structuredData[keyText];
if (Array.isArray(existing)) {
if (!existing.includes(val)) {
structuredData[keyText].push(val);
}
} else if (existing !== val) {
structuredData[keyText] = [existing, val];
}
} else {
structuredData[keyText] = val;
}
});
// --- Schema Normalization ---
const { findBestMatch } = require('./schemaMatcher');
const schema = require('./generated_schema.json');
const validKeys = Object.keys(schema.properties);
const normalizedData = {};
Object.keys(structuredData).forEach(extractedKey => {
const bestMatch = findBestMatch(extractedKey, validKeys);
if (bestMatch) {
normalizedData[bestMatch] = structuredData[extractedKey];
} else {
normalizedData[extractedKey] = structuredData[extractedKey];
}
});
res.json({
StructuredData: normalizedData
});
} catch (err) {
console.error("!!! PROCESSING ERROR (/analyze) !!!", err);
res.status(500).json({
error: 'Processing failed',
details: err.error?.message || err.message,
failedStep: err.step
});
} finally {
fs.unlink(filePath, () => { });
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});