@@ -40,34 +40,35 @@ providerRoutes.post(
4040 `${ SERVICES_API_BASE_PATH } /encrypt` ,
4141 express . raw ( { limit : '25mb' } ) ,
4242 async ( req , res ) => {
43- try {
44- const data = req . body . toString ( )
45- if ( ! data ) {
46- res . status ( 400 ) . send ( 'Missing required body' )
47- return
48- }
49- const result = await new EncryptHandler ( req . oceanNode ) . handle ( {
50- blob : data ,
51- encoding : 'string' ,
52- encryptionType : EncryptMethod . ECIES ,
53- command : PROTOCOL_COMMANDS . ENCRYPT ,
54- caller : req . caller ,
55- nonce : req . query . nonce as string ,
56- consumerAddress : req . query . consumerAddress as string ,
57- signature : req . query . signature as string
58- } )
59- if ( result . stream ) {
60- const encryptedData = await streamToString ( result . stream as Readable )
61- res . header ( 'Content-Type' , 'application/octet-stream' )
62- res . status ( 200 ) . send ( encryptedData )
63- } else {
64- res . status ( result . status . httpStatus ) . send ( result . status . error )
43+ try {
44+ const data = req . body . toString ( )
45+ if ( ! data ) {
46+ res . status ( 400 ) . send ( 'Missing required body' )
47+ return
48+ }
49+ const result = await new EncryptHandler ( req . oceanNode ) . handle ( {
50+ blob : data ,
51+ encoding : 'string' ,
52+ encryptionType : EncryptMethod . ECIES ,
53+ command : PROTOCOL_COMMANDS . ENCRYPT ,
54+ caller : req . caller ,
55+ nonce : req . query . nonce as string ,
56+ consumerAddress : req . query . consumerAddress as string ,
57+ signature : req . query . signature as string
58+ } )
59+ if ( result . stream ) {
60+ const encryptedData = await streamToString ( result . stream as Readable )
61+ res . header ( 'Content-Type' , 'application/octet-stream' )
62+ res . status ( 200 ) . send ( encryptedData )
63+ } else {
64+ res . status ( result . status . httpStatus ) . send ( result . status . error )
65+ }
66+ } catch ( error ) {
67+ HTTP_LOGGER . log ( LOG_LEVELS_STR . LEVEL_ERROR , `Error: ${ error } ` )
68+ res . status ( 500 ) . send ( 'Internal Server Error' )
6569 }
66- } catch ( error ) {
67- HTTP_LOGGER . log ( LOG_LEVELS_STR . LEVEL_ERROR , `Error: ${ error } ` )
68- res . status ( 500 ) . send ( 'Internal Server Error' )
6970 }
70- } )
71+ )
7172
7273// There are two ways of encrypting a file:
7374
@@ -89,76 +90,79 @@ providerRoutes.post(
8990 express . raw ( { limit : '25mb' , type : 'application/octet-stream' } ) ,
9091 express . json ( ) ,
9192 async ( req , res ) => {
92- const writeResponse = async (
93- result : P2PCommandResponse ,
94- encryptMethod : EncryptMethod
95- ) => {
96- if ( result . stream ) {
97- const encryptedData = await streamToString ( result . stream as Readable )
98- res . set ( result . status . headers )
99- res . status ( 200 ) . send ( encryptedData )
100- } else {
101- res . status ( result . status . httpStatus ) . send ( result . status . error )
93+ const writeResponse = async (
94+ result : P2PCommandResponse ,
95+ encryptMethod : EncryptMethod
96+ ) => {
97+ if ( result . stream ) {
98+ const encryptedData = await streamToString ( result . stream as Readable )
99+ res . set ( result . status . headers )
100+ res . status ( 200 ) . send ( encryptedData )
101+ } else {
102+ res . status ( result . status . httpStatus ) . send ( result . status . error )
103+ }
102104 }
103- }
104105
105- const getEncryptedData = async (
106- encryptMethod : EncryptMethod . AES | EncryptMethod . ECIES ,
107- input : Buffer
108- ) => {
109- const result = await new EncryptFileHandler ( req . oceanNode ) . handle ( {
110- rawData : input ,
111- encryptionType : encryptMethod ,
112- command : PROTOCOL_COMMANDS . ENCRYPT_FILE ,
113- caller : req . caller ,
114- nonce : req . query . nonce as string ,
115- consumerAddress : req . query . consumerAddress as string ,
116- signature : req . query . signature as string
117- } )
118- return result
119- }
120-
121- try {
122- const encryptMethod : EncryptMethod = getEncryptMethodFromString (
123- req . query . encryptMethod as string
124- )
125- let result : P2PCommandResponse
126- if ( req . is ( 'application/json' ) ) {
127- // body as fileObject
128- result = await new EncryptFileHandler ( req . oceanNode ) . handle ( {
129- files : req . body as StorageObject ,
106+ const getEncryptedData = async (
107+ encryptMethod : EncryptMethod . AES | EncryptMethod . ECIES ,
108+ input : Buffer
109+ ) => {
110+ const result = await new EncryptFileHandler ( req . oceanNode ) . handle ( {
111+ rawData : input ,
130112 encryptionType : encryptMethod ,
131113 command : PROTOCOL_COMMANDS . ENCRYPT_FILE ,
132114 caller : req . caller ,
133115 nonce : req . query . nonce as string ,
134116 consumerAddress : req . query . consumerAddress as string ,
135117 signature : req . query . signature as string
136118 } )
137- return await writeResponse ( result , encryptMethod )
138- // raw data on body
139- } else if ( req . is ( 'application/octet-stream' ) || req . is ( 'multipart/form-data' ) ) {
140- if ( req . is ( 'application/octet-stream' ) ) {
141- result = await getEncryptedData ( encryptMethod , req . body )
142- return await writeResponse ( result , encryptMethod )
143- } else {
144- // multipart/form-data
145- const data : Buffer [ ] = [ ]
146- req . on ( 'data' , function ( chunk ) {
147- data . push ( chunk )
119+ return result
120+ }
121+
122+ try {
123+ const encryptMethod : EncryptMethod = getEncryptMethodFromString (
124+ req . query . encryptMethod as string
125+ )
126+ let result : P2PCommandResponse
127+ if ( req . is ( 'application/json' ) ) {
128+ // body as fileObject
129+ result = await new EncryptFileHandler ( req . oceanNode ) . handle ( {
130+ files : req . body as StorageObject ,
131+ encryptionType : encryptMethod ,
132+ command : PROTOCOL_COMMANDS . ENCRYPT_FILE ,
133+ caller : req . caller ,
134+ nonce : req . query . nonce as string ,
135+ consumerAddress : req . query . consumerAddress as string ,
136+ signature : req . query . signature as string
148137 } )
149- req . on ( 'end' , async function ( ) {
150- result = await getEncryptedData ( encryptMethod , Buffer . concat ( data ) )
138+ return await writeResponse ( result , encryptMethod )
139+ // raw data on body
140+ } else if ( req . is ( 'application/octet-stream' ) || req . is ( 'multipart/form-data' ) ) {
141+ if ( req . is ( 'application/octet-stream' ) ) {
142+ result = await getEncryptedData ( encryptMethod , req . body )
151143 return await writeResponse ( result , encryptMethod )
152- } )
144+ } else {
145+ // multipart/form-data
146+ const data : Buffer [ ] = [ ]
147+ req . on ( 'data' , function ( chunk ) {
148+ data . push ( chunk )
149+ } )
150+ req . on ( 'end' , async function ( ) {
151+ result = await getEncryptedData ( encryptMethod , Buffer . concat ( data ) )
152+ return await writeResponse ( result , encryptMethod )
153+ } )
154+ }
155+ } else {
156+ res
157+ . status ( 400 )
158+ . send ( 'Invalid request (missing body data or invalid content-type)' )
153159 }
154- } else {
155- res . status ( 400 ) . send ( 'Invalid request (missing body data or invalid content-type)' )
160+ } catch ( error ) {
161+ HTTP_LOGGER . log ( LOG_LEVELS_STR . LEVEL_ERROR , `Error: ${ error } ` )
162+ res . status ( 500 ) . send ( 'Internal Server Error' )
156163 }
157- } catch ( error ) {
158- HTTP_LOGGER . log ( LOG_LEVELS_STR . LEVEL_ERROR , `Error: ${ error } ` )
159- res . status ( 500 ) . send ( 'Internal Server Error' )
160164 }
161- } )
165+ )
162166
163167providerRoutes . get ( `${ SERVICES_API_BASE_PATH } /initialize` , async ( req , res ) => {
164168 try {
0 commit comments