(dest *destinationWriter, httpClient *http.Client, assetURL, fileName string, isArchive bool)
| 273 | } |
| 274 | |
| 275 | func downloadAsset(dest *destinationWriter, httpClient *http.Client, assetURL, fileName string, isArchive bool) error { |
| 276 | if err := dest.Check(fileName); err != nil { |
| 277 | return err |
| 278 | } |
| 279 | |
| 280 | req, err := http.NewRequest("GET", assetURL, nil) |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | |
| 285 | req.Header.Set("Accept", "application/octet-stream") |
| 286 | if isArchive { |
| 287 | // adding application/json to Accept header due to a bug in the zipball/tarball API endpoint that makes it mandatory |
| 288 | req.Header.Set("Accept", "application/octet-stream, application/json") |
| 289 | |
| 290 | // override HTTP redirect logic to avoid "legacy" Codeload resources |
| 291 | oldClient := *httpClient |
| 292 | httpClient = &oldClient |
| 293 | httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 294 | if len(via) == 1 { |
| 295 | req.URL.Path = removeLegacyFromCodeloadPath(req.URL.Path) |
| 296 | } |
| 297 | return nil |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | resp, err := httpClient.Do(req) |
| 302 | if err != nil { |
| 303 | return err |
| 304 | } |
| 305 | defer resp.Body.Close() |
| 306 | |
| 307 | if resp.StatusCode > 299 { |
| 308 | return api.HandleHTTPError(resp) |
| 309 | } |
| 310 | |
| 311 | if len(fileName) == 0 { |
| 312 | contentDisposition := resp.Header.Get("Content-Disposition") |
| 313 | |
| 314 | _, params, err := mime.ParseMediaType(contentDisposition) |
| 315 | if err != nil { |
| 316 | return fmt.Errorf("unable to parse file name of archive: %w", err) |
| 317 | } |
| 318 | if serverFileName, ok := params["filename"]; ok { |
| 319 | fileName = filepath.Base(serverFileName) |
| 320 | } else { |
| 321 | return errors.New("unable to determine file name of archive") |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return dest.Copy(fileName, resp.Body) |
| 326 | } |
| 327 | |
| 328 | var codeloadLegacyRE = regexp.MustCompile(`^(/[^/]+/[^/]+/)legacy\.`) |
| 329 |
no test coverage detected