uploadVideo method

Future<UploadJsonResult> uploadVideo(
  1. String? filePath
)

Implementation

Future<UploadJsonResult> uploadVideo(String? filePath) async {
  // FIXME: image_picker有bug,选择视频后缀为.jpg,此处替换一下
  String? fileName = filePath!.split("/").last.replaceAll(".jpg", ".mp4");
  String? username = SpUtil.getString(BytedeskConstants.uid);
  const uploadUrl =
      '${BytedeskConstants.httpUploadUrl}/visitor/api/upload/video';
  BytedeskUtils.printLog(
      "uploadVideo fileName $fileName, username $username, upload Url $uploadUrl");
  //
  Map<String, String> headers = {
    HttpHeaders.contentTypeHeader: "video/mp4",
  };
  var uri = Uri.parse(uploadUrl);
  var request = http.MultipartRequest('POST', uri)
    ..fields['file_name'] = "${username!}_$fileName"
    ..fields['username'] = username
    ..headers.addAll(headers)
    ..files.add(await http.MultipartFile.fromPath('file', filePath,
        // FIXME: 设置不起作用?
        contentType: MediaType('video', 'mp4')));

  http.Response response =
      await http.Response.fromStream(await request.send());
  // debugPrint("Result: ${response.body}");

  //解决json解析中的乱码问题
  Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
  //将string类型数据 转换为json类型的数据
  final responseJson = json.decode(utf8decoder.convert(response.bodyBytes));
  debugPrint("upload Video responseJson $responseJson");
  //
  return UploadJsonResult.fromJson(responseJson);
}