-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_upload.go
139 lines (119 loc) · 3.06 KB
/
file_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
package telegram
import (
"encoding/json"
"io"
"reflect"
"strings"
)
type Fileable interface{}
type FileUploader interface {
Name() string
Reader() (io.Reader, error)
Size() int64
}
var fileUploaderInterface = reflect.TypeOf((*FileUploader)(nil)).Elem()
type fileUpload struct {
params map[string]string
fieldname string
file FileUploader
err error
}
func isFileUpload(req interface{}) (upload fileUpload, isUpload bool) {
val := reflect.ValueOf(req)
if val.Kind() != reflect.Ptr {
return fileUpload{}, false
}
val = val.Elem()
if val.Kind() != reflect.Struct {
return fileUpload{}, false
}
var uploader reflect.Value
var uploaderField reflect.StructField
for i := 0; i < val.NumField(); i++ {
f := val.Field(i)
if f.Kind() == reflect.Interface && !f.IsZero() && f.Elem().Type().Implements(fileUploaderInterface) {
uploader = f
uploaderField = val.Type().Field(i)
isUpload = true
break
}
}
if !isUpload {
return fileUpload{}, false
}
upload.params = make(map[string]string)
upload.fieldname, _ = parseTag(uploaderField.Tag.Get("json"))
upload.file = uploader.Interface().(FileUploader)
for i := 0; i < val.NumField(); i++ {
f := val.Field(i)
if f == uploader {
continue
}
fieldType := val.Type().Field(i)
fieldName, tagOpts := parseTag(fieldType.Tag.Get("json"))
if tagOpts.Contains("omitempty") && isEmptyValue(f) {
continue
}
kind := f.Kind()
if kind == reflect.String {
upload.params[fieldName] = f.String()
} else {
data, err := json.Marshal(f.Interface())
if err != nil {
upload.err = err
return
}
upload.params[fieldName] = string(data)
}
}
return upload, true
}
// tagOptions is the string following a comma in a struct field's "json"
// tag, or the empty string. It does not include the leading comma.
type tagOptions string
// parseTag splits a struct field's json tag into its name and
// comma-separated options.
func parseTag(tag string) (string, tagOptions) {
if idx := strings.Index(tag, ","); idx != -1 {
return tag[:idx], tagOptions(tag[idx+1:])
}
return tag, tagOptions("")
}
// Contains reports whether a comma-separated list of options
// contains a particular substr flag. substr must be surrounded by a
// string boundary or commas.
func (o tagOptions) Contains(optionName string) bool {
if len(o) == 0 {
return false
}
s := string(o)
for s != "" {
var next string
i := strings.Index(s, ",")
if i >= 0 {
s, next = s[:i], s[i+1:]
}
if s == optionName {
return true
}
s = next
}
return false
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}