module.exports = class S3ServiceAccess {
constructor(objectStoreCredentials) {
const AWS = require('aws-sdk');
const credentials = new AWS.Credentials(
objectStoreCredentials.access_key_id,
objectStoreCredentials.secret_access_key);
AWS.config.update({
region: objectStoreCredentials.region,
});
this._s3 = new AWS.S3({
credentials: credentials,
apiVersion: '2006-03-01',
s3ForcePathStyle: true
});
this._bucket = objectStoreCredentials.bucket;
}
getObjectAsBuffer(objectKey) {
const params = {
Bucket: this._bucket,
Key: objectKey
};
return this._s3.getObject(params).promise().then((data) => data.Body);
}
}
service EnrichmentService {
function scanAttachmentText(batchSize: Integer) returns Boolean;
}
srv.on('scanAttachmentText', async (req) => {
const attachments = await getAttachmentsToScan(req);
const limit = req.data.batchSize || 5;
if (attachments && attachments.length > 0) {
for (let i = 0; i < Math.min(attachments.length, limit); i++) {
setTimeout(() => {
callOCR(attachments[i], persistOCROutput);
}, i * 10 * 1000);
}
}
return true;
});
const cfenv = require('cfenv');
const mlService = cfenv.getAppEnv().getService('default_sap-document-information-extraction');
const axios = require('axios').default;
const FormData = require('form-data');
// AWS Object Store specific
const S3ServiceAccess = require('./util/S3ServiceAccess');
const objectstore = cfenv.getAppEnv().getService('sf-objectstore');
const objectstoreCredentials = objectstore.credentials;
const S3Service = new S3ServiceAccess(objectstoreCredentials);
async function callOCR(options, bpItem, cb) {
const destinationServiceToken = await fetchDestinationServiceToken();
try {
const fileStream = await S3Service.getObjectAsBuffer(bpItem.AttachmentId);
var options = {
'method': 'POST',
'url': mlService.credentials.endpoints.backend.url + '/document-information-extraction/v1/document/jobs',
'headers': {
'Authorization': 'Bearer ' + destinationServiceToken
},
formData: {
'options': `{
"extraction": {
"headerFields": [
"documentNumber",
"deliveryDate"
],
"lineItemFields": [
"materialNumber",
"unitOfMeasure"
]
},
"clientId": "default",
"documentType": "invoice",
"receivedDate": "2020-02-17",
"enrichment": {
"sender": {
"top": 5,
"type": "businessEntity",
"subtype": "supplier"
},
"employee": {
"type": "employee"
}
}
}`,
'file': {
'value': fileStream,
'options': {
'filename': bpItem.OriginalFileName,
'contentType': null
}
}
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
const jobId = JSON.parse(response.body).id;
// wait for final job outcome, 70 seconds
setTimeout(() => {
request({
'method': 'GET',
'url': mlService.credentials.endpoints.backend.url + '/document-information-extraction/v1/document/jobs/' + jobId + '/pages/text',
'headers': {
'Authorization': 'Bearer ' + destinationServiceToken
}
}, function (error, response) {
if (error) throw new Error(error);
const jsonOutput = JSON.parse(response.body);
try {
const ocrStrings = Object.values(jsonOutput.results).map(page => page.map(word_boxes => word_boxes.word_boxes)).flat(2).map(item => item.content).join(" ");
cb(ocrStrings, bpItem, fileStream);
}
catch (error) {
console.log("error for file: ", bpItem.AttachmentId);
cb("", bpItem, fileStream);
}
});
}, 70 * 1000);
});
} catch (error) {
// TODO Handle Error
console.log(error);
}
};
async function fetchDestinationServiceToken() {
var destinationServiceToken = "";
try {
const uaaServiceResponse = await axios(getDestinationServiceTokenOptions());
if (uaaServiceResponse.status === 200) {
destinationServiceToken = uaaServiceResponse.data.access_token;
}
} catch (uaaServiceError) {
// TODO Handle Error
console.log('uaaServiceError', uaaServiceError.message);
}
return destinationServiceToken;
};
function getDestinationServiceTokenOptions() {
const uaaDestinationCredentials = mlService.credentials.uaa.clientid + ':' + mlService.credentials.uaa.clientsecret;
var destinationServiceTokenFormData = new FormData();
var destinationServiceTokenHeaders = destinationServiceTokenFormData.getHeaders();
destinationServiceTokenHeaders['Authorization'] = 'Basic ' + Buffer.from(uaaDestinationCredentials).toString('base64');
destinationServiceTokenHeaders['Content-type'] = 'application/x-www-form-urlencoded';
const destinationServiceTokenOptions = {
method: 'POST',
url: mlService.credentials.uaa.url + '/oauth/token',
headers: destinationServiceTokenHeaders,
params: { client_id: mlService.credentials.uaa.clientid, grant_type: 'client_credentials' }
}
return destinationServiceTokenOptions;
};
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
13 | |
13 | |
11 | |
10 | |
10 | |
10 | |
8 | |
8 |