feat(chat): files upload over HTTP before the prompt, up to 100 MiB each
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import 'dart:js_interop';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'chat_upload.dart';
|
||||
|
||||
@JS('fetch')
|
||||
external JSPromise<_Response> _fetch(JSString input, _Init init);
|
||||
|
||||
extension type _Init._(JSObject _) implements JSObject {
|
||||
external factory _Init({
|
||||
JSString method,
|
||||
JSAny headers,
|
||||
JSAny body,
|
||||
JSString credentials,
|
||||
});
|
||||
}
|
||||
|
||||
extension type _Response._(JSObject _) implements JSObject {
|
||||
external int get status;
|
||||
|
||||
external JSPromise<JSString> text();
|
||||
}
|
||||
|
||||
/// The browser keeps the login cookie; `same-origin` sends it only to the
|
||||
/// page's own server.
|
||||
Future<ChatUploadReply> postChatUpload(
|
||||
Uri uri, {
|
||||
required String name,
|
||||
required String mime,
|
||||
required Uint8List bytes,
|
||||
}) async {
|
||||
final response = await _fetch(
|
||||
uri.toString().toJS,
|
||||
_Init(
|
||||
method: 'POST'.toJS,
|
||||
headers: {
|
||||
chatUploadNameHeader: Uri.encodeComponent(name),
|
||||
'Content-Type': mime,
|
||||
}.jsify()!,
|
||||
body: bytes.toJS,
|
||||
credentials: 'same-origin'.toJS,
|
||||
),
|
||||
).toDart;
|
||||
final body = (await response.text().toDart).toDart;
|
||||
return (status: response.status, body: body);
|
||||
}
|
||||
Reference in New Issue
Block a user