Localize IPFS upload and attach messages

Give IpfsUploadConfigException a reason enum mapped to l10n strings in
the attach flow, and show a localized empty-file message and generic
upload-failed message instead of raw English exception text.
This commit is contained in:
zjs81 committed 2026-10-24 21:58:39 -07:00
1 parent 34db64d7af
commit c111835bd0
24 files changed
+536 -13

No files matched your search

+25 -2
View File
@@ -4,6 +4,7 @@ import 'package:file_selector/file_selector.dart' as file_selector;
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../l10n/app_localizations.dart';
import '../l10n/l10n.dart';
import '../services/app_settings_service.dart';
import 'ipfs_upload_helper.dart';
@@ -58,7 +59,7 @@ Future<void> attachIpfsLinkToDraft({
if (!context.mounted) return;
showDismissibleSnackBar(
context,
content: Text(l10n.chat_ipfsUploadFailed('$error')),
content: Text(ipfsUploadErrorText(l10n, error)),
);
}
@@ -69,7 +70,10 @@ Future<void> attachIpfsLinkToDraft({
final bytes = await file.readAsBytes();
if (!context.mounted) return;
if (bytes.isEmpty) {
showFailure('The selected file is empty.');
showDismissibleSnackBar(
context,
content: Text(l10n.chat_ipfsSelectedFileEmpty),
);
return;
}
messageUrl = await IpfsUploadHelper.uploadImageWithSettings(
@@ -108,6 +112,25 @@ Future<void> attachIpfsLinkToDraft({
}
}
/// The localized snackbar text for an upload [error]: config problems get a
/// specific message, anything else a generic one with technical details.
@visibleForTesting
String ipfsUploadErrorText(AppLocalizations l10n, Object error) {
if (error is IpfsUploadConfigException) {
return switch (error.reason) {
IpfsUploadConfigError.emptyEndpoint => l10n.chat_ipfsUploadNotConfigured,
IpfsUploadConfigError.invalidEndpoint => l10n.chat_ipfsEndpointInvalid,
IpfsUploadConfigError.pinataTokenMissing =>
l10n.chat_ipfsPinataTokenMissing,
IpfsUploadConfigError.filebaseTokenMissing =>
l10n.chat_ipfsFilebaseTokenMissing,
IpfsUploadConfigError.kuboPathInvalid => l10n.chat_ipfsKuboPathInvalid,
};
}
final details = '$error'.replaceFirst(RegExp(r'^Exception: '), '');
return l10n.chat_ipfsUploadFailed(details);
}
/// Puts [messageUrl] in front of [existingText], leaving the text intact.
@visibleForTesting
String composeIpfsLinkDraft({
+20 -11
View File
@@ -5,15 +5,24 @@ import 'package:http/http.dart' as http;
import '../models/app_settings.dart';
/// Thrown when the IPFS upload settings can't be used, before or instead of
/// a network failure.
class IpfsUploadConfigException implements Exception {
const IpfsUploadConfigException(this.message);
/// Why the IPFS upload settings can't be used.
enum IpfsUploadConfigError {
emptyEndpoint,
invalidEndpoint,
pinataTokenMissing,
filebaseTokenMissing,
kuboPathInvalid,
}
final String message;
/// Thrown when the IPFS upload settings can't be used, before or instead of
/// a network failure. The UI maps [reason] to a localized message.
class IpfsUploadConfigException implements Exception {
const IpfsUploadConfigException(this.reason);
final IpfsUploadConfigError reason;
@override
String toString() => message;
String toString() => 'IpfsUploadConfigException(${reason.name})';
}
class IpfsUploadHelper {
@@ -80,7 +89,7 @@ class IpfsUploadHelper {
final endpoint = uploadEndpoint.trim();
if (endpoint.isEmpty) {
throw const IpfsUploadConfigException(
'Configure an IPFS upload endpoint first.',
IpfsUploadConfigError.emptyEndpoint,
);
}
@@ -89,7 +98,7 @@ class IpfsUploadHelper {
!(endpointUri.isScheme('http') || endpointUri.isScheme('https')) ||
endpointUri.host.isEmpty) {
throw const IpfsUploadConfigException(
'The IPFS upload endpoint must be an http(s) URL.',
IpfsUploadConfigError.invalidEndpoint,
);
}
@@ -103,17 +112,17 @@ class IpfsUploadHelper {
final safeFileName = _sanitizeUploadFileName(fileName);
if (isPinata && token.isEmpty) {
throw const IpfsUploadConfigException(
'Pinata requires an API key or JWT.',
IpfsUploadConfigError.pinataTokenMissing,
);
}
if (isFilebase && token.isEmpty) {
throw const IpfsUploadConfigException(
'Filebase RPC requires an API token.',
IpfsUploadConfigError.filebaseTokenMissing,
);
}
if (isKubo && !_kuboAddPath.hasMatch(endpointUri.path)) {
throw const IpfsUploadConfigException(
'Custom (Kubo) upload endpoint must end with /api/v0/add.',
IpfsUploadConfigError.kuboPathInvalid,
);
}