forked from jstaf/onedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_session.go
333 lines (305 loc) · 10.7 KB
/
upload_session.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package fs
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/jstaf/onedriver/fs/graph"
"github.com/rs/zerolog/log"
)
const (
// 10MB is the recommended upload size according to the graph API docs
uploadChunkSize uint64 = 10 * 1024 * 1024
// uploads larget than 4MB must use a formal upload session
uploadLargeSize uint64 = 4 * 1024 * 1024
)
// upload states
const (
uploadNotStarted = iota
uploadStarted
uploadComplete
uploadErrored
)
// UploadSession contains a snapshot of the file we're uploading. We have to
// take the snapshot or the file may have changed on disk during upload (which
// would break the upload). It is not recommended to directly deserialize into
// this structure from API responses in case Microsoft ever adds a size, data,
// or modTime field to the response.
type UploadSession struct {
ID string `json:"id"`
OldID string `json:"oldID"`
ParentID string `json:"parentID"`
NodeID uint64 `json:"nodeID"`
Name string `json:"name"`
ExpirationDateTime time.Time `json:"expirationDateTime"`
Size uint64 `json:"size,omitempty"`
Data []byte `json:"data,omitempty"`
QuickXORHash string `json:"quickxorhash,omitempty"`
ModTime time.Time `json:"modTime,omitempty"`
retries int
sync.Mutex
UploadURL string `json:"uploadUrl"`
ETag string `json:"eTag,omitempty"`
state int
error // embedded error tracks errors that killed an upload
}
// MarshalJSON implements a custom JSON marshaler to avoid race conditions
func (u *UploadSession) MarshalJSON() ([]byte, error) {
u.Lock()
defer u.Unlock()
type SerializeableUploadSession UploadSession
return json.Marshal((*SerializeableUploadSession)(u))
}
// UploadSessionPost is the initial post used to create an upload session
type UploadSessionPost struct {
Name string `json:"name,omitempty"`
ConflictBehavior string `json:"@microsoft.graph.conflictBehavior,omitempty"`
FileSystemInfo `json:"fileSystemInfo,omitempty"`
}
// FileSystemInfo carries the filesystem metadata like Mtime/Atime
type FileSystemInfo struct {
LastModifiedDateTime time.Time `json:"lastModifiedDateTime,omitempty"`
}
func (u *UploadSession) getState() int {
u.Lock()
defer u.Unlock()
return u.state
}
// setState is just a helper method to set the UploadSession state and make error checking
// a little more straightforwards.
func (u *UploadSession) setState(state int, err error) error {
u.Lock()
u.state = state
u.error = err
u.Unlock()
return err
}
// NewUploadSession wraps an upload of a file into an UploadSession struct
// responsible for performing uploads for a file.
func NewUploadSession(inode *Inode, data *[]byte) (*UploadSession, error) {
if data == nil {
return nil, errors.New("data to upload cannot be nil")
}
// create a generic session for all files
inode.RLock()
session := UploadSession{
ID: inode.DriveItem.ID,
OldID: inode.DriveItem.ID,
ParentID: inode.DriveItem.Parent.ID,
NodeID: inode.nodeID,
Name: inode.DriveItem.Name,
Data: *data,
ModTime: *inode.DriveItem.ModTime,
}
inode.RUnlock()
session.Size = uint64(len(*data)) // just in case it somehow differs
session.QuickXORHash = graph.QuickXORHash(data)
return &session, nil
}
// cancel the upload session by deleting the temp file at the endpoint.
func (u *UploadSession) cancel(auth *graph.Auth) {
u.Lock()
// small upload sessions will also have an empty UploadURL in addition to
// uninitialized large file uploads.
nonemptyURL := u.UploadURL != ""
u.Unlock()
if nonemptyURL {
state := u.getState()
if state == uploadStarted || state == uploadErrored {
// dont care about result, this is purely us being polite to the server
go graph.Delete(u.UploadURL, auth)
}
}
}
// Internal method used for uploading individual chunks of a DriveItem. We have
// to make things this way because the internal Put func doesn't work all that
// well when we need to add custom headers. Will return without an error if
// irrespective of HTTP status (errors are reserved for stuff that prevented
// the HTTP request at all).
func (u *UploadSession) uploadChunk(auth *graph.Auth, offset uint64) ([]byte, int, error) {
u.Lock()
url := u.UploadURL
if url == "" {
u.Unlock()
return nil, -1, errors.New("UploadSession UploadURL cannot be empty")
}
u.Unlock()
// how much of the file are we going to upload?
end := offset + uploadChunkSize
var reqChunkSize uint64
if end > u.Size {
end = u.Size
reqChunkSize = end - offset + 1
}
if offset > u.Size {
return nil, -1, errors.New("offset cannot be larger than DriveItem size")
}
auth.Refresh()
client := &http.Client{}
request, _ := http.NewRequest(
"PUT",
url,
bytes.NewReader((u.Data)[offset:end]),
)
// no Authorization header - it will throw a 401 if present
request.Header.Add("Content-Length", strconv.Itoa(int(reqChunkSize)))
frags := fmt.Sprintf("bytes %d-%d/%d", offset, end-1, u.Size)
log.Info().Str("id", u.ID).Msg("Uploading " + frags)
request.Header.Add("Content-Range", frags)
resp, err := client.Do(request)
if err != nil {
// this is a serious error, not simply one with a non-200 return code
return nil, -1, err
}
defer resp.Body.Close()
response, _ := ioutil.ReadAll(resp.Body)
return response, resp.StatusCode, nil
}
// Upload copies the file's contents to the server. Should only be called as a
// goroutine, or it can potentially block for a very long time. The uploadSession.error
// field contains errors to be handled if called as a goroutine.
func (u *UploadSession) Upload(auth *graph.Auth) error {
log.Info().Str("id", u.ID).Str("name", u.Name).Msg("Uploading file.")
u.setState(uploadStarted, nil)
var uploadPath string
var resp []byte
if u.Size < uploadLargeSize {
// Small upload sessions use a simple PUT request, but this does not support
// adding file modification times. We don't really care though, because
// after some experimentation, the Microsoft API doesn't seem to properly
// support these either (this is why we have to use etags).
if isLocalID(u.ID) {
uploadPath = fmt.Sprintf(
"/me/drive/items/%s:/%s:/content",
url.PathEscape(u.ParentID),
url.PathEscape(u.Name),
)
} else {
uploadPath = fmt.Sprintf(
"/me/drive/items/%s/content",
url.PathEscape(u.ID),
)
}
// small files handled in this block
var err error
resp, err = graph.Put(uploadPath, auth, bytes.NewReader(u.Data))
if err != nil && strings.Contains(err.Error(), "resourceModified") {
// retry the request after a second, likely the server is having issues
time.Sleep(time.Second)
resp, err = graph.Put(uploadPath, auth, bytes.NewReader(u.Data))
}
if err != nil {
return u.setState(uploadErrored, fmt.Errorf("small upload failed: %w", err))
}
} else {
if isLocalID(u.ID) {
uploadPath = fmt.Sprintf(
"/me/drive/items/%s:/%s:/createUploadSession",
url.PathEscape(u.ParentID),
url.PathEscape(u.Name),
)
} else {
uploadPath = fmt.Sprintf(
"/me/drive/items/%s/createUploadSession",
url.PathEscape(u.ID),
)
}
sessionPostData, _ := json.Marshal(UploadSessionPost{
ConflictBehavior: "replace",
FileSystemInfo: FileSystemInfo{
LastModifiedDateTime: u.ModTime,
},
})
resp, err := graph.Post(uploadPath, auth, bytes.NewReader(sessionPostData))
if err != nil {
return u.setState(uploadErrored, fmt.Errorf("failed to create upload session: %w", err))
}
// populate UploadURL/expiration - we unmarshal into a fresh session here
// just in case the API does something silly at a later date and overwrites
// a field it shouldn't.
tmp := UploadSession{}
if err = json.Unmarshal(resp, &tmp); err != nil {
return u.setState(uploadErrored,
fmt.Errorf("could not unmarshal upload session post response: %w", err))
}
u.Lock()
u.UploadURL = tmp.UploadURL
u.ExpirationDateTime = tmp.ExpirationDateTime
u.Unlock()
// api upload session created successfully, now do actual content upload
var status int
nchunks := int(math.Ceil(float64(u.Size) / float64(uploadChunkSize)))
for i := 0; i < nchunks; i++ {
resp, status, err = u.uploadChunk(auth, uint64(i)*uploadChunkSize)
if err != nil {
return u.setState(uploadErrored, fmt.Errorf("failed to perform chunk upload: %w", err))
}
// retry server-side failures with an exponential back-off strategy. Will not
// exit this loop unless it receives a non 5xx error or serious failure
for backoff := 1; status >= 500; backoff *= 2 {
log.Error().
Str("id", u.ID).
Str("name", u.Name).
Int("chunk", i).
Int("nchunks", nchunks).
Int("status", status).
Msgf("The OneDrive server is having issues, retrying chunk upload in %ds.", backoff)
time.Sleep(time.Duration(backoff) * time.Second)
resp, status, err = u.uploadChunk(auth, uint64(i)*uploadChunkSize)
if err != nil { // a serious, non 4xx/5xx error
return u.setState(uploadErrored, fmt.Errorf("failed to perform chunk upload: %w", err))
}
}
// handle client-side errors
if status >= 400 {
return u.setState(uploadErrored, fmt.Errorf("error uploading chunk - HTTP %d: %s", status, string(resp)))
}
}
}
// server has indicated that the upload was successful - now we check to verify the
// checksum is what it's supposed to be.
remote := graph.DriveItem{}
if err := json.Unmarshal(resp, &remote); err != nil {
if len(resp) == 0 {
// the API frequently just returns a 0-byte response for completed
// multipart uploads, so we manually fetch the newly updated item
var remotePtr *graph.DriveItem
if isLocalID(u.ID) {
remotePtr, err = graph.GetItemChild(u.ParentID, u.Name, auth)
} else {
remotePtr, err = graph.GetItem(u.ID, auth)
}
if err == nil {
remote = *remotePtr
} else {
return u.setState(uploadErrored,
fmt.Errorf("failed to get item post-upload: %w", err))
}
} else {
return u.setState(uploadErrored,
fmt.Errorf("could not unmarshal response: %w: %s", err, string(resp)),
)
}
}
if remote.File == nil && remote.Size != u.Size {
// if we are absolutely pounding the microsoft API, a remote item may sometimes
// come back without checksums, so we check the size of the uploaded item instead.
return u.setState(uploadErrored, errors.New("size mismatch when remote checksums did not exist"))
} else if !remote.VerifyChecksum(u.QuickXORHash) {
return u.setState(uploadErrored, errors.New("remote checksum did not match"))
}
// update the UploadSession's ID in the event that we exchange a local for a remote ID
u.Lock()
u.ID = remote.ID
u.ETag = remote.ETag
u.Unlock()
return u.setState(uploadComplete, nil)
}