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