fix(importer): correctly parse curseforge download url JSON response
This commit is contained in:
@@ -57,8 +57,8 @@ func (i *CurseForgeImporter) downloadAndProcessFile(url string) (hash string, si
|
||||
|
||||
// getFileDownloadURL получает прямую ссылку на скачивание файла с API CurseForge.
|
||||
func (i *CurseForgeImporter) getFileDownloadURL(projectID, fileID int) (string, error) {
|
||||
url := fmt.Sprintf("https://api.curseforge.com/v1/mods/%d/files/%d/download-url", projectID, fileID)
|
||||
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
|
||||
apiURL := fmt.Sprintf("https://api.curseforge.com/v1/mods/%d/files/%d/download-url", projectID, fileID)
|
||||
req, err := http.NewRequestWithContext(context.Background(), "GET", apiURL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -71,27 +71,21 @@ func (i *CurseForgeImporter) getFileDownloadURL(projectID, fileID int) (string,
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
if resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusMovedPermanently {
|
||||
location, err := resp.Location()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get redirect location: %w", err)
|
||||
}
|
||||
return location.String(), nil
|
||||
}
|
||||
return "", fmt.Errorf("bad status getting download URL: %s", resp.Status)
|
||||
}
|
||||
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read download URL response body: %w", err)
|
||||
var result struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return "", fmt.Errorf("failed to decode download URL response: %w", err)
|
||||
}
|
||||
|
||||
downloadURL := string(bodyBytes)
|
||||
if downloadURL == "" {
|
||||
return "", fmt.Errorf("received empty download URL")
|
||||
if result.Data == "" {
|
||||
return "", fmt.Errorf("received empty download URL from API")
|
||||
}
|
||||
|
||||
return downloadURL, nil
|
||||
return result.Data, nil
|
||||
}
|
||||
|
||||
// findModpackBySlug ищет ID проекта по его "слагу" (части URL).
|
||||
|
||||
@@ -40,9 +40,3 @@ type CurseForgeFilesResponse struct {
|
||||
TotalCount int `json:"totalCount"`
|
||||
} `json:"pagination"`
|
||||
}
|
||||
|
||||
type CurseForgeFileResponse struct {
|
||||
Data struct {
|
||||
DownloadURL string `json:"downloadUrl"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user