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