authReqUpload method
Implementation
Future<dynamic> authReqUpload(String path, File f,
{Map<String, dynamic>? body,
Map<String, String>? headers,
Map<String, String>? context,
ProgressCallback? progress}) async {
if (body == null) {
body = <String, dynamic>{};
}
var size = await f.length();
body["filename"] ??= p.basename(f.path);
body["type"] ??= lookupMimeType(body["filename"]) ?? "application/octet-stream";
body["size"] = size;
// first, get upload ready
var res = await authReq(path, method: "POST", body: body, context: context);
var r = http.StreamedRequest("PUT", Uri.parse(res["PUT"]));
r.contentLength = size; // required so upload is not chunked
r.headers["Content-Type"] = body["type"];
void Function(List<int> event) add = r.sink.add;
if (progress != null) {
int current = 0;
var add2 = add;
add = (List<int> event) {
current += event.length;
progress(current / size);
add2(event); // this shall call the original add
};
}
// connect file to sink
f.openRead().listen(add, onDone: r.sink.close, onError: r.sink.addError);
// perform upload
var postRes = await http.Client().send(r);
if (postRes.statusCode >= 300) {
// something went wrong
var postBody = await postRes.stream.bytesToString();
throw AtOnlineNetworkException(postBody);
}
// call finalize, return response
return await req(res["Complete"], method: "POST", context: context);
}