uploadVideoBytes method

Future<UploadJsonResult> uploadVideoBytes(
  1. String? fileName,
  2. List<int>? fileBytes,
  3. String? mimeType
)

Implementation

Future<UploadJsonResult> uploadVideoBytes(
    String? fileName, List<int>? fileBytes, String? mimeType) 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, bytes ${fileBytes!.length}, mimeType $mimeType");
  //
  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(http.MultipartFile.fromBytes('file', fileBytes,
        filename: fileName,
        // 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);
}