livePostFileUpload static method

Future<DataResult> livePostFileUpload({
  1. required String headImage,
})

Implementation

static Future<DataResult> livePostFileUpload(
    {required String headImage}) async {
  String? image = await BaseDao.compressImage(headImage);
  File imageFile = File(headImage);
  Uint8List bytes = imageFile.readAsBytesSync();
  var name = image.substring(image.lastIndexOf('/') + 1, image.length);
  var baseEntity = await BaseDao.fromBaseEncryptV3(
    {"contentLength": bytes.length.toString(), "fileName": name},
    Address.livePostFileUpload(),
  );
  if (baseEntity.result) {
    try {
      Response response;
      Options options = Options(headers: {
        "Content-Type": baseEntity.data['contentType'],
        "Content-Length": bytes.length.toString()
      });
      response = await Dio().put(baseEntity.data['preUrl'],
          data: MultipartFile.fromBytes(bytes).finalize(),
          options: options, onSendProgress: (count, total) {
        AppConfig.printLog(count / total);
      }, onReceiveProgress: (data, to) {});
      if (response.statusCode == 200) {
        var res = await BaseDao.fromBaseEncryptV3(
          {
            "ossName": baseEntity.data['ossName'],
            "preUrl": baseEntity.data['preUrl']
          },
          Address.livePostFileSuccess(),
        );
        if (res.result) {
          return DataResult(res.data, true);
        }
      }
    } catch (e) {
      AppConfig.printLog(e);
    }
  }
  return baseEntity;
}