You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.5 KiB
117 lines
3.5 KiB
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:common_utils/common_utils.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:tencentcloud_cos_sdk_plugin/cos.dart';
|
|
import 'package:tencentcloud_cos_sdk_plugin/cos_transfer_manger.dart';
|
|
import 'package:tencentcloud_cos_sdk_plugin/pigeon.dart';
|
|
|
|
import '../../data/models/appconfig.dart';
|
|
|
|
class UploadOss {
|
|
static final UploadOss _shared = UploadOss._internal();
|
|
factory UploadOss() => _shared;
|
|
|
|
static String bucket = '';
|
|
static String region = '';
|
|
static String secretId = '';
|
|
static String secretKey = '';
|
|
static String userId = '';
|
|
|
|
static CosTransferManger? _cosTransferManger;
|
|
|
|
UploadOss._internal();
|
|
|
|
static initOss() {
|
|
bucket = AppConfig.env('COS_BUCKET_NAME') ?? '';
|
|
region = AppConfig.env('COS_REGION') ?? '';
|
|
secretId = AppConfig.env('COS_SECRET_ID') ?? '';
|
|
secretKey = AppConfig.env('COS_SECRET_KEY') ?? '';
|
|
|
|
final GetStorage storage = GetStorage();
|
|
Map? userInfo = storage.read("user_info");
|
|
if (userInfo != null) {
|
|
userId = userInfo['user']['uid'];
|
|
} else {
|
|
userId = '';
|
|
}
|
|
}
|
|
|
|
static Future<CosTransferManger> getTransferManger() async {
|
|
if (_cosTransferManger == null) {
|
|
await Cos().initWithPlainSecret(secretId, secretKey);
|
|
if (Cos().hasTransferManger(region)) {
|
|
_cosTransferManger = Cos().getTransferManger(region);
|
|
} else {
|
|
CosXmlServiceConfig serviceConfig = CosXmlServiceConfig(
|
|
region: region,
|
|
isHttps: true,
|
|
);
|
|
_cosTransferManger = await Cos().registerTransferManger(
|
|
region, serviceConfig..region = region, TransferConfig());
|
|
}
|
|
}
|
|
return _cosTransferManger!;
|
|
}
|
|
|
|
static Future<String> upload({
|
|
String? filepath,
|
|
String rootDir = 'file',
|
|
String? fileType,
|
|
Function? callback,
|
|
ProgressCallback? onSendProgress,
|
|
}) async {
|
|
if (bucket.isEmpty ||
|
|
region.isEmpty ||
|
|
secretId.isEmpty ||
|
|
secretKey.isEmpty ||
|
|
userId.isEmpty) {
|
|
initOss();
|
|
}
|
|
String pathName = userId.isNotEmpty
|
|
? 'User/$userId/$rootDir/${getDate()}/${getRandom(12)}.${fileType ?? getFileType(filepath!)}'
|
|
: 'User/$rootDir/${getDate()}/${getRandom(12)}.${fileType ?? getFileType(filepath!)}';
|
|
CosTransferManger cosTransferManger = await getTransferManger();
|
|
final completer = Completer<String>();
|
|
await cosTransferManger.upload(
|
|
bucket,
|
|
pathName,
|
|
filePath: filepath,
|
|
progressCallBack: onSendProgress,
|
|
resultListener: ResultListener(
|
|
(Map<String?, String?>? header, CosXmlResult? result) {
|
|
final url = result?.accessUrl ??
|
|
'https://$bucket.cos.$region.myqcloud.com/$pathName';
|
|
completer.complete(url);
|
|
},
|
|
(clientException, serviceException) {
|
|
completer.completeError(clientException ??
|
|
serviceException ??
|
|
Exception('Unknown error'));
|
|
},
|
|
),
|
|
);
|
|
return completer.future;
|
|
}
|
|
|
|
static String getDate() {
|
|
DateTime now = DateTime.now();
|
|
return DateUtil.formatDate(now, format: 'yyyy/MM/dd');
|
|
}
|
|
|
|
static String getRandom(int num) {
|
|
String alphabet = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
|
|
String left = '';
|
|
for (var i = 0; i < num; i++) {
|
|
left = left + alphabet[Random().nextInt(alphabet.length)];
|
|
}
|
|
return left;
|
|
}
|
|
|
|
static String getFileType(String path) {
|
|
List<String> array = path.split('.');
|
|
return array[array.length - 1];
|
|
}
|
|
}
|
|
|