import pako from 'pako' ; import debugging from 'debug' ; import { TransferFile } from "../../types" ; import UploadProvider from "./UploadProvider" ; import Transfer from '../Transfer' ; const debug = debugging ( "blymp:CompressionUploadProvider" ) ; /**
 * Compression Upload Provider: This provider wraps around other Upload Providers
 * to compress uploaded files.
 *
 * This class works in conjunction with the "Decompress" download processor
 */ export default class CompressionUploadProvider implements UploadProvider { /**
     * Upload provider we are wrapping
     */ private provider : UploadProvider ; /**
     * Current parent transfer
     */ private transfer : Transfer ; /**
     * Data for the compressed file
     */ private compressedFile : Uint8Array | undefined ; constructor ( provider , transfer ) { debug ( "Created Compression Wrapper for provider" , provider ) ; this . provider = provider ; this . transfer = transfer ; } getEstimatedTotalSize ( ) { return this . provider . getEstimatedTotalSize ( ) * 0.75 ; } getDownloadProcessor ( ) { return "DecompressProcessor" ; } getNumberOfFiles ( ) { return this . provider . getNumberOfFiles ( ) ; } getFileInfo ( index , beforePrepare ) { if ( ! this . compressedFile && ! beforePrepare ) { throw new Error ( 'Internal error: Compressed file does not exist yet' ) ; } const generalInfo = this . provider . getFileInfo ( index , beforePrepare ) ; if ( beforePrepare || ! this . compressedFile ) { return generalInfo ; } return { ... generalInfo , size : this . compressedFile . byteLength } ; } getFileSlice ( _ , start , end ) { if ( ! this . compressedFile ) { throw new Error ( 'Internal error: Compressed file does not exist yet' ) ; } const slice = this . compressedFile . slice ( start , end ) ; return Promise . resolve ( slice ) ; } prepareFile ( index ) { debug ( "Compressing file before transfer" ) ; this . transfer . setTransferStatusText ( "Compressing file before sending..." ) ; return new Promise ( async ( resolve ) => { debug ( "Reading file into RAM" ) ; await this . provider . prepareFile ( index ) ; const fileInfo = this . provider . getFileInfo ( index , false ) ; const fileContent = await this . provider . getFileSlice ( index , 0 , fileInfo . size ) ; debug ( "File read done, compressing file now" ) ; const data = new Uint8Array ( fileContent as ArrayBufferLike ) ; this . compressedFile = pako . deflate ( data ) ; debug ( "File compression done" ) ; this . transfer . setTransferStatusText ( "Transferring compressed file..." ) ; resolve ( ) ; } ) ; } }