This repository was archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathpfop.go
212 lines (188 loc) · 5.13 KB
/
pfop.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
package storage
import (
"context"
"fmt"
"github.com/qiniu/api.v7/v7/auth"
"github.com/qiniu/api.v7/v7/client"
"github.com/qiniu/api.v7/v7/conf"
"net/http"
)
// OperationManager 提供了数据处理相关的方法
type OperationManager struct {
Client *client.Client
Mac *auth.Credentials
Cfg *Config
}
// NewOperationManager 用来构建一个新的数据处理对象
func NewOperationManager(mac *auth.Credentials, cfg *Config) *OperationManager {
if cfg == nil {
cfg = &Config{}
}
return &OperationManager{
Client: &client.DefaultClient,
Mac: mac,
Cfg: cfg,
}
}
// NewOperationManager 用来构建一个新的数据处理对象
func NewOperationManagerEx(mac *auth.Credentials, cfg *Config, clt *client.Client) *OperationManager {
if cfg == nil {
cfg = &Config{}
}
if clt == nil {
clt = &client.DefaultClient
}
return &OperationManager{
Client: clt,
Mac: mac,
Cfg: cfg,
}
}
// PfopRet 为数据处理请求的回复内容
type PfopRet struct {
PersistentID string `json:"persistentId,omitempty"`
}
// PrefopRet 为数据处理请求的状态查询回复内容
type PrefopRet struct {
ID string `json:"id"`
Code int `json:"code"`
Desc string `json:"desc"`
InputBucket string `json:"inputBucket,omitempty"`
InputKey string `json:"inputKey,omitempty"`
Pipeline string `json:"pipeline,omitempty"`
Reqid string `json:"reqid,omitempty"`
Items []FopResult
}
func (r *PrefopRet) String() string {
strData := fmt.Sprintf("Id: %s\r\nCode: %d\r\nDesc: %s\r\n", r.ID, r.Code, r.Desc)
if r.InputBucket != "" {
strData += fmt.Sprintln(fmt.Sprintf("InputBucket: %s", r.InputBucket))
}
if r.InputKey != "" {
strData += fmt.Sprintln(fmt.Sprintf("InputKey: %s", r.InputKey))
}
if r.Pipeline != "" {
strData += fmt.Sprintln(fmt.Sprintf("Pipeline: %s", r.Pipeline))
}
if r.Reqid != "" {
strData += fmt.Sprintln(fmt.Sprintf("Reqid: %s", r.Reqid))
}
strData = fmt.Sprintln(strData)
for _, item := range r.Items {
strData += fmt.Sprintf("\tCmd:\t%s\r\n\tCode:\t%d\r\n\tDesc:\t%s\r\n", item.Cmd, item.Code, item.Desc)
if item.Error != "" {
strData += fmt.Sprintf("\tError:\t%s\r\n", item.Error)
} else {
if item.Hash != "" {
strData += fmt.Sprintf("\tHash:\t%s\r\n", item.Hash)
}
if item.Key != "" {
strData += fmt.Sprintf("\tKey:\t%s\r\n", item.Key)
}
if item.Keys != nil {
if len(item.Keys) > 0 {
strData += "\tKeys: {\r\n"
for _, key := range item.Keys {
strData += fmt.Sprintf("\t\t%s\r\n", key)
}
strData += "\t}\r\n"
}
}
}
strData += "\r\n"
}
return strData
}
// FopResult 云处理操作列表,包含每个云处理操作的状态信息
type FopResult struct {
Cmd string `json:"cmd"`
Code int `json:"code"`
Desc string `json:"desc"`
Error string `json:"error,omitempty"`
Hash string `json:"hash,omitempty"`
Key string `json:"key,omitempty"`
Keys []string `json:"keys,omitempty"`
}
// Pfop 持久化数据处理
//
// bucket 资源空间
// key 源资源名
// fops 云处理操作列表,
// notifyURL 处理结果通知接收URL
// pipeline 多媒体处理队列名称
// force 强制执行数据处理
//
func (m *OperationManager) Pfop(bucket, key, fops, pipeline, notifyURL string,
force bool) (persistentID string, err error) {
pfopParams := map[string][]string{
"bucket": []string{bucket},
"key": []string{key},
"fops": []string{fops},
}
if pipeline != "" {
pfopParams["pipeline"] = []string{pipeline}
}
if notifyURL != "" {
pfopParams["notifyURL"] = []string{notifyURL}
}
if force {
pfopParams["force"] = []string{"1"}
}
var ret PfopRet
ctx := auth.WithCredentials(context.TODO(), m.Mac)
reqHost, reqErr := m.ApiHost(bucket)
if reqErr != nil {
err = reqErr
return
}
reqURL := fmt.Sprintf("%s/pfop/", reqHost)
headers := http.Header{}
headers.Add("Content-Type", conf.CONTENT_TYPE_FORM)
err = m.Client.CallWithForm(ctx, &ret, "POST", reqURL, headers, pfopParams)
if err != nil {
return
}
persistentID = ret.PersistentID
return
}
// Prefop 持久化处理状态查询
func (m *OperationManager) Prefop(persistentID string) (ret PrefopRet, err error) {
ctx := context.TODO()
reqHost := m.PrefopApiHost(persistentID)
reqURL := fmt.Sprintf("%s/status/get/prefop?id=%s", reqHost, persistentID)
headers := http.Header{}
headers.Add("Content-Type", conf.CONTENT_TYPE_FORM)
err = m.Client.Call(ctx, &ret, "GET", reqURL, headers)
return
}
func (m *OperationManager) ApiHost(bucket string) (apiHost string, err error) {
var zone *Zone
if m.Cfg.Zone != nil {
zone = m.Cfg.Zone
} else {
if v, zoneErr := GetZone(m.Mac.AccessKey, bucket); zoneErr != nil {
err = zoneErr
return
} else {
zone = v
}
}
scheme := "http://"
if m.Cfg.UseHTTPS {
scheme = "https://"
}
apiHost = fmt.Sprintf("%s%s", scheme, zone.ApiHost)
return
}
func (m *OperationManager) PrefopApiHost(persistentID string) (apiHost string) {
apiHost = "api.qiniu.com"
if m.Cfg.Zone != nil {
apiHost = m.Cfg.Zone.ApiHost
}
if m.Cfg.UseHTTPS {
apiHost = fmt.Sprintf("https://%s", apiHost)
} else {
apiHost = fmt.Sprintf("http://%s", apiHost)
}
return
}