forked from lxm_tools/flutter-picgo
feature:implement qiniu upload image
This commit is contained in:
@@ -4,10 +4,11 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter_picgo/utils/net.dart';
|
||||
|
||||
class QiniuApi {
|
||||
static const String BASE_URL = 'http://upload.qiniup.com/';
|
||||
|
||||
static Future upload(FormData data) async {
|
||||
Response res = await NetUtils.getInstance().post(BASE_URL, data: data);
|
||||
/// 上传
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -15,15 +16,14 @@ class QiniuApi {
|
||||
/// https://developer.qiniu.com/kodo/manual/1208/upload-token
|
||||
/// https://pub.flutter-io.cn/packages/crypto
|
||||
static String generateUpToken(
|
||||
String accessKey, String secretKey, String putPolicy) {
|
||||
String accessKey, String secretKey, String encodePutPolicy) {
|
||||
//对 JSON 编码的上传策略进行URL 安全的 Base64 编码,得到待签名字符串:
|
||||
var encodedPutPolicy = base64Encode(utf8.encode(putPolicy));
|
||||
// 使用访问密钥(AK/SK)对上一步生成的待签名字符串计算HMAC-SHA1签名:
|
||||
var hmacsha1 = Hmac(sha1, utf8.encode(secretKey));
|
||||
var digest = hmacsha1.convert(utf8.encode(encodedPutPolicy));
|
||||
var digest = hmacsha1.convert(utf8.encode(encodePutPolicy));
|
||||
// 对签名进行URL安全的Base64编码:
|
||||
var encodeSign = base64Encode(digest.bytes);
|
||||
return '$accessKey:$encodeSign:$encodedPutPolicy';
|
||||
var encodeSign = urlSafeBase64Encode(digest.bytes);
|
||||
return '$accessKey:$encodeSign:$encodePutPolicy';
|
||||
}
|
||||
|
||||
/// 生成putPolicy
|
||||
@@ -31,8 +31,34 @@ class QiniuApi {
|
||||
static String generatePutPolicy(String bucket, String key) {
|
||||
Map<String, dynamic> map = {
|
||||
'scope': '$bucket:$key',
|
||||
'deadline': new DateTime.now().millisecondsSinceEpoch + 3600
|
||||
'deadline': 1909497600 // 2030-07-06
|
||||
};
|
||||
return json.encode(map);
|
||||
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 '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user