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 '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,117 @@
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:flutter_picgo/api/qiniu_api.dart';
|
||||
import 'package:flutter_picgo/model/qiniu_config.dart';
|
||||
import 'package:flutter_picgo/model/uploaded.dart';
|
||||
import 'package:flutter_picgo/resources/pb_type_keys.dart';
|
||||
import 'package:flutter_picgo/utils/image_upload.dart';
|
||||
import 'dart:io';
|
||||
import 'package:flutter_picgo/utils/strategy/image_upload_strategy.dart';
|
||||
import 'package:flutter_picgo/utils/strings.dart';
|
||||
|
||||
class QiniuImageUpload extends ImageUploadStrategy {
|
||||
@override
|
||||
Future<Uploaded> delete(Uploaded uploaded) {
|
||||
return null;
|
||||
Future<Uploaded> delete(Uploaded uploaded) async {
|
||||
String infoStr = await ImageUploadUtils.getUploadedItemInfo(uploaded.id);
|
||||
QiniuuploadedInfo info;
|
||||
try {
|
||||
info = QiniuuploadedInfo.fromJson(json.decode(infoStr));
|
||||
} catch (e) {}
|
||||
if (info != null) {}
|
||||
return uploaded;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uploaded> upload(File file, String renameImage) {
|
||||
return null;
|
||||
Future<Uploaded> upload(File file, String renameImage) async {
|
||||
try {
|
||||
String configStr = await ImageUploadUtils.getPBConfig(PBTypeKeys.qiniu);
|
||||
if (isBlank(configStr)) {
|
||||
throw QiniuError(error: '读取配置文件错误!请重试');
|
||||
}
|
||||
QiniuConfig config = QiniuConfig.fromJson(json.decode(configStr));
|
||||
// putPolicy
|
||||
String resourceKey = path.joinAll([config.path ?? '', renameImage]);
|
||||
String putPolicy = QiniuApi.generatePutPolicy(config.bucket, resourceKey);
|
||||
String upToken = QiniuApi.generateUpToken(
|
||||
config.accessKey, config.secretKey, putPolicy);
|
||||
var result = await QiniuApi.upload(
|
||||
config.area,
|
||||
FormData.fromMap({
|
||||
"key": resourceKey,
|
||||
"token": upToken,
|
||||
"fileName": renameImage,
|
||||
"file":
|
||||
await MultipartFile.fromFile(file.path, filename: renameImage),
|
||||
}),
|
||||
{
|
||||
"Authorization": 'UpToken $upToken',
|
||||
});
|
||||
var uploadedItem = Uploaded(
|
||||
-1,
|
||||
'${path.join(config.url, result["key"])}${config.options}',
|
||||
PBTypeKeys.qiniu,
|
||||
info: json.encode(
|
||||
QiniuuploadedInfo(hash: result["hash"], key: result["key"])));
|
||||
await ImageUploadUtils.saveUploadedItem(uploadedItem);
|
||||
return uploadedItem;
|
||||
} on DioError catch (e) {
|
||||
debugPrint(e.response.data);
|
||||
if (e.type == DioErrorType.RESPONSE &&
|
||||
e.error.toString().indexOf('400') > 0) {
|
||||
throw QiniuError(error: '400 请求报文格式错误');
|
||||
} else if (e.type == DioErrorType.RESPONSE &&
|
||||
e.error.toString().indexOf('401') > 0) {
|
||||
throw QiniuError(error: '401 管理凭证无效');
|
||||
} else if (e.type == DioErrorType.RESPONSE &&
|
||||
e.error.toString().indexOf('599') > 0) {
|
||||
throw QiniuError(error: '599 服务端操作失败');
|
||||
} else if (e.type == DioErrorType.RESPONSE &&
|
||||
e.error.toString().indexOf('612') > 0) {
|
||||
throw QiniuError(error: '612 待删除资源不存在');
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// SMMSError describes the error info when request failed.
|
||||
class QiniuError implements Exception {
|
||||
QiniuError({
|
||||
this.error,
|
||||
});
|
||||
|
||||
dynamic error;
|
||||
|
||||
String get message => (error?.toString() ?? '');
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
var msg = 'QiniuError $message';
|
||||
if (error is Error) {
|
||||
msg += '\n${error.stackTrace}';
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
class QiniuuploadedInfo {
|
||||
String hash;
|
||||
String key;
|
||||
|
||||
QiniuuploadedInfo({this.hash, this.key});
|
||||
|
||||
QiniuuploadedInfo.fromJson(Map<String, dynamic> json) {
|
||||
hash = json['hash'];
|
||||
key = json['key'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['hash'] = this.hash;
|
||||
data['key'] = this.key;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user