Files
meshcore-open/lib/helpers/ipfs_link_attach_flow.dart
T
zjs81 084a8fb98b Show IPFS upload configuration errors instead of failing silently
Throw IpfsUploadConfigException for upload configuration problems, and
route every failure in the attach flow to the user, so a missing key or
bad endpoint no longer escapes as an unhandled ArgumentError.
2026-10-24 21:57:35 -07:00

123 lines
3.6 KiB
Dart

import 'dart:convert';
import 'package:file_selector/file_selector.dart' as file_selector;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../l10n/l10n.dart';
import '../services/app_settings_service.dart';
import 'ipfs_upload_helper.dart';
import 'snack_bar_builder.dart';
/// Shared "send image via IPFS" flow for the direct-message and channel chat
/// screens: picks a file, uploads it with the configured IPFS settings and
/// puts the resulting link at the start of the composer draft.
///
/// [maxBytes] is the screen's outbound byte limit and [encodeOutbound] turns
/// draft text into what the radio would actually send, so the limit is
/// checked against the encoded form.
Future<void> attachIpfsLinkToDraft({
required BuildContext context,
required TextEditingController controller,
required FocusNode focusNode,
required int maxBytes,
required String Function(String text) encodeOutbound,
}) async {
final l10n = context.l10n;
final settings = context.read<AppSettingsService>().settings;
if (settings.uploadIpfsEndpoint.trim().isEmpty) {
showDismissibleSnackBar(
context,
content: Text(l10n.chat_ipfsUploadNotConfigured),
);
return;
}
void showFailure(Object error) {
if (!context.mounted) return;
showDismissibleSnackBar(
context,
content: Text(l10n.chat_ipfsUploadFailed('$error')),
);
}
final String messageUrl;
try {
final file = await file_selector.openFile();
if (file == null || !context.mounted) return;
final bytes = await file.readAsBytes();
if (!context.mounted) return;
if (bytes.isEmpty) {
showFailure('The selected file is empty.');
return;
}
messageUrl = await IpfsUploadHelper.uploadImageWithSettings(
settings: settings,
bytes: bytes,
fileName: file.name,
mimeType: IpfsUploadHelper.mimeTypeForFileName(file.name),
);
} on Exception catch (error) {
showFailure(error);
return;
} catch (error, stackTrace) {
FlutterError.reportError(
FlutterErrorDetails(
exception: error,
stack: stackTrace,
library: 'ipfs link attach',
),
);
showFailure(error);
return;
}
if (!context.mounted) return;
final messageText = composeIpfsLinkDraft(
messageUrl: messageUrl,
existingText: controller.text.trim(),
maxBytes: maxBytes,
encodeOutbound: encodeOutbound,
);
if (messageText == null) {
showDismissibleSnackBar(
context,
content: Text(l10n.chat_messageTooLong(maxBytes)),
);
return;
}
controller.value = TextEditingValue(
text: messageText,
selection: TextSelection.collapsed(offset: messageText.length),
);
focusNode.requestFocus();
}
/// Builds the draft with [messageUrl] in front of [existingText], trimming
/// [existingText] from the end until the encoded result fits [maxBytes].
/// Returns null when even the link alone does not fit.
@visibleForTesting
String? composeIpfsLinkDraft({
required String messageUrl,
required String existingText,
required int maxBytes,
required String Function(String text) encodeOutbound,
}) {
bool fits(String text) =>
utf8.encode(encodeOutbound(text)).length <= maxBytes;
final messageText = existingText.isEmpty
? '$messageUrl '
: '$messageUrl\n$existingText';
if (fits(messageText)) return messageText;
if (existingText.isEmpty) return null;
final originalCharacters = existingText.characters.toList();
for (var count = originalCharacters.length - 1; count >= 0; count--) {
final candidate = '$messageUrl\n${originalCharacters.take(count).join()}';
if (fits(candidate)) return candidate;
}
return null;
}