1+ /* eslint-disable no-await-in-loop */
12const sizeof = require ( 'object-sizeof' ) ;
23const { AttachmentProcessor } = require ( '@elastic.io/component-commons-library' ) ;
34const { messages } = require ( 'elasticio-node' ) ;
@@ -6,69 +7,70 @@ const xml2Json = require('../xml2Json.js');
67
78const MAX_FILE_SIZE = 5242880 ; // 5 MiB
89
9- function checkFileName ( fileName , pattern ) {
10- if ( fileName === undefined ) {
11- return false ;
12- }
10+ function checkFileName ( self , fileName , pattern ) {
11+ if ( fileName === undefined ) {
12+ return false ;
13+ }
1314
14- if ( ! pattern . test ( fileName ) ) {
15- console . log ( '%s does not match pattern: \'%s\'' , fileName , pattern ) ;
16- return false ;
17- }
15+ if ( ! pattern . test ( fileName ) ) {
16+ self . logger . debug ( '%s does not match pattern: \'%s\'' , fileName , pattern ) ;
17+ return false ;
18+ }
1819
19- if ( fileName . split ( '.' ) . pop ( ) !== 'xml' ) {
20- console . log ( '%s is not .xml file: ' , fileName ) ;
21- return false ;
22- }
20+ if ( fileName . split ( '.' ) . pop ( ) !== 'xml' ) {
21+ self . logger . debug ( '%s is not .xml file: ' , fileName ) ;
22+ return false ;
23+ }
2324
24- return true ;
25+ return true ;
2526}
2627
2728module . exports . process = async function processAction ( msg , cfg ) {
28- const { attachments } = msg ;
29- const pattern = new RegExp ( cfg !== undefined ? cfg . pattern || '(.xml)' : '(.xml)' ) ;
30- let foundXML = false ;
29+ const self = this ;
30+ const { attachments } = msg ;
31+ const pattern = new RegExp ( cfg !== undefined ? cfg . pattern || '(.xml)' : '(.xml)' ) ;
32+ let foundXML = false ;
3133
32- console . log ( 'Attachment to XML started' ) ;
33- console . log ( 'Found %s attachments' , Object . keys ( attachments || { } ) . length ) ;
34+ self . logger . info ( 'Attachment to XML started' ) ;
35+ self . logger . info ( 'Found %s attachments' , Object . keys ( attachments || { } ) . length ) ;
3436
35- // eslint-disable-next-line no-restricted-syntax
36- for ( const key of Object . keys ( attachments ) ) {
37- const attachment = attachments [ key ] ;
38- const fileName = key ;
39- let fileSize = attachment . size ; // get file size based attachment object may not be define or be accurate
40- console . log ( 'Processing attachment=%s' , fileName ) ;
37+ // eslint-disable-next-line no-restricted-syntax
38+ for ( const key of Object . keys ( attachments ) ) {
39+ const attachment = attachments [ key ] ;
40+ const fileName = key ;
41+ // get file size based attachment object may not be define or be accurate
42+ let fileSize = attachment . size ;
43+ self . logger . info ( 'Processing attachment=%s' , fileName ) ;
4144
42- if ( checkFileName ( fileName , pattern ) ) {
43- if ( fileSize === undefined || fileSize < MAX_FILE_SIZE ) {
44- // eslint-disable-next-line no-await-in-loop
45- const response = await new AttachmentProcessor ( ) . getAttachment ( attachment . url , 'arraybuffer' ) ;
45+ if ( checkFileName ( self , fileName , pattern ) ) {
46+ if ( fileSize === undefined || fileSize < MAX_FILE_SIZE ) {
47+ // eslint-disable-next-line no-await-in-loop
48+ const response = await new AttachmentProcessor ( ) . getAttachment ( attachment . url , 'arraybuffer' ) ;
4649
47- if ( response . status >= 400 ) {
48- throw new Error ( `Error in making request to ${ attachment . url }
50+ if ( response . status >= 400 ) {
51+ throw new Error ( `Error in making request to ${ attachment . url }
4952 Status code: ${ response . status } ,
5053 Body: ${ Buffer . from ( response . data , 'binary' ) . toString ( 'base64' ) } ` ) ;
51- }
54+ }
5255
53- const responseBodyString = Buffer . from ( response . data , 'binary' ) . toString ( 'utf-8' ) ;
54- fileSize = sizeof ( responseBodyString ) ;
56+ const responseBodyString = Buffer . from ( response . data , 'binary' ) . toString ( 'utf-8' ) ;
57+ fileSize = sizeof ( responseBodyString ) ;
5558
56- if ( fileSize < MAX_FILE_SIZE ) {
57- // eslint-disable-next-line no-await-in-loop
58- const returnMsg = await xml2Json . process ( responseBodyString ) ;
59- this . emit ( 'data' , messages . newMessageWithBody ( returnMsg . body ) ) ;
60- foundXML = true ;
61- } else {
62- throw new Error ( `Attachment ${ key } is too large to be processed my XML component.`
63- + ` File limit is: ${ MAX_FILE_SIZE } byte, file given was: ${ fileSize } byte.` ) ;
64- }
65- } else {
66- throw new Error ( `Attachment ${ key } is too large to be processed my XML component.`
67- + ` File limit is: ${ MAX_FILE_SIZE } byte, file given was: ${ fileSize } byte.` ) ;
68- }
59+ if ( fileSize < MAX_FILE_SIZE ) {
60+ const returnMsg = await xml2Json . process ( this , responseBodyString ) ;
61+ foundXML = true ;
62+ await self . emit ( 'data' , messages . newMessageWithBody ( returnMsg . body ) ) ;
63+ } else {
64+ throw new Error ( `Attachment ${ key } is too large to be processed my XML component.`
65+ + ` File limit is: ${ MAX_FILE_SIZE } byte, file given was: ${ fileSize } byte.` ) ;
6966 }
67+ } else {
68+ throw new Error ( `Attachment ${ key } is too large to be processed my XML component.`
69+ + ` File limit is: ${ MAX_FILE_SIZE } byte, file given was: ${ fileSize } byte.` ) ;
70+ }
7071 }
71- if ( ! foundXML ) {
72- console . log ( `No XML files that match the pattern found with in attachments. Pattern: ${ pattern } ` ) ;
73- }
72+ }
73+ if ( ! foundXML ) {
74+ self . logger . info ( `No XML files that match the pattern found with in attachments. Pattern: ${ pattern } ` ) ;
75+ }
7476} ;
0 commit comments