github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/requester/transfer/download_instanceinfo.go (about)

     1  package transfer
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  type (
     8  	//DownloadInstanceInfo 状态详细信息, 用于导出状态文件
     9  	DownloadInstanceInfo struct {
    10  		DownloadStatus *DownloadStatus
    11  		Ranges         RangeList
    12  	}
    13  
    14  	// DownloadInstanceInfoExporter 断点续传类型接口
    15  	DownloadInstanceInfoExporter interface {
    16  		GetInstanceInfo() *DownloadInstanceInfo
    17  		SetInstanceInfo(*DownloadInstanceInfo)
    18  	}
    19  )
    20  
    21  // GetInstanceInfo 从断点信息获取下载状态
    22  func (m *DownloadInstanceInfoExport) GetInstanceInfo() (eii *DownloadInstanceInfo) {
    23  	eii = &DownloadInstanceInfo{
    24  		Ranges: m.Ranges,
    25  	}
    26  
    27  	var downloaded int64
    28  	switch m.RangeGenMode {
    29  	case RangeGenMode_BlockSize:
    30  		downloaded = m.GenBegin - eii.Ranges.Len()
    31  	default:
    32  		downloaded = m.TotalSize - eii.Ranges.Len()
    33  	}
    34  	eii.DownloadStatus = &DownloadStatus{
    35  		startTime:  time.Now(),
    36  		totalSize:  m.TotalSize,
    37  		downloaded: downloaded,
    38  		gen:        NewRangeListGenBlockSize(m.TotalSize, m.GenBegin, m.BlockSize),
    39  	}
    40  	switch m.RangeGenMode {
    41  	case RangeGenMode_BlockSize:
    42  		eii.DownloadStatus.gen = NewRangeListGenBlockSize(m.TotalSize, m.GenBegin, m.BlockSize)
    43  	default:
    44  		eii.DownloadStatus.gen = NewRangeListGenDefault(m.TotalSize, m.TotalSize, len(m.Ranges), len(m.Ranges))
    45  	}
    46  	return eii
    47  }
    48  
    49  // SetInstanceInfo 从下载状态导出断点信息
    50  func (m *DownloadInstanceInfoExport) SetInstanceInfo(eii *DownloadInstanceInfo) {
    51  	if eii == nil {
    52  		return
    53  	}
    54  
    55  	if eii.DownloadStatus != nil {
    56  		m.TotalSize = eii.DownloadStatus.TotalSize()
    57  		if eii.DownloadStatus.gen != nil {
    58  			m.GenBegin = eii.DownloadStatus.gen.LoadBegin()
    59  			m.BlockSize = eii.DownloadStatus.gen.LoadBlockSize()
    60  			m.RangeGenMode = eii.DownloadStatus.gen.RangeGenMode()
    61  		} else {
    62  			m.RangeGenMode = RangeGenMode_Default
    63  		}
    64  	}
    65  	m.Ranges = eii.Ranges
    66  }