chunkedUploadFile method

  1. @override
Future<bool> chunkedUploadFile(
  1. Uri url,
  2. String serverType,
  3. String uploadPath,
  4. String uploadName,
  5. String fileType,
  6. Uint8List fileBytes, {
  7. void progressCallback(
    1. int loaded,
    2. int total
    )?,
})
override

Implementation

@override
Future<bool> chunkedUploadFile(
  Uri url,
  String serverType,
  String uploadPath,
  String uploadName,
  String fileType,
  Uint8List fileBytes, {
  void Function(int loaded, int total)? progressCallback,
}) async {
  try {
    final int fileSize = fileBytes.length;
    const int chunkSize = 6 * 1024 * 1024;
    final int totalChunks = (fileSize / chunkSize).ceil();
    int startBytes = 0;
    for (int i = 1; i <= totalChunks; i++) {
      final endBytes = min(startBytes + chunkSize, fileSize);
      final chunk = fileBytes.sublist(startBytes, endBytes);
      final multipartFile = http.MultipartFile(
        'file',
        Stream.fromIterable([chunk]),
        fileSize,
        filename: uploadName,
        contentType: MediaType.parse(fileType),
      );
      final request = http.MultipartRequest('POST', url)
        ..fields['serverType'] = serverType
        ..fields['uploadPath'] = uploadPath
        ..fields['totalChunks'] = '$totalChunks'
        ..fields['currentChunk'] = '$i'
        ..files.add(multipartFile);
      final resp = await request.send();
      await resp.stream.bytesToString();
      startBytes = endBytes;
      if (progressCallback != null) {
        progressCallback(startBytes, fileSize);
      }
      if (resp.statusCode != 200) {
        return false;
      }
    }
    return true;
  } catch (err) {
    logger.severe('Error at StorageUtilImpl.chunkedUploadFile >>> $err');
    return false;
  }
}