uploadSinglePart method
Implementation
Future<UploadPart> uploadSinglePart({
required String remotePath,
required String localPath,
required int memberLevel,
required String uploadid,
required int partseq,
}) async {
final pcsHost = 'd.pcs.baidu.com';
final path = 'rest/2.0/pcs/superfile2';
final method = 'upload';
final params = {
'type': 'tmpfile',
'path': Uri.encodeComponent(remotePath),
'uploadid': uploadid,
'partseq': partseq.toString(),
};
final blockSize = _getBlockSize(memberLevel);
final file = File(localPath);
final accessFile = file.openSync();
final start = blockSize * partseq;
var end = start + blockSize;
final fileLength = file.lengthSync();
if (end > fileLength) {
end = fileLength;
}
final bufferSize = end - start;
final buffer = List<int>.filled(bufferSize, 0, growable: true);
accessFile.setPositionSync(start);
final readBufferSize = accessFile.readIntoSync(buffer, 0, bufferSize);
accessFile.closeSync();
final uploadBytes = buffer.sublist(0, readBufferSize);
final fileName = Uri.file(remotePath).pathSegments.last;
final formFile =
http.MultipartFile.fromBytes('file', uploadBytes, filename: fileName);
final queryParameters = {
'access_token': accessToken,
'method': method,
...params,
};
final uri = Uri(
host: pcsHost,
scheme: 'https',
path: path,
queryParameters: queryParameters,
);
final request = http.MultipartRequest('POST', uri);
request.files.add(formFile);
final response = await request.send();
final body = await utf8.decodeStream(response.stream);
final map = json.decode(body);
return UploadPart.fromJson(map, uploadBytes.length);
}