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