Debian Codegraph @

upload.go

raw
1 package b2
2
3 import (
4 "bytes"
5 "crypto/sha1"
6 "encoding/hex"
7 "encoding/json"
8 "io"
9 "io/ioutil"
10 "net/http"
11 "net/url"
12 )
13
14 // Upload uploads a file to a B2 bucket. If mimeType is "", "b2/x-auto" will be used.
15 //
16 // Concurrent calls to Upload will use separate upload URLs, but consequent ones
17 // will attempt to reuse previously obtained ones to save b2_get_upload_url calls.
18 // Upload URL failures are handled transparently.
19 //
20 // Since the B2 API requires a SHA1 header, normally the file will first be read
21 // entirely into a memory buffer. Two cases avoid the memory copy: if r is a
22 // bytes.Buffer, the SHA1 will be computed in place; otherwise, if r implements io.Seeker
23 // (like *os.File and *bytes.Reader), the file will be read twice, once to compute
24 // the SHA1 and once to upload.
25 //
26 // If a file by this name already exist, a new version will be created.
27 func (b *Bucket) Upload(r io.Reader, name, mimeType string) (*FileInfo, error) {
28 var body io.ReadSeeker
29 switch r := r.(type) {
30 case *bytes.Buffer:
31 defer r.Reset() // we are expected to consume it
32 body = bytes.NewReader(r.Bytes())
33 case io.ReadSeeker:
34 body = r
35 default:
36 debugf("upload %s: buffering", name)
37 b, err := ioutil.ReadAll(r)
38 if err != nil {
39 return nil, err
40 }
41 body = bytes.NewReader(b)
42 }
43
44 h := sha1.New()
45 length, err := io.Copy(h, body)
46 if err != nil {
47 return nil, err
48 }
49 sha1Sum := hex.EncodeToString(h.Sum(nil))
50
51 var fi *FileInfo
52 for i := 0; i < 5; i++ {
53 if _, err = body.Seek(0, io.SeekStart); err != nil {
54 return nil, err
55 }
56
57 fi, err = b.UploadWithSHA1(body, name, mimeType, sha1Sum, length)
58 if err == nil {
59 break
60 }
61 if err, ok := UnwrapError(err); ok && err.Status == http.StatusUnauthorized {
62 // We are forced to pass nil to login, risking a double login (which is
63 // wasteful, but not harmful) because the API does not give us access to
64 // the failed response (without hacks).
65 if err := b.c.login(nil); err != nil {
66 return nil, err
67 }
68 i--
69 }
70 }
71 return fi, err
72 }
73
74 type uploadURL struct {
75 UploadURL, AuthorizationToken string
76 }
77
78 func (b *Bucket) getUploadURL() (u *uploadURL, err error) {
79 b.uploadURLsMu.Lock()
80 if len(b.uploadURLs) > 0 {
81 u = b.uploadURLs[len(b.uploadURLs)-1]
82 b.uploadURLs = b.uploadURLs[:len(b.uploadURLs)-1]
83 }
84 b.uploadURLsMu.Unlock()
85 if u != nil {
86 return
87 }
88
89 res, err := b.c.doRequest("b2_get_upload_url", map[string]interface{}{
90 "bucketId": b.ID,
91 })
92 if err != nil {
93 return
94 }
95 defer drainAndClose(res.Body)
96 err = json.NewDecoder(res.Body).Decode(&u)
97 return
98 }
99
100 func (b *Bucket) putUploadURL(u *uploadURL) {
101 b.uploadURLsMu.Lock()
102 defer b.uploadURLsMu.Unlock()
103 b.uploadURLs = append(b.uploadURLs, u)
104 }
105
106 // UploadWithSHA1 is like Upload, but allows the caller to specify previously
107 // known SHA1 and length of the file. It never does any buffering, nor does it
108 // retry on failure.
109 //
110 // Note that retrying on most upload failures, not just error handling, is
111 // mandatory by the B2 API documentation. If the error Status is Unauthorized,
112 // a call to (*Client).LoginInfo(true) should be performed first.
113 //
114 // sha1Sum should be the hex encoding of the SHA1 sum of what will be read from r.
115 //
116 // This is an advanced interface, most clients should use Upload, and consider
117 // passing it a bytes.Buffer or io.ReadSeeker to avoid buffering.
118 func (b *Bucket) UploadWithSHA1(r io.Reader, name, mimeType, sha1Sum string, length int64) (*FileInfo, error) {
119 uurl, err := b.getUploadURL()
120 if err != nil {
121 return nil, err
122 }
123
124 req, err := http.NewRequest("POST", uurl.UploadURL, ioutil.NopCloser(r))
125 if err != nil {
126 return nil, err
127 }
128 req.ContentLength = length
129 req.Header.Set("Authorization", uurl.AuthorizationToken)
130 req.Header.Set("X-Bz-File-Name", url.QueryEscape(name))
131 req.Header.Set("Content-Type", mimeType)
132 req.Header.Set("X-Bz-Content-Sha1", sha1Sum)
133
134 res, err := b.c.hc.Do(req)
135 if err != nil {
136 debugf("upload %s: %s", name, err)
137 return nil, err
138 }
139 debugf("upload %s (%d %s)", name, length, sha1Sum)
140 defer drainAndClose(res.Body)
141
142 fi := fileInfoObj{}
143 if err = json.NewDecoder(res.Body).Decode(&fi); err != nil {
144 return nil, err
145 }
146 b.putUploadURL(uurl)
147 return fi.makeFileInfo(), nil
148 }