uploadDocument method

Future<TTransferTask?> uploadDocument(
  1. String filePath,
  2. String fileName,
  3. bool isPublic,
  4. String fileType,
  5. Uint8List? bytes,
  6. dynamic successCallBack(
    1. String? docId
    )?,
  7. dynamic progressCallBack(
    1. double progress
    )?,
  8. dynamic failCallBack(
    1. CosXmlClientException? clientException,
    2. CosXmlServiceException? serviceException
    )?,
)

Implementation

Future<TTransferTask?> uploadDocument(
  String filePath,
  String fileName,
  bool isPublic,
  String fileType,
  Uint8List? bytes,
  Function(String? docId)? successCallBack,
  Function(double progress)? progressCallBack,
  Function(
    CosXmlClientException? clientException,
    CosXmlServiceException? serviceException,
  )?
  failCallBack,
) 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(
            "uploadFail: clientException=$p0 | serviceException: code=${p1?.errorCode} msg=${p1?.errorMessage} requestId=${p1?.requestId}",
            actionModule: ActionModule.documentUseCases.name,
            actionName: ActionName.uploadDocument.name,
          );
          failCallBack?.call(p0, p1);
        },
        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,
          );
        },
      );
    }
  }
  TCICLog.error(
    "uploadDocument failed before COS upload: missing schoolInfo or cosToken",
    actionModule: ActionModule.documentUseCases.name,
    actionName: ActionName.uploadDocument.name,
  );
  failCallBack?.call(null, null);
  return null;
}