MCPcopy
hub / github.com/cli/cli / downloadAsset

Function downloadAsset

pkg/cmd/release/download/download.go:273–324  ·  view source on GitHub ↗
(dest *destinationWriter, httpClient *http.Client, assetURL, fileName string, isArchive bool)

Source from the content-addressed store, hash-verified

271}
272
273func 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
326var codeloadLegacyRE = regexp.MustCompile(`^(/[^/]+/[^/]+/)legacy\.`)
327

Callers 1

downloadAssetsFunction · 0.70

Calls 10

HandleHTTPErrorFunction · 0.92
CheckMethod · 0.80
BaseMethod · 0.80
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
ErrorfMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected