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.
134 lines
4.1 KiB
Dart
134 lines
4.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import '../../../core/servers/elevation/machine_elevation.dart';
|
|
import '../../../core/transport/server_url.dart';
|
|
import '../data/upload/chat_upload.dart';
|
|
import '../domain/chat_attachment_picker.dart';
|
|
|
|
/// Posts one file's bytes to a `POST /files?path=<folder>` URL.
|
|
typedef FilesUploadPost =
|
|
Future<ChatUploadReply> Function(
|
|
Uri uri, {
|
|
required String name,
|
|
required String mime,
|
|
required Uint8List bytes,
|
|
Map<String, String> headers,
|
|
});
|
|
|
|
/// The transport the Files panel uploads with.
|
|
///
|
|
/// Production keeps [postChatUpload] (raw body, name header, session cookie).
|
|
/// Widget tests swap in a recorder so no socket is opened. Restore it after a
|
|
/// test.
|
|
FilesUploadPost postFileUpload = postChatUpload;
|
|
|
|
/// Why one file could not be stored, in words the panel shows as they are.
|
|
class FilesUploadException implements Exception {
|
|
const FilesUploadException(this.message);
|
|
final String message;
|
|
@override
|
|
String toString() => message;
|
|
}
|
|
|
|
/// Uploads [file] into [directory] on the server behind [endpoint] and returns
|
|
/// the stored path the server reports. Every failure is a
|
|
/// [FilesUploadException]; nothing is retried or renamed on the caller's
|
|
/// behalf, so an existing file is reported rather than replaced.
|
|
///
|
|
/// [elevation] is the machine's step-up state: its token rides on the
|
|
/// request as [elevationHeader], and a 403 `elevation_required` refusal asks
|
|
/// for the password once and retries.
|
|
Future<String> uploadFileInto(
|
|
String endpoint,
|
|
String directory,
|
|
ChatPickedFile file, {
|
|
MachineElevation? elevation,
|
|
}) async {
|
|
final name = file.name.trim();
|
|
if (name.isEmpty) {
|
|
throw const FilesUploadException(
|
|
'A file without a name cannot be uploaded.',
|
|
);
|
|
}
|
|
final Uri uri;
|
|
try {
|
|
uri = ServerUrl.resourceUri(
|
|
endpoint,
|
|
'/files',
|
|
query: <String, String>{'path': directory},
|
|
);
|
|
} on FormatException {
|
|
throw const FilesUploadException(
|
|
'No server is configured to upload files to.',
|
|
);
|
|
}
|
|
Future<ChatUploadReply> post() async {
|
|
final token = elevation?.token;
|
|
try {
|
|
return await postFileUpload(
|
|
uri,
|
|
name: name,
|
|
mime: file.mime.trim().isEmpty ? 'application/octet-stream' : file.mime,
|
|
bytes: file.bytes,
|
|
headers: {elevationHeader: ?token},
|
|
);
|
|
} catch (error) {
|
|
throw FilesUploadException('"$name" could not be uploaded: $error');
|
|
}
|
|
}
|
|
|
|
var reply = await post();
|
|
if (elevation != null && refusedForElevation(reply)) {
|
|
elevation.clear();
|
|
if (await elevation.ensure()) reply = await post();
|
|
}
|
|
final reason = reply.body.trim();
|
|
switch (reply.status) {
|
|
case 200:
|
|
break;
|
|
case 401:
|
|
throw const FilesUploadException(
|
|
'Sign in to upload files (Settings → Sign in).',
|
|
);
|
|
case 403 when refusedForElevation(reply):
|
|
throw FilesUploadException(
|
|
'"$name" was not uploaded: confirm the password to change files.',
|
|
);
|
|
case 409:
|
|
throw FilesUploadException('"$name" already exists in this folder.');
|
|
case 413:
|
|
throw FilesUploadException(
|
|
'"$name" is too large to upload (100 MiB limit).',
|
|
);
|
|
default:
|
|
throw FilesUploadException(
|
|
reason.isEmpty
|
|
? '"$name" could not be uploaded (HTTP ${reply.status}).'
|
|
: '"$name" could not be uploaded: $reason',
|
|
);
|
|
}
|
|
try {
|
|
final decoded = jsonDecode(reply.body);
|
|
final path = decoded is Map ? decoded['path'] : null;
|
|
if (path is String && path.isNotEmpty) return path;
|
|
} on FormatException {
|
|
// Reported below.
|
|
}
|
|
throw FilesUploadException('"$name" upload returned no stored path.');
|
|
}
|
|
|
|
/// The request header carrying a confirmed connection's elevation token.
|
|
const elevationHeader = 'X-Pipelines-Elevation';
|
|
|
|
/// Whether [reply] is the server's 403 step-up refusal.
|
|
bool refusedForElevation(ChatUploadReply reply) {
|
|
if (reply.status != 403) return false;
|
|
try {
|
|
final decoded = jsonDecode(reply.body);
|
|
return decoded is Map && decoded['code'] == elevationRequiredCode;
|
|
} on FormatException {
|
|
return false;
|
|
}
|
|
}
|