github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/requester/uploader/instance_state.go (about)

     1  package uploader
     2  
     3  import (
     4  	"github.com/iikira/iikira-go-utils/requester/transfer"
     5  )
     6  
     7  type (
     8  	// BlockState 文件区块信息
     9  	BlockState struct {
    10  		ID       int            `json:"id"`
    11  		Range    transfer.Range `json:"range"`
    12  		CheckSum string         `json:"checksum"`
    13  	}
    14  
    15  	// InstanceState 上传断点续传信息
    16  	InstanceState struct {
    17  		BlockList []*BlockState `json:"block_list"`
    18  	}
    19  )
    20  
    21  func (muer *MultiUploader) getWorkerListByInstanceState(is *InstanceState) workerList {
    22  	workers := make(workerList, 0, len(is.BlockList))
    23  	for _, blockState := range is.BlockList {
    24  		if blockState.CheckSum == "" {
    25  			workers = append(workers, &worker{
    26  				id:         blockState.ID,
    27  				partOffset: blockState.Range.Begin,
    28  				splitUnit:  NewBufioSplitUnit(muer.file, blockState.Range, muer.speedsStat, muer.rateLimit),
    29  				checksum:   blockState.CheckSum,
    30  			})
    31  		} else {
    32  			// 已经完成的, 也要加入 (可继续优化)
    33  			workers = append(workers, &worker{
    34  				id:         blockState.ID,
    35  				partOffset: blockState.Range.Begin,
    36  				splitUnit: &fileBlock{
    37  					readRange: blockState.Range,
    38  					readed:    blockState.Range.End - blockState.Range.Begin,
    39  					readerAt:  muer.file,
    40  				},
    41  				checksum: blockState.CheckSum,
    42  			})
    43  		}
    44  	}
    45  	return workers
    46  }