Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
345bd44323 |
18
util.go
18
util.go
@@ -37,7 +37,7 @@ func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
|
||||
// print entire body
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
fmt.Println(string(body))
|
||||
panic("bad status: " + res.Status)
|
||||
return AssetsReply{}, fmt.Errorf("bad status: %s", res.Status)
|
||||
}
|
||||
|
||||
// Process response
|
||||
@@ -80,7 +80,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
|
||||
if Debug {
|
||||
// create the directory if it doesn't exist
|
||||
if err := os.MkdirAll(downloadDir + "/fragments/" + filen, os.ModePerm); err != nil {
|
||||
if err := os.MkdirAll(downloadDir+"/fragments/"+filen, os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
downloaded := 0
|
||||
remContent := make([]byte, 0)
|
||||
currentRem := 0
|
||||
if (manifest.Remainder.Size > 0) {
|
||||
if manifest.Remainder.Size > 0 {
|
||||
url := transformShaToURL(manifest.Remainder.Sha512, manifest.Remainder.Size)
|
||||
content, err := getContent(url)
|
||||
if err != nil {
|
||||
@@ -97,7 +97,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
}
|
||||
remContent = content
|
||||
downloaded += len(content)
|
||||
|
||||
|
||||
if Debug {
|
||||
// save remainder to file in the fragments/ directory, prefix with gap
|
||||
if err := os.WriteFile(filepath.Join(downloadDir, "fragments", filen, fmt.Sprintf("%d.%s.%d.bytes", manifest.Remainder.Offsets[0], manifest.Remainder.Sha512, manifest.Remainder.Size)), remContent, os.ModePerm); err != nil {
|
||||
@@ -105,12 +105,13 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
trackFrag := make([]Frag, 0)
|
||||
|
||||
// start downloading chunks
|
||||
//currentOffset := 0
|
||||
ret := make([]byte, manifest.Size)
|
||||
totalMB := float32(manifest.Size) / 1024.0 / 1024.0
|
||||
for _, fragment := range manifest.Fragments {
|
||||
url := transformShaToURL(fragment.Sha512, fragment.Size)
|
||||
content, err := getContent(url)
|
||||
@@ -129,9 +130,10 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
percent := float32(downloaded) / float32(manifest.Size) * 100.0
|
||||
fmt.Printf("\r\033[32mDownloaded\033[0m %d/%d (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downloaded, manifest.Size, percent, filen)
|
||||
downedMB := float32(downloaded) / 1024.0 / 1024.0
|
||||
fmt.Printf("\r\033[32mDownloaded\033[0m %fMB/%fMB (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downedMB, totalMB, percent, filen)
|
||||
}
|
||||
|
||||
// sort trackFrag by Offset
|
||||
@@ -145,7 +147,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
||||
if frag.Offset > curPos {
|
||||
// gap detected
|
||||
gapSize := frag.Offset - curPos
|
||||
if currentRem + gapSize > len(remContent) {
|
||||
if currentRem+gapSize > len(remContent) {
|
||||
panic("not enough remainder content to fill gap")
|
||||
}
|
||||
copy(ret[curPos:curPos+gapSize], remContent[currentRem:currentRem+gapSize])
|
||||
|
||||
Reference in New Issue
Block a user