diff --git a/util.go b/util.go index 7109c24..9460c69 100644 --- a/util.go +++ b/util.go @@ -13,12 +13,12 @@ import ( var Debug = false -func DoAssetsRequest(addonID, version string) AssetsReply { +func DoAssetsRequest(addonID, version string) (AssetsReply, error) { url := "https://api-ar-workshop.bistudio.com/workshop-api/api/v3.0/s2s/assets/download-list" body := `{"assets":{"` + addonID + `":"` + version + `"}}` req, err := http.NewRequest("POST", url, strings.NewReader(body)) if err != nil { - panic(err) + return AssetsReply{}, err } req.Header.Add("x-client-id", "$edb1b7862bba5cade1f6e06bfdeac2c") @@ -29,7 +29,7 @@ func DoAssetsRequest(addonID, version string) AssetsReply { res, err := http.DefaultClient.Do(req) if err != nil { - panic(err) + return AssetsReply{}, err } defer res.Body.Close() @@ -47,24 +47,24 @@ func DoAssetsRequest(addonID, version string) AssetsReply { panic(err) } - return assetsReply + return assetsReply, nil } -func GetManifest(manifestUrl string) Manifest { +func GetManifest(manifestUrl string) (Manifest, error) { url := "https://ar-gcp-cdn.bistudio.com/manifest/" + manifestUrl req, err := http.NewRequest("GET", url, nil) if err != nil { - panic(err) + return Manifest{}, err } res, err := http.DefaultClient.Do(req) if err != nil { - panic(err) + return Manifest{}, err } defer res.Body.Close() if res.StatusCode != 200 { - panic("bad status: " + res.Status) + return Manifest{}, fmt.Errorf("bad status: %s", res.Status) } // Decode the JSON response @@ -73,15 +73,15 @@ func GetManifest(manifestUrl string) Manifest { panic(err) } - return manifest + return manifest, nil } -func Download(manifest Manifest, downloadDir string, filen string) []byte { +func Download(manifest Manifest, downloadDir string, filen string) ([]byte, error) { if Debug { // create the directory if it doesn't exist if err := os.MkdirAll(downloadDir + "/fragments/" + filen, os.ModePerm); err != nil { - panic(err) + return nil, err } } @@ -91,14 +91,17 @@ func Download(manifest Manifest, downloadDir string, filen string) []byte { currentRem := 0 if (manifest.Remainder.Size > 0) { url := transformShaToURL(manifest.Remainder.Sha512, manifest.Remainder.Size) - content := getContent(url) + content, err := getContent(url) + if err != nil { + return nil, err + } 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 { - panic(err) + return nil, err } } } @@ -110,7 +113,10 @@ func Download(manifest Manifest, downloadDir string, filen string) []byte { ret := make([]byte, manifest.Size) for _, fragment := range manifest.Fragments { url := transformShaToURL(fragment.Sha512, fragment.Size) - content := getContent(url) + content, err := getContent(url) + if err != nil { + return nil, err + } downloaded += len(content) * len(fragment.Offsets) for _, offset := range fragment.Offsets { trackFrag = append(trackFrag, Frag{Offset: offset, Size: len(content)}) @@ -120,7 +126,7 @@ func Download(manifest Manifest, downloadDir string, filen string) []byte { if Debug { // save fragment to file in the fragments/ directory if err := os.WriteFile(filepath.Join(downloadDir, "fragments", filen, fmt.Sprintf("%d.%s.%d.bytes", fragment.Offsets[0], fragment.Sha512, fragment.Size)), content, os.ModePerm); err != nil { - panic(err) + return nil, err } } @@ -152,26 +158,26 @@ func Download(manifest Manifest, downloadDir string, filen string) []byte { copy(ret[curPos:], remContent) fmt.Println() - return ret + return ret, nil } -func getContent(url string) []byte { +func getContent(url string) ([]byte, error) { resp, err := http.Get(url) if err != nil { - panic(err) + return nil, err } defer resp.Body.Close() if resp.StatusCode != 200 { - panic("bad status: " + resp.Status) + return nil, fmt.Errorf("bad status: %s", resp.Status) } body, err := io.ReadAll(resp.Body) if err != nil { - panic(err) + return nil, err } - return body + return body, nil } func transformShaToURL(sha512s string, size int) string {