@@ -10,7 +10,7 @@ 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' ;
@@ -62,9 +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 ) ;
67+ const reportedProgress = Math . round ( ( loadedBytes / size ) * 100 ) ;
68+ options . progressCallback ( reportedProgress ) ;
6869 } ;
6970
7071 const decryptFile : DecryptFileFunction = async ( _ , key , iv ) => {
@@ -92,7 +93,7 @@ export class NetworkFacade {
9293 throw new Error ( 'Download aborted' ) ;
9394 }
9495
95- const encryptedContentStream = await this . downloadService . downloadFile ( downloadable . url , size , {
96+ const encryptedContentStream = await this . downloadService . downloadFile ( downloadable . url , {
9697 progressCallback : onProgress ,
9798 abortController : options ?. abortController ,
9899 rangeHeader : rangeOptions ?. range ,
@@ -140,9 +141,10 @@ export class NetworkFacade {
140141 let encryptionTransform : Transform ;
141142 let hash : Buffer ;
142143
143- const onProgress : UploadProgressCallback = ( progress : number ) => {
144+ const onProgress : UploadProgressCallback = ( loadedBytes : number ) => {
144145 if ( ! options ?. progressCallback ) return ;
145- options . progressCallback ( progress ) ;
146+ const reportedProgress = Math . round ( ( loadedBytes / size ) * 100 ) ;
147+ options . progressCallback ( reportedProgress ) ;
146148 } ;
147149
148150 const encryptFile : EncryptFileFunction = async ( _ , key , iv ) => {
@@ -157,7 +159,7 @@ export class NetworkFacade {
157159 } ;
158160
159161 const uploadFile : UploadFileFunction = async ( url ) => {
160- await this . uploadService . uploadFile ( url , size , encryptionTransform , {
162+ await this . uploadService . uploadFile ( url , encryptionTransform , {
161163 abortController : abortable ,
162164 progressCallback : onProgress ,
163165 } ) ;
0 commit comments