微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在颤动中将分块的视频文件上传到 cloudinary

如何解决在颤动中将分块的视频文件上传到 cloudinary

我正在尝试将大于 100mb 的视频文件分块上传到 cloudinary。

我创建了一个类来分割视频文件上传每个块。

import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:http/http.dart' as http;

import 'network.dart';

class ChunkedUploader {
  final http.Client _client;

  ChunkedUploader(this._client);

  Future<http.StreamedResponse> upload({
    String url,File file,Map<String,dynamic> body,int maxChunkSize,int uniqueId,Function(double) onUploadProgress,}) =>
      UploadRequest(
        _client,url: url,file: file,body: body,maxChunkSize: maxChunkSize,uniqueId: uniqueId,onUploadProgress: onUploadProgress,).upload();
}

class UploadRequest {
  final http.Client client;
  final String url;
  final File file;
  final Map<String,dynamic> body;
  final int uniqueId;
  final Function(double) onUploadProgress;
  int _fileSize;
  int _maxChunkSize;

  UploadRequest(
    this.client,{
    this.url,this.file,this.body,this.uniqueId,this.onUploadProgress,}) {
    _fileSize = file.lengthSync();
    _maxChunkSize = min(_fileSize,maxChunkSize ?? _fileSize);
  }

  Future<http.StreamedResponse> upload() async {

    http.StreamedResponse finalResponse;

    for (int i = 0; i < _chunksCount; i++) {
      final start = _getChunkStart(i);
      final end = _getChunkEnd(i);
      final chunkStream = _getChunkStream(start,end);
      

      MultipartRequest request = MultipartRequest(
        "POST",Uri.parse(url),onProgress: (int bytes,int total) {
          _updateProgress(i,bytes,total);
        },);

      //Add the headers
      request.headers.addAll(_getHeaders(start,end));

      //Add the body
      request.fields.addAll(body);

      //Get the file
      http.multipartfile multipartfile =
          http.multipartfile("file",chunkStream,end - start);
      request.files.add(multipartfile);

      //Send the file
      http.StreamedResponse response =
          await client.send(request).catchError((error) {
        throw error;
      });

      print(response);
      finalResponse = response;
    }
    return finalResponse;
  }

  Stream<List<int>> _getChunkStream(int start,int end) =>
      file.openRead(start,end);

  // Updating total upload progress
  _updateProgress(int chunkIndex,int chunkCurrent,int chunkTotal) {
    int totalUploadedSize = (chunkIndex * _maxChunkSize) + chunkCurrent;
    double totalUploadProgress = (totalUploadedSize / _fileSize) * 100;
    this.onUploadProgress?.call(totalUploadProgress);
  }

  // Returning start byte offset of current chunk
  int _getChunkStart(int chunkIndex) => chunkIndex * _maxChunkSize;

  // Returning end byte offset of current chunk
  int _getChunkEnd(int chunkIndex) =>
      min((chunkIndex + 1) * _maxChunkSize,_fileSize);

  // Returning a header map object containing Content-Range
  // https://tools.ietf.org/html/rfc7233#section-2
  Map<String,String> _getHeaders(int start,int end) => {
        'X-Unique-Upload-Id': "$uniqueId",'Content-Range': 'bytes $start-${end - 1}/$_fileSize',};

  // Returning chunks count based on file size and maximum chunk size
  int get _chunksCount => (_fileSize / _maxChunkSize).ceil();
}

在提出请求时,我收到了

回复
Status code - 500
Body - {
    “error” : {
        “message” : "General Error”
    }
}

我已与 cloudinary 取得联系,他们已找到该请求,并表示该请求失败并显示错误:UTF-8 中的字节序列无效。

任何帮助将不胜感激

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。