1package tq
2
3import (
4	"net/http"
5
6	"github.com/git-lfs/git-lfs/v3/lfsapi"
7	"github.com/git-lfs/git-lfs/v3/tools"
8	"github.com/rubyist/tracerx"
9)
10
11const (
12	maxVerifiesConfigKey     = "lfs.transfer.maxverifies"
13	defaultMaxVerifyAttempts = 3
14)
15
16func verifyUpload(c *lfsapi.Client, remote string, t *Transfer) error {
17	action, err := t.Actions.Get("verify")
18	if err != nil {
19		return err
20	}
21	if action == nil {
22		return nil
23	}
24
25	req, err := http.NewRequest("POST", action.Href, nil)
26	if err != nil {
27		return err
28	}
29
30	err = lfsapi.MarshalToRequest(req, struct {
31		Oid  string `json:"oid"`
32		Size int64  `json:"size"`
33	}{Oid: t.Oid, Size: t.Size})
34	if err != nil {
35		return err
36	}
37
38	req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
39	req.Header.Set("Accept", "application/vnd.git-lfs+json")
40	for key, value := range action.Header {
41		req.Header.Set(key, value)
42	}
43
44	mv := c.GitEnv().Int(maxVerifiesConfigKey, defaultMaxVerifyAttempts)
45	mv = tools.MaxInt(defaultMaxVerifyAttempts, mv)
46	req = c.LogRequest(req, "lfs.verify")
47
48	for i := 1; i <= mv; i++ {
49		tracerx.Printf("tq: verify %s attempt #%d (max: %d)", t.Oid[:7], i, mv)
50
51		var res *http.Response
52		if t.Authenticated {
53			res, err = c.Do(req)
54		} else {
55			res, err = c.DoWithAuth(remote, c.Endpoints.AccessFor(action.Href), req)
56		}
57
58		if err != nil {
59			tracerx.Printf("tq: verify err: %+v", err.Error())
60		} else {
61			err = res.Body.Close()
62			break
63		}
64	}
65	return err
66}
67