9702d68d2d
Carry the protocol error code, wrap each machine's gateway so an elevation_required refusal shows one native password dialog, elevates that connection and retries; send the upload token with file uploads; add a Settings sign-in security section with the weak-password warning and sign out everywhere; accept the __Host- session cookie.
89 lines
2.7 KiB
Dart
89 lines
2.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import '../../../../core/transport/server_url.dart';
|
|
import '../../domain/chat_attachment_draft.dart';
|
|
import '../../domain/chat_attachment_wire.dart';
|
|
|
|
import 'chat_upload_stub.dart'
|
|
if (dart.library.io) 'chat_upload_io.dart'
|
|
if (dart.library.js_interop) 'chat_upload_web.dart'
|
|
as platform;
|
|
|
|
/// The server's reply to one stored chat file.
|
|
typedef ChatUploadReply = ({int status, String body});
|
|
|
|
/// Posts one file to the server's `/chat-uploads`: the bytes travel as their
|
|
/// own HTTP request, outside the websocket frame, and the reply names the
|
|
/// stored file a prompt then references.
|
|
Future<ChatUploadReply> postChatUpload(
|
|
Uri uri, {
|
|
required String name,
|
|
required String mime,
|
|
required Uint8List bytes,
|
|
Map<String, String> headers = const {},
|
|
}) => platform.postChatUpload(
|
|
uri,
|
|
name: name,
|
|
mime: mime,
|
|
bytes: bytes,
|
|
headers: headers,
|
|
);
|
|
|
|
/// The header that carries the file name; the server refuses a post without it.
|
|
const chatUploadNameHeader = 'X-Pipelines-Upload-Name';
|
|
|
|
/// Uploads [bytes] for [attachment] to the server behind [endpoint] and
|
|
/// returns the stored name a prompt references. Every failure is a
|
|
/// [ChatAttachmentWireException] in words the composer shows as they are.
|
|
Future<String> uploadChatAttachment(
|
|
String endpoint,
|
|
ChatAttachment attachment,
|
|
Uint8List bytes,
|
|
) async {
|
|
final uri = ServerUrl.resolveArtifactUrl(endpoint, '/chat-uploads');
|
|
if (uri == null) {
|
|
throw const ChatAttachmentWireException(
|
|
'No server is configured to upload files to.',
|
|
);
|
|
}
|
|
final ChatUploadReply reply;
|
|
try {
|
|
reply = await postChatUpload(
|
|
uri,
|
|
name: attachment.name.trim().isEmpty ? 'attachment' : attachment.name,
|
|
mime: attachment.mime.trim().isEmpty
|
|
? 'application/octet-stream'
|
|
: attachment.mime,
|
|
bytes: bytes,
|
|
);
|
|
} catch (error) {
|
|
throw ChatAttachmentWireException(
|
|
'"${attachment.name}" could not be uploaded: $error',
|
|
);
|
|
}
|
|
if (reply.status == 401) {
|
|
throw const ChatAttachmentWireException(
|
|
'Sign in to upload files (Settings → Sign in).',
|
|
);
|
|
}
|
|
if (reply.status != 200) {
|
|
final reason = reply.body.trim();
|
|
throw ChatAttachmentWireException(
|
|
reason.isEmpty
|
|
? '"${attachment.name}" could not be uploaded (HTTP ${reply.status}).'
|
|
: '"${attachment.name}" could not be uploaded: $reason',
|
|
);
|
|
}
|
|
try {
|
|
final decoded = jsonDecode(reply.body);
|
|
final upload = decoded is Map ? decoded['upload'] : null;
|
|
if (upload is String && upload.isNotEmpty) return upload;
|
|
} on FormatException {
|
|
// Reported below.
|
|
}
|
|
throw ChatAttachmentWireException(
|
|
'"${attachment.name}" upload returned no file name.',
|
|
);
|
|
}
|