import BaseTask from "./BaseTask" ; /**
 * 分块任务
 */ class ChunkTask extends BaseTask { private _blocks : Block [ ] = [ ] ; private _blockSize : number = 0 ; private _chunkSize : number = 0 ; param file param blockSize param chunkSize constructor ( file , blockSize , chunkSize ) { super ( file ) ; this . _blockSize = blockSize ; this . _chunkSize = chunkSize ; this . spliceFile2Block ( ) ; } /**
     * 将文件分块
     */ public spliceFile2Block ( ) { this . _blocks = [ ] ; let fileSize = this . _file . size ; let file = this . _file ; let blockCount = Math . ceil ( fileSize / this . _blockSize ) ; for ( let i = 0 ; i < blockCount ; i ++ ) { let start = i * this . _blockSize ; let end = start + this . _blockSize ; let block = new Block ( start , end , file . slice ( start , end ) , this . _chunkSize , file ) ; this . _blocks . push ( block ) ; } } returns { Block [ ] } get blocks ( ) : Block [ ] { return this . _blocks ; } returns { Block } get processingBlock ( ) : Block { for ( let block of this . _blocks ) { if ( ! block . processing ) { continue ; } return block ; } throw Error ( "找不到正在处理的Block" ) ; } get finishedBlocksSize ( ) : number { let size = 0 ; for ( let block of this . _blocks ) { size += ( block . isFinish ? block . data . size : 0 ) ; } return size ; } get chunks ( ) : Chunk [ ] { let array = [ ] ; for ( let block of this . _blocks ) { for ( let chunk of block . chunks ) { array . push ( chunk ) ; } } return array ; } returns { Block } get processingChunk ( ) : Chunk { for ( let block of this . _blocks ) { if ( ! block . processing ) { continue ; } for ( let chunk of block . chunks ) { if ( ! chunk . processing ) { continue ; } return chunk ; } } throw Error ( "找不到正在处理的Chunk" ) ; } returns { number } get totalChunkCount ( ) : number { let count = 0 ; for ( let block of this . _blocks ) { count += block . chunks . length ; } return count ; } } /**
 * 分块，分块大小七牛固定是4M
 */ class Block { private _data : Blob ; private _start : number ; private _end : number ; private _chunks : Chunk [ ] = [ ] ; private _isFinish : boolean = false ; private _processing : boolean = false ; private _file : File ; param start param end param data param chunkSize param file constructor ( start , end , data , chunkSize , file ) { this . _data = data ; this . _start = start ; this . _end = end ; this . _file = file ; this . spliceBlock2Chunk ( chunkSize ) ; } /**
     * 将块分片
     */ private spliceBlock2Chunk ( chunkSize ) { let blockSize = this . _data . size ; let data = this . _data ; let chunkCount = Math . ceil ( blockSize / chunkSize ) ; for ( let i = 0 ; i < chunkCount ; i ++ ) { let start = i * chunkSize ; let end = start + chunkSize ; let chunk = new Chunk ( start , end , data . slice ( start , end ) , this ) ; this . _chunks . push ( chunk ) ; } } returns { boolean } get processing ( ) : boolean { return this . _processing ; } set processing ( value ) { this . _processing = value ; } returns { File } get file ( ) : File { return this . _file ; } returns { boolean } get isFinish ( ) : boolean { return this . _isFinish ; } set isFinish ( value ) { this . _isFinish = value ; } returns { Blob } get data ( ) : Blob { return this . _data ; } returns { number } get start ( ) : number { return this . _start ; } returns { number } get end ( ) : number { return this . _end ; } get chunks ( ) : Chunk [ ] { return this . _chunks ; } } /**
 * 分片，分片大小可以自定义，至少1字节
 */ class Chunk { private _start : number ; private _end : number ; private _data : Blob ; private _processing : boolean = false ; private _isFinish : boolean = false ; private _ctx : string ; private _block : Block ; private _host : string ; param start param end param data param block constructor ( start , end , data , block ) { this . _start = start ; this . _end = end ; this . _data = data ; this . _block = block ; } returns { Block } get block ( ) : Block { return this . _block ; } returns { number } get start ( ) : number { return this . _start ; } returns { number } get end ( ) : number { return this . _end ; } returns { Blob } get data ( ) : Blob { return this . _data ; } returns { boolean } get isFinish ( ) : boolean { return this . _isFinish ; } set isFinish ( value ) { this . _isFinish = value ; } get host ( ) : string { return this . _host ; } set host ( value ) { this . _host = value ; } returns { boolean } get processing ( ) : boolean { return this . _processing ; } set processing ( value ) { this . _processing = value ; } returns { string } get ctx ( ) : string { return this . _ctx ; } set ctx ( value ) { this . _ctx = value ; } } export { ChunkTask , Block , Chunk } ;