-
Notifications
You must be signed in to change notification settings - Fork 749
/
store.go
94 lines (88 loc) · 2.94 KB
/
store.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
package server
import (
//"context"
"errors"
"fmt"
"strings"
"time"
"github.com/astaxie/beego/httplib"
log "github.com/sjqzhang/seelog"
"github.com/sjqzhang/tusd"
)
type hookDataStore struct {
tusd.DataStore
}
//func (store hookDataStore) NewUpload(ctx context.Context, info handler.FileInfo) (upload handler.Upload, err error) {
// var (
// jsonResult JsonResult
// )
// if Config().AuthUrl != "" {
// if auth_token, ok := info.MetaData["auth_token"]; !ok {
// msg := "token auth fail,auth_token is not in http header Upload-Metadata," +
// "in uppy uppy.setMeta({ auth_token: '9ee60e59-cb0f-4578-aaba-29b9fc2919ca' })"
// log.Error(msg, fmt.Sprintf("current header:%v", info.MetaData))
// return nil, httpError{error: errors.New(msg), statusCode: 401}
// } else {
// req := httplib.Post(Config().AuthUrl)
// req.Param("auth_token", auth_token)
// req.SetTimeout(time.Second*5, time.Second*10)
// content, err := req.String()
// content = strings.TrimSpace(content)
// if strings.HasPrefix(content, "{") && strings.HasSuffix(content, "}") {
// if err = json.Unmarshal([]byte(content), &jsonResult); err != nil {
// log.Error(err)
// return nil, httpError{error: errors.New(err.Error() + content), statusCode: 401}
// }
// if jsonResult.Data != "ok" {
// return nil, httpError{error: errors.New(content), statusCode: 401}
// }
// } else {
// if err != nil {
// log.Error(err)
// return nil, err
// }
// if strings.TrimSpace(content) != "ok" {
// return nil, httpError{error: errors.New(content), statusCode: 401}
// }
// }
// }
// }
// return store.DataStore.NewUpload(ctx, info)
//}
func (store hookDataStore) NewUpload(info tusd.FileInfo) (id string, err error) {
var (
jsonResult JsonResult
)
if Config().AuthUrl != "" {
if auth_token, ok := info.MetaData["auth_token"]; !ok {
msg := "token auth fail,auth_token is not in http header Upload-Metadata," +
"in uppy uppy.setMeta({ auth_token: '9ee60e59-cb0f-4578-aaba-29b9fc2919ca' })"
log.Error(msg, fmt.Sprintf("current header:%v", info.MetaData))
return "", httpError{error: errors.New(msg), statusCode: 401}
} else {
req := httplib.Post(Config().AuthUrl)
req.Param("auth_token", auth_token)
req.SetTimeout(time.Second*5, time.Second*10)
content, err := req.String()
content = strings.TrimSpace(content)
if strings.HasPrefix(content, "{") && strings.HasSuffix(content, "}") {
if err = json.Unmarshal([]byte(content), &jsonResult); err != nil {
log.Error(err)
return "", httpError{error: errors.New(err.Error() + content), statusCode: 401}
}
if jsonResult.Data != "ok" {
return "", httpError{error: errors.New(content), statusCode: 401}
}
} else {
if err != nil {
log.Error(err)
return "", err
}
if strings.TrimSpace(content) != "ok" {
return "", httpError{error: errors.New(content), statusCode: 401}
}
}
}
}
return store.DataStore.NewUpload(info)
}