forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileupload.go
121 lines (102 loc) · 3.3 KB
/
fileupload.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
package stripe
import (
"encoding/json"
"io"
"mime/multipart"
"os"
"path/filepath"
)
// FileUploadParams is the set of parameters that can be used when creating a
// file upload.
// For more details see https://stripe.com/docs/api#create_file_upload.
type FileUploadParams struct {
Params `form:"*"`
// File is a deprecated form of FileReader and Filename that will do the same thing, but
// allows referencing a file directly. Please prefer the use of FileReader and Filename instead.
File *os.File
// FileReader is a reader with the contents of the file that should be uploaded.
FileReader io.Reader
// Filename is just the name of the file without path information.
Filename string
Purpose FileUploadPurpose
}
// FileUploadListParams is the set of parameters that can be used when listing
// file uploads. For more details see https://stripe.com/docs/api#list_file_uploads.
type FileUploadListParams struct {
ListParams `form:"*"`
Created int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
Purpose FileUploadPurpose `form:"purpose"`
}
// FileUploadPurpose is the purpose of a particular file upload. Allowed values
// are "dispute_evidence" and "identity_document".
type FileUploadPurpose string
// FileUpload is the resource representing a Stripe file upload.
// For more details see https://stripe.com/docs/api#file_uploads.
type FileUpload struct {
Created int64 `json:"created"`
ID string `json:"id"`
Purpose FileUploadPurpose `json:"purpose"`
Size int64 `json:"size"`
Type string `json:"type"`
URL string `json:"url"`
}
// FileUploadList is a list of file uploads as retrieved from a list endpoint.
type FileUploadList struct {
ListMeta
Values []*FileUpload `json:"data"`
}
// AppendDetails adds the file upload details to an io.ReadWriter. It returns
// the boundary string for a multipart/form-data request and an error (if one
// exists).
func (f *FileUploadParams) AppendDetails(body io.ReadWriter) (string, error) {
writer := multipart.NewWriter(body)
var err error
if len(f.Purpose) > 0 {
err = writer.WriteField("purpose", string(f.Purpose))
if err != nil {
return "", err
}
}
// Support both FileReader/Filename and File with
// the former being the newer preferred version
if f.FileReader != nil && f.Filename != "" {
part, err := writer.CreateFormFile("file", filepath.Base(f.Filename))
if err != nil {
return "", err
}
_, err = io.Copy(part, f.FileReader)
if err != nil {
return "", err
}
} else if f.File != nil {
part, err := writer.CreateFormFile("file", filepath.Base(f.File.Name()))
if err != nil {
return "", err
}
_, err = io.Copy(part, f.File)
if err != nil {
return "", err
}
}
err = writer.Close()
if err != nil {
return "", err
}
return writer.Boundary(), nil
}
// UnmarshalJSON handles deserialization of a FileUpload.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (f *FileUpload) UnmarshalJSON(data []byte) error {
type file FileUpload
var ff file
err := json.Unmarshal(data, &ff)
if err == nil {
*f = FileUpload(ff)
} else {
// the id is surrounded by "\" characters, so strip them
f.ID = string(data[1 : len(data)-1])
}
return nil
}