-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathqiniu_api.dart
179 lines (167 loc) · 6.02 KB
/
qiniu_api.dart
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
import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:dio/dio.dart';
import 'package:flutter_picgo/utils/net.dart';
import 'package:flutter_picgo/utils/strings.dart';
class QiniuApi {
/// 管理Base_url,accesstoken和uptoken不共用baseurl
static const String BASE_URL = "https://rs.qbox.me";
static const String BASE_URL2 = "https://rsf.qbox.me";
static const String accessKey = 'accessKey';
static const String secretKey = 'secretKey';
/// 上传
static Future upload(
String area, FormData data, Map<String, dynamic> headers) async {
Response res = await NetUtils.getInstance()
.post(getHost(area), data: data, options: Options(headers: headers));
return res.data;
}
/// 删除
static Future delete(String url, String ak, String sk) async {
Response res = await NetUtils.getInstance().post(url,
options: Options(
extra: {
QiniuApi.accessKey: ak,
QiniuApi.secretKey: sk,
},
contentType: 'application/x-www-form-urlencoded',
));
return res.data;
}
// 资源列举
static Future list(Map<String, dynamic> query, String ak, String sk) async {
Response res =
await NetUtils.getInstance().get('${QiniuApi.BASE_URL2}/list',
queryParameters: query,
options: Options(
extra: {
QiniuApi.accessKey: ak,
QiniuApi.secretKey: sk,
},
contentType: 'application/x-www-form-urlencoded',
));
return res.data;
}
/// 上传凭证算法
/// https://developer.qiniu.com/kodo/manual/1208/upload-token
/// https://pub.flutter-io.cn/packages/crypto
static String generateUpToken(
String accessKey, String secretKey, String encodePutPolicy) {
//对 JSON 编码的上传策略进行URL 安全的 Base64 编码,得到待签名字符串:
// 使用访问密钥(AK/SK)对上一步生成的待签名字符串计算HMAC-SHA1签名:
var hmacsha1 = Hmac(sha1, utf8.encode(secretKey));
var digest = hmacsha1.convert(utf8.encode(encodePutPolicy));
// 对签名进行URL安全的Base64编码:
var encodeSign = urlSafeBase64Encode(digest.bytes);
return '$accessKey:$encodeSign:$encodePutPolicy';
}
/// 生成管理凭证
/// https://developer.qiniu.com/kodo/manual/1201/access-token
static String generateAuthToken(
String method,
String path,
String query,
String host,
String contentType,
String body,
String accessKey,
String secretKey) {
var signStr = '${method.toUpperCase()} $path';
if (!isBlank(query)) {
signStr += '?$query';
}
signStr += '\nHost: $host';
if (!isBlank(contentType)) {
signStr += '\nContent-Type: $contentType';
}
signStr += '\n\n';
if (contentType != 'application/octet-stream' && body != null) {
signStr += body;
}
// 使用SecertKey对上一步生成的原始字符串计算HMAC-SHA1签名:
var hmacsha1 = Hmac(sha1, utf8.encode(secretKey));
var sign = hmacsha1.convert(utf8.encode(signStr));
var encodedSign = urlSafeBase64Encode(sign.bytes);
return '$accessKey:$encodedSign';
}
/// 管理凭证历史文档
/// https://developer.qiniu.com/kodo/manual/6671/historical-document-management-certificate
static String generateAuthTokenByQBox(String path, String query, String body,
String accessKey, String secretKey) {
String signStr = '$path?$query\n${body ?? ''}';
// 使用SecertKey对上一步生成的原始字符串计算HMAC-SHA1签名:
var hmacsha1 = Hmac(sha1, utf8.encode(secretKey));
var sign = hmacsha1.convert(utf8.encode(signStr));
var encodedSign = urlSafeBase64Encode(sign.bytes);
return '$accessKey:$encodedSign';
}
/// 生成putPolicy
/// https://developer.qiniu.com/kodo/manual/1206/put-policy
static String generatePutPolicy(String bucket, String key) {
Map<String, dynamic> map = {
'scope': '$bucket:$key',
'deadline': 1909497600 // 2030-07-06
};
return urlSafeBase64Encode(utf8.encode(json.encode(map)));
}
/// URL安全的Base64编码
/// https://developer.qiniu.com/kodo/manual/1231/appendix#urlsafe-base64
static String urlSafeBase64Encode(List<int> bytes) {
String str = base64Encode(bytes);
return str.replaceAll('+', '-').replaceAll('/', '_');
}
/// Access Area get Host
/// https://developer.qiniu.com/kodo/manual/1671/region-endpoint
static String getHost(String area) {
switch (area) {
case 'z0':
return 'https://upload.qiniup.com';
case 'z1':
return 'https://upload-z1.qiniup.com';
case 'z2':
return 'https://upload-z2.qiniup.com';
case 'na0':
return 'https://upload-na0.qiniup.com';
case 'as0':
return 'https://upload-as0.qiniup.com';
default:
return '';
}
}
}
/// 七牛管理验签拦截器
class QiniuInterceptor extends InterceptorsWrapper {
@override
Future onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
if (options.path.contains(QiniuApi.BASE_URL)) {
String ak = '${options.extra[QiniuApi.accessKey]}';
String sk = '${options.extra[QiniuApi.secretKey]}';
options.data = options.data ?? '';
var accessToken = QiniuApi.generateAuthToken(
options.method,
options.uri.path,
options.uri.query,
options.uri.host,
options.contentType,
options.data,
ak,
sk);
options.headers.addAll({
'Authorization': 'Qiniu $accessToken',
});
} else if (options.path.contains(QiniuApi.BASE_URL2)) {
String ak = '${options.extra[QiniuApi.accessKey]}';
String sk = '${options.extra[QiniuApi.secretKey]}';
options.data = options.data ?? '';
var accessToken = QiniuApi.generateAuthTokenByQBox(
options.uri.path, options.uri.query, options.data, ak, sk);
options.headers.addAll({
'Authorization': 'QBox $accessToken',
});
}
return handler.next(options);
}
}