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.
78 lines
2.5 KiB
78 lines
2.5 KiB
import 'dart:typed_data';
|
|
|
|
import 'package:flustars/flustars.dart';
|
|
import 'package:flutter_oss_aliyun/flutter_oss_aliyun.dart';
|
|
import 'package:dating_touchme_app/network/network_service.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import '../network/user_api.dart';
|
|
|
|
class OSSManager {
|
|
static OSSManager get instance => _instance;
|
|
// 静态单例实例
|
|
static final OSSManager _instance = OSSManager._internal();
|
|
bool isInit = false;
|
|
// 工厂构造函数,返回单例实例
|
|
factory OSSManager() {
|
|
return _instance;
|
|
}
|
|
|
|
// 私有构造函数,防止外部直接实例化
|
|
OSSManager._internal();
|
|
late UserApi _userApi;
|
|
|
|
Future<String> uploadFile(
|
|
Uint8List fileData,
|
|
String objectName, {
|
|
Function(int count, int total)? onSendProgress,
|
|
Function(int count, int total)? onReceiveProgress,
|
|
}) async {
|
|
try {
|
|
_userApi = Get.find<UserApi>();
|
|
final response = await _userApi.getApplyTempAuth();
|
|
// 调用getApplyTempAuth获取OSS临时授权
|
|
if (response.data.isSuccess) {
|
|
Client.init(
|
|
ossEndpoint: response.data.data?.endpoint ?? '',
|
|
bucketName: response.data.data?.bucketName ?? '',
|
|
authGetter: (){
|
|
return Auth(
|
|
accessKey: response.data.data?.accessKey ?? '',
|
|
accessSecret: response.data.data?.accessKeySecret ?? '',
|
|
secureToken: response.data.data?.token ?? '',
|
|
expire: DateUtil.formatDateMs(response.data.data?.expiredTime ?? 0)
|
|
);
|
|
}
|
|
);
|
|
final result = await Client().putObject(
|
|
fileData,
|
|
objectName,
|
|
option: PutRequestOption(
|
|
onSendProgress: onSendProgress != null
|
|
? (count, total) {
|
|
onSendProgress(count, total);
|
|
}
|
|
: null,
|
|
onReceiveProgress: onReceiveProgress != null
|
|
? (count, total) {
|
|
onReceiveProgress(count, total);
|
|
}
|
|
: null,
|
|
override: false,
|
|
aclModel: AclMode.publicRead,
|
|
storageType: StorageType.ia,
|
|
),
|
|
);
|
|
|
|
return result.statusCode == 200
|
|
? 'https://${response.data.data?.bucketName}.${response.data.data?.endpoint}/$objectName'
|
|
: throw Exception("上传失败");
|
|
}
|
|
return throw Exception("获取临时授权失败");
|
|
}catch(e){
|
|
print('上传失败$e');
|
|
return throw Exception("上传失败");
|
|
}
|
|
}
|
|
}
|