preCreate method
Future<PreCreate>
preCreate({
- required String remotePath,
- required String localPath,
- UploadRenameRtype rtype = UploadRenameRtype.none,
- required int memberLevel,
- String? uploadid,
- BaiduMd5? md5,
- Md5Calculated? onMd5Calculated,
预上传,其实就是在远端创建文件或文件夹
remotePath
为远端的上传
localPath
为本地的文件路径
rtype
为重命名策略
uploadid
为上传的id, 这个是远端的上传id,理论上是用于后续的分片上传
memberLevel
为用户的会员等级,这个等级决定了可以上传多大的文件和切片规则
md5Calculated
为md5计算完成后的回调,用于获取到md5值
Implementation
Future<PreCreate> preCreate({
required String remotePath,
required String localPath,
UploadRenameRtype rtype = UploadRenameRtype.none,
required int memberLevel,
String? uploadid,
BaiduMd5? md5,
Md5Calculated? onMd5Calculated,
}) async {
final path = 'rest/2.0/xpan/file';
final method = 'precreate';
// remotePath = '/apps/$appName/$remotePath';
final body = <String, String>{
'path': Uri.encodeComponent(remotePath),
// 'path': remotePath,
'autoinit': '1',
'rtype': rtype.index.toString(),
};
// final srcLocalPath = localPath;
// // 先找到真实路径
// localPath = findRealPath(localPath);
//
// if (showLog) {
// print('$srcLocalPath => $localPath');
// }
// path 上传后使用的文件绝对路径,需要urlencode
// size int 是 4096 RequestBody参数 文件或目录的大小,单位B,目录的话大小为0
// isdir int 是 0 RequestBody参数 是否目录,0 文件、1 目录
// autoinit int 是 1 RequestBody参数 固定值1
// rtype int 否 1 RequestBody参数 文件命名策略,默认0
// 0 为不重命名,返回冲突
// 1 为只要path冲突即重命名
// 2 为path冲突且block_list不同才重命名
// 3 为覆盖
// uploadid string 否 P1-MTAuMjI4LjQzLjMxOjE1OTU4NTg== RequestBody参数 上传id
// block_list string 是 ["98d02a0f54781a93e354b1fc85caf488", "ca5273571daefb8ea01a42bfa5d02220"] RequestBody参数 文件各分片MD5数组的json串。block_list的含义如下,如果上传的文件小于4MB,其md5值(32位小写)即为block_list字符串数组的唯一元素;如果上传的文件大于4MB,需要将上传的文件按照4MB大小在本地切分成分片,不足4MB的分片自动成为最后一个分片,所有分片的md5值(32位小写)组成的字符串数组即为block_list。
// content-md5 string 否 b20f8ac80063505f264e5f6fc187e69a RequestBody参数 文件MD5
// slice-md5 string 否 9aa0aa691s5c0257c5ab04dd7eddaa47 RequestBody参数 文件校验段的MD5,校验段对应文件前256KB
// local_ctime string 否 1595919297 RequestBody参数 客户端创建时间, 默认为当前时间戳
// local_mtime string 否 1595919297 RequestBody参数 客户端修改时间,默认为当前时间戳
// 根据本地的 localPath 来创建参数
if (FileSystemEntity.isDirectorySync(localPath)) {
// 文件夹
body['size'] = '0';
body['isdir'] = '1';
} else if (FileSystemEntity.isFileSync(localPath)) {
body['size'] = File(localPath).lengthSync().toString();
body['isdir'] = '0';
} else {
throw Exception('不支持的类型');
}
final baiduMd5 = md5 ??
BaiduMd5(
filePath: localPath,
memberLevel: memberLevel,
);
final blockMd5 = baiduMd5.blockMd5List;
body['block_list'] = json.encode(blockMd5);
body['content-md5'] = await baiduMd5.contentMd5;
body['slice-md5'] = baiduMd5.sliceMd5;
onMd5Calculated?.call(baiduMd5);
final now = DateTime.now().millisecondsSinceEpoch / 1000;
body['local_ctime'] = now.toString();
body['local_mtime'] = now.toString();
if (uploadid != null) {
body['uploadid'] = uploadid;
}
final map = await _post(
path: path,
method: method,
bodyParams: body,
);
return PreCreate.fromJson(map);
}