forked from qiniu/qshell
/
rs.go
203 lines (178 loc) · 5.24 KB
/
rs.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
package qshell
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"net/url"
"qiniu/api.v6/auth/digest"
"qiniu/api.v6/conf"
"qiniu/api.v6/rs"
"strings"
)
type FetchResult struct {
Key string `json:"key"`
Hash string `json:"hash"`
MimeType string `json:"mimeType"`
Fsize int64 `json:"fsize"`
}
type ChgmEntryPath struct {
Bucket string
Key string
MimeType string
}
type ChtypeEntryPath struct {
Bucket string
Key string
FileType int
}
type DeleteAfterDaysEntryPath struct {
Bucket string
Key string
DeleteAfterDays int
}
type RenameEntryPath struct {
Bucket string
OldKey string
NewKey string
}
type MoveEntryPath struct {
SrcBucket string
DestBucket string
SrcKey string
DestKey string
}
type CopyEntryPath struct {
SrcBucket string
DestBucket string
SrcKey string
DestKey string
}
type BatchItemRet struct {
Code int `json:"code"`
Data BatchItemRetData `json:"data"`
}
type BatchItemRetData struct {
Fsize int `json:"fsize,omitempty"`
Hash string `json:"hash,omitempty"`
MimeType string `json:"mimeType,omitempty"`
PutTime int64 `json:"putTime,omitempty"`
Error string `json:"error,omitempty"`
FileType int `json:"type"`
}
func Fetch(mac *digest.Mac, remoteResUrl, bucket, key string) (fetchResult FetchResult, err error) {
client := rs.NewMac(mac)
entry := bucket
if key != "" {
entry += ":" + key
}
fetchUri := fmt.Sprintf("/fetch/%s/to/%s",
base64.URLEncoding.EncodeToString([]byte(remoteResUrl)),
base64.URLEncoding.EncodeToString([]byte(entry)))
err = client.Conn.Call(nil, &fetchResult, conf.IO_HOST+fetchUri)
return
}
func Prefetch(mac *digest.Mac, bucket, key string) (err error) {
client := rs.NewMac(mac)
prefetchUri := fmt.Sprintf("/prefetch/%s", base64.URLEncoding.EncodeToString([]byte(bucket+":"+key)))
err = client.Conn.Call(nil, nil, conf.IO_HOST+prefetchUri)
return
}
func PrivateUrl(mac *digest.Mac, publicUrl string, deadline int64) (finalUrl string, err error) {
srcUri, pErr := url.Parse(publicUrl)
if pErr != nil {
err = pErr
return
}
h := hmac.New(sha1.New, mac.SecretKey)
urlToSign := srcUri.String()
if strings.Contains(publicUrl, "?") {
urlToSign = fmt.Sprintf("%s&e=%d", urlToSign, deadline)
} else {
urlToSign = fmt.Sprintf("%s?e=%d", urlToSign, deadline)
}
h.Write([]byte(urlToSign))
sign := base64.URLEncoding.EncodeToString(h.Sum(nil))
token := mac.AccessKey + ":" + sign
finalUrl = fmt.Sprintf("%s&token=%s", urlToSign, token)
return
}
func Saveas(mac *digest.Mac, publicUrl string, saveBucket string, saveKey string) (string, error) {
uri, parseErr := url.Parse(publicUrl)
if parseErr != nil {
return "", parseErr
}
baseUrl := uri.Host + uri.RequestURI()
saveEntry := saveBucket + ":" + saveKey
encodedSaveEntry := base64.URLEncoding.EncodeToString([]byte(saveEntry))
baseUrl += "|saveas/" + encodedSaveEntry
h := hmac.New(sha1.New, mac.SecretKey)
h.Write([]byte(baseUrl))
sign := h.Sum(nil)
encodedSign := base64.URLEncoding.EncodeToString(sign)
return publicUrl + "|saveas/" + encodedSaveEntry + "/sign/" + mac.AccessKey + ":" + encodedSign, nil
}
func BatchStat(client rs.Client, entries []rs.EntryPath) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIStat(e.Bucket, e.Key)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchChgm(client rs.Client, entries []ChgmEntryPath) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIChangeMime(e.Bucket, e.Key, e.MimeType)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchChtype(client rs.Client, entries []ChtypeEntryPath) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIChangeType(e.Bucket, e.Key, e.FileType)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchDeleteAfterDays(client rs.Client, entries []DeleteAfterDaysEntryPath) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIDeleteAfterDays(e.Bucket, e.Key, e.DeleteAfterDays)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchDelete(client rs.Client, entries []rs.EntryPath) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIDelete(e.Bucket, e.Key)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchRename(client rs.Client, entries []RenameEntryPath, force bool) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIMove(e.Bucket, e.OldKey, e.Bucket, e.NewKey, force)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchMove(client rs.Client, entries []MoveEntryPath, force bool) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URIMove(e.SrcBucket, e.SrcKey, e.DestBucket, e.DestKey, force)
}
err = client.Batch(nil, &ret, b)
return
}
func BatchCopy(client rs.Client, entries []CopyEntryPath, force bool) (ret []BatchItemRet, err error) {
b := make([]string, len(entries))
for i, e := range entries {
b[i] = rs.URICopy(e.SrcBucket, e.SrcKey, e.DestBucket, e.DestKey, force)
}
err = client.Batch(nil, &ret, b)
return
}