MCPcopy
hub / github.com/perkeep/perkeep / fetchVia

Method fetchVia

pkg/client/get.go:74–155  ·  view source on GitHub ↗
(ctx context.Context, b blob.Ref, v []blob.Ref)

Source from the content-addressed store, hash-verified

72var blobsRx = regexp.MustCompile(blob.Pattern)
73
74func (c *Client) fetchVia(ctx context.Context, b blob.Ref, v []blob.Ref) (body io.ReadCloser, size uint32, err error) {
75 if c.sto != nil {
76 if len(v) > 0 {
77 return nil, 0, errors.New("FetchVia not supported in non-HTTP mode")
78 }
79 return c.sto.Fetch(ctx, b)
80 }
81 pfx, err := c.blobPrefix()
82 if err != nil {
83 return nil, 0, err
84 }
85 url := fmt.Sprintf("%s/%s", pfx, b)
86
87 if len(v) > 0 {
88 buf := bytes.NewBufferString(url)
89 buf.WriteString("?via=")
90 for i, br := range v {
91 if i != 0 {
92 buf.WriteString(",")
93 }
94 buf.WriteString(br.String())
95 }
96 url = buf.String()
97 }
98
99 req := c.newRequest(ctx, "GET", url)
100 resp, err := c.httpClient.Do(req)
101 if err != nil {
102 return nil, 0, err
103 }
104 defer func() {
105 if err != nil {
106 resp.Body.Close()
107 }
108 }()
109 if resp.StatusCode == http.StatusNotFound {
110 // Per blob.Fetcher contract:
111 return nil, 0, os.ErrNotExist
112 }
113 if resp.StatusCode != http.StatusOK {
114 return nil, 0, fmt.Errorf("Got status code %d from blobserver for %s", resp.StatusCode, b)
115 }
116
117 var reader io.Reader = resp.Body
118 var closer io.Closer = resp.Body
119 if resp.ContentLength > 0 {
120 if resp.ContentLength > math.MaxUint32 {
121 return nil, 0, fmt.Errorf("Blob %s over %d bytes", b, uint32(math.MaxUint32))
122 }
123 size = uint32(resp.ContentLength)
124 } else {
125 var buf bytes.Buffer
126 size = 0
127 // Might be compressed. Slurp it to memory.
128 n, err := io.CopyN(&buf, resp.Body, constants.MaxBlobSize+1)
129 if n > blobserver.MaxBlobSize {
130 return nil, 0, fmt.Errorf("Blob %s over %d bytes; not reading more", b, blobserver.MaxBlobSize)
131 }

Callers 1

FetchMethod · 0.95

Calls 6

blobPrefixMethod · 0.95
newRequestMethod · 0.95
UpdateShareChainMethod · 0.95
FetchMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected