import { Environment } from '@internxt/inxt-js' ; import { ActionState , FileInfo } from '@internxt/inxt-js/build/api' ; import { Readable } from 'stream' ; import localStorageService from '../../core/services/local-storage.service' ; import { UserSettings } from '../../auth/types' ; import { TeamsSettings } from '../../teams/types' ; import * as blobToStream from 'blob-to-stream' ; import * as uuid from 'uuid' ; export const MAX_ALLOWED_UPLOAD_SIZE = 1024 * 1024 * 1024 ; type ProgressCallback = ( progress : number , uploadedBytes : number | null , totalBytes : number | null ) => void ; interface IUploadParams { filesize : number ; filecontent : Blob ; progressCallback : ProgressCallback ; } interface IDownloadParams { fileToken ? : string ; fileEncryptionKey ? : Buffer ; progressCallback : ProgressCallback ; } interface EnvironmentConfig { bridgeUser : string ; bridgePass : string ; encryptionKey : string ; bucketId : string ; useProxy : boolean ; } export class Network { private environment : Environment ; constructor ( bridgeUser , bridgePass , encryptionKey ) { if ( ! bridgeUser ) { throw new Error ( 'Bridge user not provided' ) ; } if ( ! bridgePass ) { throw new Error ( 'Bridge pass not provided' ) ; } if ( ! encryptionKey ) { throw new Error ( 'Mnemonic not provided' ) ; } this . environment = new Environment ( { bridgePass , bridgeUser , encryptionKey , bridgeUrl : process . env . REACT_APP_STORJ_BRIDGE , } ) ; } param bucketId param params returns uploadFile ( bucketId , params ) { let actionState ; if ( ! bucketId ) { throw new Error ( 'Bucket id not provided' ) ; } const promise = new Promise ( ( resolve , reject ) => { actionState = this . environment . upload ( bucketId , { name : uuid . v4 ( ) , progressCallback : params . progressCallback , finishedCallback : ( err , fileId ) => { if ( err ) { return reject ( err ) ; } if ( ! fileId ) { return reject ( Error ( 'File not created' ) ) ; } resolve ( fileId ) ; } , } , { label : 'OneStreamOnly' , params : { source : { size : params . filesize , stream : blobToStream ( params . filecontent ) } , useProxy : process . env . REACT_APP_DONT_USE_PROXY !== 'true' , concurrency : 6 , } , } ) ; } ) ; return [ promise , actionState ] ; } param bucketId param fileId param params returns downloadFile ( bucketId , fileId , params ) { const [ downloadStreamPromise , actionState ] = this . getFileDownloadStream ( bucketId , fileId , params ) ; let errored = false ; const promise = new Promise < Blob > ( ( resolve , reject ) => { downloadStreamPromise . then ( ( downloadStream ) => { const chunks = [ ] ; downloadStream . on ( 'data' , ( chunk ) => { chunks . push ( chunk ) ; } ) . once ( 'error' , ( err ) => { errored = true ; reject ( err ) ; } ) . once ( 'end' , ( ) => { if ( errored ) { return ; } const uploadedBytes = chunks . reduce ( ( acumm , chunk ) => acumm + chunk . length , 0 ) ; params . progressCallback ( 1 , uploadedBytes , uploadedBytes ) ; resolve ( new Blob ( chunks , { type : 'application/octet-stream' } ) ) ; } ) ; } ) . catch ( reject ) ; } ) ; return [ promise , actionState ] ; } getFileDownloadStream ( bucketId , fileId , params ) { let actionState ; if ( ! bucketId ) { throw new Error ( 'Bucket id not provided' ) ; } if ( ! fileId ) { throw new Error ( 'File id not provided' ) ; } const promise = new Promise < Readable > ( ( resolve , reject ) => { actionState = this . environment . download ( bucketId , fileId , { ... params , finishedCallback : ( err , downloadStream ) => { if ( err ) { return reject ( err ) ; } if ( ! downloadStream ) { return reject ( Error ( 'Download stream is empty' ) ) ; } resolve ( downloadStream ) ; } , } , { label : 'OneStreamOnly' , params : { useProxy : process . env . REACT_APP_DONT_USE_PROXY !== 'true' , concurrency : 6 , } , } ) ; } ) ; return [ promise , actionState ] ; } getFileInfo ( bucketId , fileId ) { return this . environment . getFileInfo ( bucketId , fileId ) ; } createFileToken ( bucketId , fileId , operation ) { return this . environment . createFileToken ( bucketId , fileId , operation ) ; } } param isTeam returns export function getEnvironmentConfig ( isTeam ? ) { if ( isTeam ) { const team = localStorageService . getTeams ( ) as TeamsSettings ; return { bridgeUser : team . bridge_user , bridgePass : team . bridge_password , encryptionKey : team . bridge_mnemonic , bucketId : team . bucket , useProxy : process . env . REACT_APP_DONT_USE_PROXY !== 'true' , } ; } const user = localStorageService . getUser ( ) as UserSettings ; return { bridgeUser : user . bridgeUser , bridgePass : user . userId , encryptionKey : user . mnemonic , bucketId : user . bucket , useProxy : process . env . REACT_APP_DONT_USE_PROXY !== 'true' , } ; } export const generateFileKey = Environment . utils . generateFileKey ;