import { generateProtonWebUID } from '@proton/shared/lib/helpers/uid' ; import { base64StringToUint8Array } from '@proton/shared/lib/helpers/encoding' ; import { generateCid , setEmbeddedAttr } from './messageEmbeddeds' ; import { MessageState } from '../../logic/messages/messagesTypes' ; /**
 * Convert data-uri to blob
 */ const dataUrlToFile = ( fileName , dataUrl ) => { const [ mime = '' , byte = '' ] = dataUrl . split ( ',' ) ; const mimeString = mime . split ( ':' ) [ 1 ] . split ( ';' ) [ 0 ] ; try { const data = base64StringToUint8Array ( decodeURIComponent ( byte ) ) ; return new File ( [ data ] , fileName , { type : mimeString } ) ; } catch { return new File ( [ byte ] , fileName , { type : mimeString } ) ; } } ; /**
 * Transform every data-uri in the message content to embedded attachment
 */ export const replaceDataUrl = ( message ) => { if ( ! message . messageDocument ?. document ) { return [ ] ; } return [ ... message . messageDocument ?. document . querySelectorAll ( 'img' ) ] . map ( ( image ) => ( { image , src : image . src || '' } ) ) . filter ( ( { src } ) => /data:image/ . test ( src ) ) . filter ( ( { src } ) => src . includes ( ',' ) ) . map ( ( { image , src } ) => { const cid = generateCid ( generateProtonWebUID ( ) , message . data ?. Sender ?. Address || '' ) ; const fileName = image . alt || `image${ Date . now ( ) }` ; const file = dataUrlToFile ( fileName , src ) ; setEmbeddedAttr ( cid , '' , image ) ; image . removeAttribute ( 'src' ) ; return { cid , file } ; } ) ; } ;