This repository has been archived by the owner on Aug 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.go
80 lines (71 loc) · 1.78 KB
/
init.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
package box
import (
"errors"
"github.com/Hexilee/gotten"
"github.com/Hexilee/gotten/headers"
"github.com/Hexilee/gotten/unmarshalers"
"net/http"
"strconv"
)
var (
service = new(Service)
mediaChecker gotten.CheckerFunc = func(response *http.Response) bool {
return response.Header.Get(headers.HeaderContentDisposition) != ""
}
textChecker gotten.CheckerFunc = func(response *http.Response) (yes bool) {
contentLength := response.Header.Get(headers.HeaderContentLength)
contentType := response.Header.Get(headers.HeaderContentType)
length, err := strconv.Atoi(contentLength)
if err == nil {
yes = length < 10 && (contentType == headers.MIMETextPlain || contentType == headers.MIMETextHTMLCharsetUTF8)
}
return
}
)
func handlePlainText(data []byte, v interface{}) (err error) {
yes, ok := v.(*bool)
if !ok {
err = errors.New("param kind must be *bool")
}
if err == nil {
switch string(data) {
case "Y":
fallthrough
case "YES":
*yes = true
case "NO ":
fallthrough
case "N":
*yes = false
default:
err = errors.New("data content is unsupported: " + string(data))
}
}
return
}
func init() {
fileCtr, err := unmarshalers.NewFileCtr()
// occur only when you don't have permission at current work directory
//if err != nil {
// panic(err)
//}
creator, err := gotten.NewBuilder().
SetBaseUrl("https://box.zjuqsc.com/item").
AddReaderUnmarshaler(fileCtr, mediaChecker).
AddUnmarshalFunc(handlePlainText, textChecker).
Build()
// err is always nil until you change code above
//if err != nil {
// panic(err)
//}
err = creator.Impl(service)
// err is always nil until you change code above
//if err != nil {
// panic(err)
//}
_ = err
}
// GetService is a getter to get service singleton
func GetService() *Service {
return service
}