-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
152 lines (131 loc) · 3.93 KB
/
api.js
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
const http = require("http");
const express = require("express");
const bodyParser = require("body-parser");
const AzureStorageBlob = require("@azure/storage-blob");
const { BlobServiceClient } = require("@azure/storage-blob");
var cors = require("cors");
const fileUpload = require("express-fileupload");
const fs = require("fs");
const {
FormRecognizerClient,
AzureKeyCredential,
} = require("@azure/ai-form-recognizer");
const { SSL_OP_EPHEMERAL_RSA } = require("constants");
//const { hasSubscribers } = require("diagnostic_channel");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const hostname = "127.0.0.1";
const port = process.env.PORT || "3002";
app.set("port", port);
const server = http.createServer(app);
server.listen(port);
// enable files upload
app.use(
fileUpload({
createParentPath: true,
})
);
app.use((req, res, next) => {
console.log("Enter CORS");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PATCH, DELETE, OPTIONS, PUT"
);
next();
});
app.options("*", cors()); // include before other routes
app.use("/api/analyze", (req, res, next) => {
if (!req.files) {
return res.status(400).send("No files were uploaded.");
}
let file = req.files.file;
uploadPath = __dirname + "\\uploads\\" + new Date().getTime() + ".jpg";
file.mv(uploadPath, async () => {
recognizeForm(uploadPath).then((result) => {
res.status(200).json({
output: result,
});
});
});
});
function verificaArquivo(file){
var ajax = new XMLHttpRequest();
ajax.open("GET",file,true);
ajax.send();
ajax.onreadystatechange = function() {
if (ajax.readyState == 4){
var jpg = ajax.responseText;
if(ajax.status===200) {
console.log("A imagem " + file + " existe");
return false;
} else {
console.log("A imagem " + file + " NAO existe");
return true;
}
}
}
}
var jsonReturn = [];
async function recognizeForm(file) {
const endpoint = "https://doors1.cognitiveservices.azure.com/";
const apiKey = "70b2796924584d8da912296e8dea613a";
const modelId = "808eb101-c6ac-422a-9298-679c47b2a0fc";
console.log("Entering Forms Recognizer");
let fileStream = fs.createReadStream(file);
const client = new FormRecognizerClient(
endpoint,
new AzureKeyCredential(apiKey)
);
const poller = await client.beginRecognizeCustomForms(modelId, fileStream, {
contentType: "application/pdf",
onProgress: (state) => {
console.log(`status: ${state.status}`);
},
});
const forms = await poller.pollUntilDone();
console.log("Forms:");
for (const form of forms || []) {
console.log(`${form.formType}, page range: ${form.pageRange}`);
console.log("Pages:");
for (const page of form.pages || []) {
console.log(`Page number: ${page.pageNumber}`);
console.log("Tables");
for (const table of page.tables || []) {
for (const cell of table.cells) {
console.log(
`cell (${cell.rowIndex},${cell.columnIndex}) ${cell.text}`
);
}
}
}
console.log("Fields:");
for (const fieldName in form.fields) {
// each field is of type FormField
const field = form.fields[fieldName];
var name = field.name;
var valor = field.value;
var obj = `{"`+`${name}`+`": "`+`${valor}`+`"}`;
jsonReturn.push(JSON.parse(obj));
}
console.log(jsonReturn);
}
fs.unlinkSync(uploadPath);
return jsonReturn;
}
async function retorna(jsonReturn){
console.log(jsonReturn);
return jsonReturn;
}
app.get("/api/json", function(req, res) {
retorna(jsonReturn).then((result) => {
res.status(200).json({
output: result,
});
});
});