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

Function downloadAsset

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

Source from the content-addressed store, hash-verified

273}
274
275func 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
328var codeloadLegacyRE = regexp.MustCompile(`^(/[^/]+/[^/]+/)legacy\.`)
329

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