|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:dio/dio.dart'; |
| 4 | +import 'package:flutter_picgo/api/tcyun_api.dart'; |
| 5 | +import 'package:flutter_picgo/model/tcyun_config.dart'; |
1 | 6 | import 'package:flutter_picgo/model/uploaded.dart'; |
| 7 | +import 'package:flutter_picgo/resources/pb_type_keys.dart'; |
| 8 | +import 'package:flutter_picgo/utils/image_upload.dart'; |
2 | 9 | import 'dart:io'; |
3 | 10 |
|
| 11 | +import 'package:path/path.dart' as path; |
4 | 12 | import 'package:flutter_picgo/utils/strategy/image_upload_strategy.dart'; |
| 13 | +import 'package:flutter_picgo/utils/strings.dart'; |
5 | 14 |
|
6 | 15 | class TcyunImageUpload implements ImageUploadStrategy { |
7 | 16 | @override |
8 | | - Future<Uploaded> delete(Uploaded uploaded) { |
9 | | - throw UnimplementedError(); |
| 17 | + Future<Uploaded> delete(Uploaded uploaded) async { |
| 18 | + TcyunUploaddedInfo info; |
| 19 | + try { |
| 20 | + info = TcyunUploaddedInfo.fromJson(json.decode(uploaded.info)); |
| 21 | + } catch (e) {} |
| 22 | + if (info != null) { |
| 23 | + await TcyunApi.deleteobject( |
| 24 | + info.secretId, info.secretKey, info.bucket, info.area, info.key); |
| 25 | + } |
| 26 | + return uploaded; |
| 27 | + } |
| 28 | + |
| 29 | + @override |
| 30 | + Future<Uploaded> upload(File file, String renameImage) async { |
| 31 | + String configStr = await ImageUploadUtils.getPBConfig(PBTypeKeys.tcyun); |
| 32 | + if (isBlank(configStr)) { |
| 33 | + throw TcyunError(error: '读取配置文件错误!请重试'); |
| 34 | + } |
| 35 | + TcyunConfig config = TcyunConfig.fromJson(json.decode(configStr)); |
| 36 | + String objectPath = isBlank(config.path) |
| 37 | + ? renameImage |
| 38 | + : path.joinAll([config.path, renameImage]); |
| 39 | + |
| 40 | + /// post |
| 41 | + String keyTime = TcyunApi.buildKeyTime(); |
| 42 | + String policy = TcyunApi.buildPolicy( |
| 43 | + config.bucket, objectPath, config.secretId, keyTime); |
| 44 | + FormData formData = FormData.fromMap({ |
| 45 | + "key": objectPath, |
| 46 | + "file": await MultipartFile.fromFile(file.path, filename: renameImage), |
| 47 | + "policy": base64.encode(utf8.encode(policy)), |
| 48 | + "q-sign-algorithm": "sha1", |
| 49 | + "q-ak": config.secretId, |
| 50 | + "q-key-time": keyTime, |
| 51 | + "q-sign-time": keyTime, |
| 52 | + "q-signature": TcyunApi.buildSignature(config.secretKey, keyTime, policy) |
| 53 | + }); |
| 54 | + print(TcyunApi.buildSignature(config.secretKey, keyTime, policy)); |
| 55 | + await TcyunApi.postObject( |
| 56 | + config.secretId, |
| 57 | + config.secretKey, |
| 58 | + config.bucket, |
| 59 | + config.area, |
| 60 | + path.extension(renameImage).replaceFirst('.', ''), |
| 61 | + formData); |
| 62 | + String imgPath = path.joinAll([ |
| 63 | + isBlank(config.customUrl) |
| 64 | + ? 'https://${config.bucket}.cos.${config.area}.myqcloud.com' |
| 65 | + : config.customUrl, |
| 66 | + objectPath |
| 67 | + ]); |
| 68 | + var uploadedItem = Uploaded(-1, '$imgPath', PBTypeKeys.tcyun, |
| 69 | + info: json.encode(TcyunUploaddedInfo( |
| 70 | + secretId: config.secretId, |
| 71 | + secretKey: config.secretKey, |
| 72 | + area: config.area, |
| 73 | + key: objectPath, |
| 74 | + bucket: config.bucket))); |
| 75 | + await ImageUploadUtils.saveUploadedItem(uploadedItem); |
| 76 | + return uploadedItem; |
10 | 77 | } |
| 78 | +} |
| 79 | + |
| 80 | +class TcyunError implements Exception { |
| 81 | + TcyunError({ |
| 82 | + this.error, |
| 83 | + }); |
| 84 | + |
| 85 | + dynamic error; |
| 86 | + |
| 87 | + String get message => (error?.toString() ?? ''); |
11 | 88 |
|
12 | 89 | @override |
13 | | - Future<Uploaded> upload(File file, String renameImage) { |
14 | | - throw UnimplementedError(); |
| 90 | + String toString() { |
| 91 | + var msg = 'TcyunError $message'; |
| 92 | + if (error is Error) { |
| 93 | + msg += '\n${error.stackTrace}'; |
| 94 | + } |
| 95 | + return msg; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class TcyunUploaddedInfo { |
| 100 | + String area; |
| 101 | + String bucket; |
| 102 | + String secretId; |
| 103 | + String secretKey; |
| 104 | + String key; |
| 105 | + |
| 106 | + TcyunUploaddedInfo( |
| 107 | + {this.area, this.bucket, this.secretId, this.secretKey, this.key}); |
| 108 | + |
| 109 | + TcyunUploaddedInfo.fromJson(Map<String, dynamic> json) { |
| 110 | + area = json['area']; |
| 111 | + bucket = json['bucket']; |
| 112 | + secretId = json['secretId']; |
| 113 | + secretKey = json['secretKey']; |
| 114 | + key = json['key']; |
| 115 | + } |
| 116 | + |
| 117 | + Map<String, dynamic> toJson() { |
| 118 | + final Map<String, dynamic> data = new Map<String, dynamic>(); |
| 119 | + data['area'] = this.area; |
| 120 | + data['bucket'] = this.bucket; |
| 121 | + data['secretId'] = this.secretId; |
| 122 | + data['secretKey'] = this.secretKey; |
| 123 | + data['key'] = this.key; |
| 124 | + return data; |
15 | 125 | } |
16 | 126 | } |
0 commit comments