This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
uploads.go
218 lines (175 loc) · 4.77 KB
/
uploads.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
package strava
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"path/filepath"
)
type UploadDetailed struct {
UploadSummary
}
type UploadSummary struct {
Id int64 `json:"id"`
ExternalId string `json:"external_id"`
Error string `json:"error"`
Status string `json:"status"`
ActivityId int64 `json:"activity_id"`
}
type UploadsService struct {
client *Client
}
type FileDataType string
var FileDataTypes = struct {
FIT FileDataType
FITGZ FileDataType
TCX FileDataType
TCXGZ FileDataType
GPX FileDataType
GPXGZ FileDataType
}{"fit", "fit.gz", "tcx", "tcx.gz", "gpx", "gpx.gz"}
var errorHandler ErrorHandler = func(response *http.Response) error {
if response.StatusCode == 400 {
contents, _ := ioutil.ReadAll(response.Body)
var e UploadSummary
json.Unmarshal(contents, &e)
return Error{e.Error, nil}
} else {
return defaultErrorHandler(response)
}
}
func NewUploadsService(client *Client) *UploadsService {
return &UploadsService{client}
}
/*********************************************************/
type UploadsGetCall struct {
service *UploadsService
id int64
}
func (s *UploadsService) Get(uploadId int64) *UploadsGetCall {
return &UploadsGetCall{
service: s,
id: uploadId,
}
}
func (c *UploadsGetCall) Do() (*UploadDetailed, error) {
data, err := c.service.client.run("GET", fmt.Sprintf("/uploads/%d", c.id), nil)
if err != nil {
return nil, err
}
var upload UploadDetailed
err = json.Unmarshal(data, &upload)
if err != nil {
return nil, err
}
return &upload, nil
}
/*********************************************************/
type UploadsCreateCall struct {
service *UploadsService
ops map[string]interface{}
filename string
fileReader io.Reader
}
// Create defines an upload call containing the contents of the reader.
// will gzip the file, if the the dataType indicates it's not already.
func (s *UploadsService) Create(dataType FileDataType, filename string, reader io.Reader) *UploadsCreateCall {
call := &UploadsCreateCall{
service: s,
ops: make(map[string]interface{}),
filename: filename,
fileReader: reader,
}
if call.filename == "" {
call.filename = fmt.Sprintf("golibraryupload.%v", dataType)
}
call.ops["data_type"] = dataType
return call
}
func (c *UploadsCreateCall) ActivityType(activityType ActivityType) *UploadsCreateCall {
c.ops["activity_type"] = string(activityType)
return c
}
func (c *UploadsCreateCall) Name(name string) *UploadsCreateCall {
c.ops["name"] = name
return c
}
func (c *UploadsCreateCall) Description(description string) *UploadsCreateCall {
c.ops["description"] = description
return c
}
func (c *UploadsCreateCall) Private() *UploadsCreateCall {
c.ops["private"] = 1
return c
}
func (c *UploadsCreateCall) Trainer() *UploadsCreateCall {
c.ops["trainer"] = 1
return c
}
func (c *UploadsCreateCall) ExternalId(id string) *UploadsCreateCall {
c.ops["external_id"] = id
return c
}
func (c *UploadsCreateCall) Do() (*UploadSummary, error) {
var err error
// since we're doing a multipart post, the request is custom built
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", filepath.Base(c.filename))
// gzip the file if it isn't already
if c.ops["data_type"].(FileDataType).isGzipped() {
_, err = io.Copy(part, c.fileReader)
if err != nil {
return nil, err
}
} else {
// gzip here for the user
gzBuffer := &bytes.Buffer{}
gzWriter := gzip.NewWriter(gzBuffer)
_, err = io.Copy(gzWriter, c.fileReader)
gzWriter.Close()
if err != nil {
return nil, err
}
io.Copy(part, gzBuffer)
c.ops["data_type"] = c.ops["data_type"].(FileDataType).toGzippedType()
}
for k, v := range c.ops {
writer.WriteField(k, fmt.Sprintf("%v", v))
}
writer.Close() // so it finishes writing everything to the body buffer
req, err := http.NewRequest("POST", basePath+"/uploads", body)
req.Header.Add("Content-Type", "multipart/form-data; boundary="+writer.Boundary())
data, err := c.service.client.runRequestWithErrorHandler(req, errorHandler)
if err != nil {
return nil, err
}
var upload UploadSummary
err = json.Unmarshal(data, &upload)
if err != nil {
return nil, err
}
return &upload, nil
}
/*********************************************************/
func (f FileDataType) isGzipped() bool {
return f == FileDataTypes.FITGZ || f == FileDataTypes.TCXGZ || f == FileDataTypes.GPXGZ
}
func (f FileDataType) toGzippedType() FileDataType {
if f == FileDataTypes.FITGZ || f == FileDataTypes.TCXGZ || f == FileDataTypes.GPXGZ {
return f
}
switch f {
case FileDataTypes.FIT:
return FileDataTypes.FITGZ
case FileDataTypes.TCX:
return FileDataTypes.TCXGZ
case FileDataTypes.GPX:
return FileDataTypes.GPXGZ
}
return f
}