@@ -10,13 +10,12 @@ import {
1010import { Environment } from '@internxt/inxt-js' ;
1111import { randomBytes } from 'node:crypto' ;
1212import { Readable , Transform } from 'node:stream' ;
13- import { DownloadOptions , UploadOptions , UploadProgressCallback } from '../../types/network.types' ;
13+ import { DownloadOptions , UploadOptions , UploadProgressCallback , DownloadProgressCallback } from '../../types/network.types' ;
1414import { CryptoService } from '../crypto.service' ;
1515import { UploadService } from './upload.service' ;
1616import { DownloadService } from './download.service' ;
1717import { ValidationService } from '../validation.service' ;
1818import { HashStream } from '../../utils/hash.utils' ;
19- import { ProgressTransform } from '../../utils/stream.utils' ;
2019import { RangeOptions } from '../../utils/network.utils' ;
2120
2221export class NetworkFacade {
@@ -54,6 +53,7 @@ export class NetworkFacade {
5453 bucketId : string ,
5554 mnemonic : string ,
5655 fileId : string ,
56+ size : number ,
5757 to : WritableStream ,
5858 rangeOptions ?: RangeOptions ,
5959 options ?: DownloadOptions ,
@@ -62,13 +62,10 @@ export class NetworkFacade {
6262 let fileStream : ReadableStream < Uint8Array > ;
6363 const abortable = options ?. abortController ?? new AbortController ( ) ;
6464
65- const onProgress : UploadProgressCallback = ( progress : number ) => {
65+ const onProgress : DownloadProgressCallback = ( loadedBytes : number ) => {
6666 if ( ! options ?. progressCallback ) return ;
67- options . progressCallback ( progress ) ;
68- } ;
69-
70- const onDownloadProgress = ( progress : number ) => {
71- onProgress ( progress ) ;
67+ const reportedProgress = Math . round ( ( loadedBytes / size ) * 100 ) ;
68+ options . progressCallback ( reportedProgress ) ;
7269 } ;
7370
7471 const decryptFile : DecryptFileFunction = async ( _ , key , iv ) => {
@@ -97,7 +94,7 @@ export class NetworkFacade {
9794 }
9895
9996 const encryptedContentStream = await this . downloadService . downloadFile ( downloadable . url , {
100- progressCallback : onDownloadProgress ,
97+ progressCallback : onProgress ,
10198 abortController : options ?. abortController ,
10299 rangeHeader : rangeOptions ?. range ,
103100 } ) ;
@@ -142,38 +139,31 @@ export class NetworkFacade {
142139 const hashStream = new HashStream ( ) ;
143140 const abortable = options ?. abortController ?? new AbortController ( ) ;
144141 let encryptionTransform : Transform ;
145- const progressTransform = new ProgressTransform ( { totalBytes : size } , ( progress ) => {
146- if ( options ?. progressCallback ) {
147- options . progressCallback ( progress * 0.95 ) ;
148- }
149- } ) ;
142+ let hash : Buffer ;
150143
151- const onProgress : UploadProgressCallback = ( progress : number ) => {
144+ const onProgress : UploadProgressCallback = ( loadedBytes : number ) => {
152145 if ( ! options ?. progressCallback ) return ;
153- options . progressCallback ( progress ) ;
146+ const reportedProgress = Math . round ( ( loadedBytes / size ) * 100 ) ;
147+ options . progressCallback ( reportedProgress ) ;
154148 } ;
155149
156150 const encryptFile : EncryptFileFunction = async ( _ , key , iv ) => {
157- encryptionTransform = from
158- . pipe (
159- await this . cryptoService . getEncryptionTransform (
160- Buffer . from ( key as ArrayBuffer ) ,
161- Buffer . from ( iv as ArrayBuffer ) ,
162- ) ,
163- )
164- . pipe ( hashStream ) ;
151+ const encryptionCipher = this . cryptoService . getEncryptionTransform (
152+ Buffer . from ( key as ArrayBuffer ) ,
153+ Buffer . from ( iv as ArrayBuffer ) ,
154+ ) ;
155+ encryptionTransform = from . pipe ( encryptionCipher ) . pipe ( hashStream ) ;
165156 } ;
166157
167158 const uploadFile : UploadFileFunction = async ( url ) => {
168- await this . uploadService . uploadFile ( url , encryptionTransform . pipe ( progressTransform ) , {
159+ await this . uploadService . uploadFile ( url , encryptionTransform , {
169160 abortController : abortable ,
170- progressCallback : ( ) => {
171- // No progress here, we are using the progressTransform
172- } ,
161+ progressCallback : onProgress ,
173162 } ) ;
174-
175- return hashStream . getHash ( ) . toString ( 'hex' ) ;
163+ hash = hashStream . getHash ( ) ;
164+ return hash . toString ( 'hex' ) ;
176165 } ;
166+
177167 const uploadOperation = async ( ) => {
178168 const uploadResult = await NetworkUpload . uploadFile (
179169 this . network ,
@@ -184,12 +174,10 @@ export class NetworkFacade {
184174 encryptFile ,
185175 uploadFile ,
186176 ) ;
187- const fileHash : Buffer = Buffer . from ( '' ) ;
188177
189- onProgress ( 1 ) ;
190178 return {
191179 fileId : uploadResult ,
192- hash : fileHash ,
180+ hash : hash ,
193181 } ;
194182 } ;
195183
0 commit comments