@@ -19,6 +19,7 @@ import {
1919} from '@nuclia/core' ;
2020import { Link } from '../../connector/domain/connector' ;
2121import { LinkExtraParams } from './sync.entity' ;
22+ import { Readable } from 'node:stream' ;
2223
2324function sha256 ( message : string ) : string {
2425 return createHash ( 'sha256' ) . update ( message ) . digest ( 'hex' ) ;
@@ -103,31 +104,33 @@ export class NucliaCloud {
103104 }
104105 if ( buffer ) {
105106 try {
106- return resource
107- . upload ( 'file' , buffer , false , {
108- contentType : data . mimeType || lookup ( filename ) || 'application/octet-stream' ,
109- filename,
110- processing : data . extract_strategy ,
111- } )
112- . pipe (
113- catchError ( ( error : any ) => {
114- console . error ( `Problem uploading ${ filename } to ${ slug } , error: ${ JSON . stringify ( error ) } ` ) ;
115- return of ( { success : false , message : error . body ?. detail || JSON . stringify ( error ) } ) ;
116- } ) ,
117- switchMap ( ( res ) => {
118- if ( res && ( res as UploadResponse ) . completed ) {
119- return of ( { success : true } ) ;
120- } else {
121- return this . deleteResource ( slug , resource ) . pipe (
122- map ( ( ) =>
123- ( res as any ) . success === false
124- ? ( res as { success : boolean ; message : string } )
125- : { success : false , message : 'Upload failed' } ,
126- ) ,
127- ) ;
128- }
107+ return this . getMD5 ( buffer ) . pipe (
108+ switchMap ( ( md5 ) =>
109+ resource . upload ( 'file' , buffer , false , {
110+ contentType : data . mimeType || lookup ( filename ) || 'application/octet-stream' ,
111+ filename,
112+ processing : data . extract_strategy ,
113+ md5,
129114 } ) ,
130- ) ;
115+ ) ,
116+ catchError ( ( error : any ) => {
117+ console . error ( `Problem uploading ${ filename } to ${ slug } , error: ${ JSON . stringify ( error ) } ` ) ;
118+ return of ( { success : false , message : error . body ?. detail || JSON . stringify ( error ) } ) ;
119+ } ) ,
120+ switchMap ( ( res ) => {
121+ if ( res && ( res as UploadResponse ) . completed ) {
122+ return of ( { success : true } ) ;
123+ } else {
124+ return this . deleteResource ( slug , resource ) . pipe (
125+ map ( ( ) =>
126+ ( res as any ) . success === false
127+ ? ( res as { success : boolean ; message : string } )
128+ : { success : false , message : 'Upload failed' } ,
129+ ) ,
130+ ) ;
131+ }
132+ } ) ,
133+ ) ;
131134 } catch ( error ) {
132135 console . error ( `Error uploading ${ filename } to ${ slug } , status ${ error } ` ) ;
133136 return this . deleteResource ( slug , resource ) . pipe ( map ( ( ) => ( { success : false } ) ) ) ;
@@ -297,4 +300,23 @@ export class NucliaCloud {
297300 }
298301 return resource ;
299302 }
303+
304+ private getMD5 ( buffer : ArrayBuffer ) : Observable < string > {
305+ return new Observable ( ( observer ) => {
306+ const output = createHash ( 'md5' ) ;
307+ const readable = new Readable ( ) ;
308+ readable . _read = ( ) => { } ; // _read is required but you can noop it
309+ readable . push ( Buffer . from ( buffer ) ) ;
310+ readable . push ( null ) ;
311+ readable . on ( 'error' , ( err ) => {
312+ console . error ( err ) ;
313+ observer . error ( err ) ;
314+ } ) ;
315+ output . once ( 'readable' , ( ) => {
316+ observer . next ( output . read ( ) . toString ( 'hex' ) ) ;
317+ observer . complete ( ) ;
318+ } ) ;
319+ readable . pipe ( output ) ;
320+ } ) ;
321+ }
300322}
0 commit comments