writeTo method
Writes the part into sink without holding it in memory.
Returns how many bytes were written, and refuses past limit — an upload
with no ceiling is a way to fill your disk.
The sink is not closed: the caller opened it and may have more to write.
Implementation
Future<int> writeTo(
StreamConsumer<List<int>> sink, {
int limit = defaultBodyLimit,
}) async {
var written = 0;
final counted = content.map((chunk) {
written += chunk.length;
if (written > limit) {
throw Rejection.payloadTooLarge(
'multipart part "$name" exceeds $limit bytes',
);
}
return chunk;
});
await sink.addStream(counted);
return written;
}