uploadDocument method
Implementation
Future<TTransferTask?> uploadDocument(
String filePath,
String fileName,
bool isPublic,
String fileType,
Uint8List? bytes,
Function(String? docId)? successCallBack,
Function(double progress)? progressCallBack,
) async {
TCICGetSchoolInfoRepModel? schoolInfo =
await TCICController.instance.getSchoolInfo();
final classInfo = await TCICController.instance.getClassInfo();
if (schoolInfo != null) {
final schoolId = schoolInfo.schoolId;
int totalSize = 0;
final cosTokenResult = await _repostory.getCosToken(
TGetCosTokenParam(schoolId: schoolId, duration: 60),
);
if (cosTokenResult != null) {
await TCos.instance.initialize(cosTokenResult);
String uploadKey = "";
if (cosTokenResult.path.isNotEmpty &&
cosTokenResult.docName.isNotEmpty) {
// 后台有返回path和shuffle(新协议)
uploadKey =
'${cosTokenResult.path}${cosTokenResult.docName}.$fileType';
} else {
uploadKey =
'document/${DateTime.now().millisecondsSinceEpoch}_$fileName';
}
return TCos.instance.uploadFile(
region: cosTokenResult.region,
bucketName: '${cosTokenResult.bucketName}-${cosTokenResult.appId}',
cosPath: uploadKey,
srcPath: filePath,
bytes: bytes,
specifiedContentType: _getType(fileType),
successCallBack: (header, result) {
if (result != null) {
final createDocumentParam = CreateDocumentParams(
docName: fileName,
docSize: totalSize,
schoolId: schoolId,
docType: fileType,
owner: classInfo!.roomInfo.teacherId,
transcodeType: _getFileTranscodeType(fileType),
permission: isPublic ? 1 : 0,
docUrl: result.accessUrl!,
);
_addDocument(createDocumentParam, classInfo.classId).then((
docId,
) {
successCallBack?.call(docId);
});
}
},
failCallBack: (p0, p1) {
TCICLog.error(
"ClientException $p0 ${p1?.requestId}",
actionModule: ActionModule.documentUseCases.name,
actionName: ActionName.uploadDocument.name,
);
},
progressCallBack: (complete, target) {
totalSize = target;
progressCallBack?.call((complete / target) * 100);
TCICLog.info(
"progress ${(complete / target) * 100}% $complete $target",
actionModule: ActionModule.documentUseCases.name,
actionName: ActionName.uploadDocument.name,
);
},
);
}
}
return null;
}