MCPcopy Create free account
hub / github.com/imroc/req / handleDownload

Function handleDownload

middleware.go:410–450  ·  view source on GitHub ↗
(c *Client, r *Response)

Source from the content-addressed store, hash-verified

408}
409
410func handleDownload(c *Client, r *Response) (err error) {
411 if r.Response == nil || !r.Request.isSaveResponse {
412 return nil
413 }
414 var body io.ReadCloser
415
416 if r.body != nil { // already read
417 body = io.NopCloser(bytes.NewReader(r.body))
418 } else {
419 body = r.Body
420 }
421
422 var output io.Writer
423 if r.Request.outputFile != "" {
424 file := r.Request.outputFile
425 if c.outputDirectory != "" && !filepath.IsAbs(file) {
426 file = c.outputDirectory + string(filepath.Separator) + file
427 }
428
429 file = filepath.Clean(file)
430
431 if err = util.CreateDirectory(filepath.Dir(file)); err != nil {
432 return err
433 }
434 output, err = os.Create(file)
435 if err != nil {
436 return
437 }
438 } else {
439 output = r.Request.output // must not nil
440 }
441
442 defer func() {
443 body.Close()
444 closeq(output)
445 }()
446
447 _, err = io.Copy(output, body)
448 r.setReceivedAt()
449 return
450}
451
452// generate URL
453func parseRequestURL(c *Client, r *Request) error {

Callers

nothing calls this directly

Calls 4

CreateDirectoryFunction · 0.92
closeqFunction · 0.85
setReceivedAtMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…