| 1 | package tq |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "os" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/lfsapi" |
| 11 | "github.com/git-lfs/git-lfs/v3/tools" |
| 12 | "github.com/git-lfs/git-lfs/v3/tr" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | TusAdapterName = "tus" |
| 17 | TusVersion = "1.0.0" |
| 18 | ) |
| 19 | |
| 20 | // Adapter for tus.io protocol resumaable uploads |
| 21 | type tusUploadAdapter struct { |
| 22 | *adapterBase |
| 23 | } |
| 24 | |
| 25 | func (a *tusUploadAdapter) WorkerStarting(workerNum int) (interface{}, error) { |
| 26 | return nil, nil |
| 27 | } |
| 28 | func (a *tusUploadAdapter) WorkerEnding(workerNum int, ctx interface{}) { |
| 29 | } |
| 30 | |
| 31 | func (a *tusUploadAdapter) DoTransfer(ctx interface{}, t *Transfer, cb ProgressCallback, authOkFunc func()) error { |
| 32 | rel, err := t.Rel("upload") |
| 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if rel == nil { |
| 37 | return errors.New(tr.Tr.Get("No upload action for object: %s", t.Oid)) |
| 38 | } |
| 39 | |
| 40 | // Note not supporting the Creation extension since the batch API generates URLs |
| 41 | // Also not supporting Concatenation to support parallel uploads of chunks; forward only |
| 42 | |
| 43 | // 1. Send HEAD request to determine upload start point |
| 44 | // Request must include Tus-Resumable header (version) |
| 45 | a.Trace("xfer: sending tus.io HEAD request for %q", t.Oid) |
| 46 | req, err := a.newHTTPRequest("HEAD", rel) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | |
| 51 | req.Header.Set("Tus-Resumable", TusVersion) |
| 52 | |
| 53 | res, err := a.doHTTP(t, req) |
| 54 | if err != nil { |
| 55 | return errors.NewRetriableError(err) |
| 56 | } |
| 57 | |
| 58 | // Response will contain Upload-Offset if supported |
| 59 | offHdr := res.Header.Get("Upload-Offset") |
| 60 | if len(offHdr) == 0 { |
| 61 | return errors.New(tr.Tr.Get("missing Upload-Offset header from tus.io HEAD response at %q, contact server admin", rel.Href)) |
| 62 | } |
| 63 | offset, err := strconv.ParseInt(offHdr, 10, 64) |
| 64 | if err != nil || offset < 0 { |
| 65 | return errors.New(tr.Tr.Get("invalid Upload-Offset value %q in response from tus.io HEAD at %q, contact server admin", offHdr, rel.Href)) |
| 66 | } |
| 67 | // Upload-Offset=size means already completed (skip) |
| 68 | // Batch API will probably already detect this, but handle just in case |
| 69 | if offset >= t.Size { |
| 70 | a.Trace("xfer: tus.io HEAD offset %d indicates %q is already fully uploaded, skipping", offset, t.Oid) |
| 71 | advanceCallbackProgress(cb, t, t.Size) |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // Open file for uploading |
| 76 | f, err := os.OpenFile(t.Path, os.O_RDONLY, 0644) |
| 77 | if err != nil { |
| 78 | return errors.Wrap(err, tr.Tr.Get("tus.io upload")) |
| 79 | } |
| 80 | defer f.Close() |
| 81 | |
| 82 | // Upload-Offset=0 means start from scratch, but still send PATCH |
| 83 | if offset == 0 { |
| 84 | a.Trace("xfer: tus.io uploading %q from start", t.Oid) |
| 85 | } else { |
| 86 | a.Trace("xfer: tus.io resuming upload %q from %d", t.Oid, offset) |
| 87 | advanceCallbackProgress(cb, t, offset) |
| 88 | } |
| 89 | |
| 90 | // 2. Send PATCH request with byte start point (even if 0) in Upload-Offset |
| 91 | // Response status must be 204 |
| 92 | // Response Upload-Offset must be request Upload-Offset plus sent bytes |
| 93 | // Response may include Upload-Expires header in which case check not passed |
| 94 | |
| 95 | a.Trace("xfer: sending tus.io PATCH request for %q", t.Oid) |
| 96 | req, err = a.newHTTPRequest("PATCH", rel) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | req.Header.Set("Tus-Resumable", TusVersion) |
| 102 | req.Header.Set("Upload-Offset", strconv.FormatInt(offset, 10)) |
| 103 | req.Header.Set("Content-Type", "application/offset+octet-stream") |
| 104 | req.Header.Set("Content-Length", strconv.FormatInt(t.Size-offset, 10)) |
| 105 | req.ContentLength = t.Size - offset |
| 106 | |
| 107 | // Ensure progress callbacks made while uploading |
| 108 | // Wrap callback to give name context |
| 109 | ccb := func(totalSize int64, readSoFar int64, readSinceLast int) error { |
| 110 | if cb != nil { |
| 111 | return cb(t.Name, totalSize, readSoFar, readSinceLast) |
| 112 | } |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | var reader lfsapi.ReadSeekCloser = tools.NewBodyWithCallback(f, t.Size, ccb) |
| 117 | reader = newStartCallbackReader(reader, func() error { |
| 118 | // seek to the offset since lfsapi.Client rewinds the body |
| 119 | if _, err := f.Seek(offset, io.SeekCurrent); err != nil { |
| 120 | return err |
| 121 | } |
| 122 | // Signal auth was ok on first read; this frees up other workers to start |
| 123 | if authOkFunc != nil { |
| 124 | authOkFunc() |
| 125 | } |
| 126 | return nil |
| 127 | }) |
| 128 | |
| 129 | req.Body = reader |
| 130 | |
| 131 | req = a.apiClient.LogRequest(req, "lfs.data.upload") |
| 132 | res, err = a.doHTTP(t, req) |
| 133 | if err != nil { |
| 134 | return errors.NewRetriableError(err) |
| 135 | } |
| 136 | |
| 137 | // A status code of 403 likely means that an authentication token for the |
| 138 | // upload has expired. This can be safely retried. |
| 139 | if res.StatusCode == 403 { |
| 140 | err = errors.New(tr.Tr.Get("Received status %d", res.StatusCode)) |
| 141 | return errors.NewRetriableError(err) |
| 142 | } |
| 143 | |
| 144 | if res.StatusCode > 299 { |
| 145 | return errors.New(tr.Tr.Get("Invalid status for %s %s: %d", |
| 146 | req.Method, |
| 147 | strings.SplitN(req.URL.String(), "?", 2)[0], |
| 148 | res.StatusCode, |
| 149 | )) |
| 150 | } |
| 151 | |
| 152 | io.Copy(io.Discard, res.Body) |
| 153 | res.Body.Close() |
| 154 | |
| 155 | return verifyUpload(a.apiClient, a.remote, t) |
| 156 | } |
| 157 | |
| 158 | func configureTusAdapter(m *concreteManifest) { |
| 159 | m.RegisterNewAdapterFunc(TusAdapterName, Upload, false, func(name string, dir Direction) Adapter { |
| 160 | switch dir { |
| 161 | case Upload: |
| 162 | bu := &tusUploadAdapter{newAdapterBase(m.fs, name, dir, nil)} |
| 163 | // self implements impl |
| 164 | bu.transferImpl = bu |
| 165 | return bu |
| 166 | case Download: |
| 167 | panic(tr.Tr.Get("Should never ask this function to download")) |
| 168 | } |
| 169 | return nil |
| 170 | }) |
| 171 | } |