upload url image or file to ipfs
This commit is contained in:
1 parent
9609a8231d
commit
a4a7e00732
33 files changed
+2594
-81
No files matched your search
@@ -6,6 +6,7 @@ import 'dart:ui' as ui;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:file_selector/file_selector.dart' as file_selector;
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
@@ -18,6 +19,7 @@ import '../helpers/chat_scroll_controller.dart';
|
||||
import '../connector/meshcore_protocol.dart';
|
||||
import '../helpers/cyr2lat.dart';
|
||||
import '../helpers/gif_helper.dart';
|
||||
import '../helpers/ipfs_upload_helper.dart';
|
||||
import '../helpers/message_url_image_helper.dart';
|
||||
import '../helpers/path_helper.dart';
|
||||
import '../helpers/reaction_helper.dart';
|
||||
@@ -40,7 +42,6 @@ import '../widgets/received_image_message.dart';
|
||||
import '../widgets/byte_count_input.dart';
|
||||
import '../widgets/chat_day_separator.dart';
|
||||
import '../widgets/empty_state.dart';
|
||||
|
||||
import '../widgets/chat_zoom_wrapper.dart';
|
||||
import '../widgets/emoji_picker.dart';
|
||||
import '../widgets/gif_message.dart';
|
||||
@@ -613,6 +614,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
bool urlImagesEnabled,
|
||||
) {
|
||||
final settingsService = context.watch<AppSettingsService>();
|
||||
final publicIpfsGateway = settingsService.settings.publicIpfsGateway;
|
||||
final enableTracing = settingsService.settings.enableMessageTracing;
|
||||
final regionOnHopsLine = settingsService.settings.regionOnHopsLine;
|
||||
final isOutgoing = message.isOutgoing;
|
||||
@@ -788,6 +790,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
MessageUrlImageFutureBuilder(
|
||||
messageId: message.messageId,
|
||||
text: message.text,
|
||||
publicGateway: publicIpfsGateway,
|
||||
builder: (context, snapshot) {
|
||||
final imageAttachment = snapshot.data;
|
||||
|
||||
@@ -1360,6 +1363,113 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _sendImageViaIpfs() async {
|
||||
final l10n = context.l10n;
|
||||
final settings = context.read<AppSettingsService>().settings;
|
||||
if (settings.uploadIpfsEndpoint.trim().isEmpty) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.chat_ipfsUploadNotConfigured),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final file_selector.XFile? file;
|
||||
try {
|
||||
file = await file_selector.openFile();
|
||||
} on Exception catch (error) {
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.chat_ipfsUploadFailed('$error')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (file == null) return;
|
||||
if (!mounted) return;
|
||||
|
||||
final Uint8List bytes;
|
||||
try {
|
||||
bytes = await file.readAsBytes();
|
||||
} on Exception catch (error) {
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.chat_ipfsUploadFailed('$error')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (bytes.isEmpty) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
l10n.chat_ipfsUploadFailed('The selected file is empty.'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final String messageUrl;
|
||||
try {
|
||||
messageUrl = await IpfsUploadHelper.uploadImageWithSettings(
|
||||
settings: settings,
|
||||
bytes: bytes,
|
||||
fileName: file.name,
|
||||
mimeType: IpfsUploadHelper.mimeTypeForFileName(file.name),
|
||||
);
|
||||
} on Exception catch (error) {
|
||||
if (!mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.chat_ipfsUploadFailed('$error')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
|
||||
final connector = context.read<MeshCoreConnector>();
|
||||
final existingText = _textController.text.trim();
|
||||
final combinedText = existingText.isEmpty
|
||||
? '$messageUrl '
|
||||
: '$messageUrl\n$existingText';
|
||||
final maxBytes = maxChannelMessageBytes(connector.selfName);
|
||||
var messageText = combinedText;
|
||||
var outboundText = connector.prepareChannelOutboundText(
|
||||
widget.channel.index,
|
||||
messageText,
|
||||
);
|
||||
if (utf8.encode(outboundText).length > maxBytes &&
|
||||
existingText.isNotEmpty) {
|
||||
final originalCharacters = existingText.characters.toList();
|
||||
for (var count = originalCharacters.length - 1; count >= 0; count--) {
|
||||
final shortened = originalCharacters.take(count).join();
|
||||
final candidate = '$messageUrl\n$shortened';
|
||||
final encoded = connector.prepareChannelOutboundText(
|
||||
widget.channel.index,
|
||||
candidate,
|
||||
);
|
||||
if (utf8.encode(encoded).length <= maxBytes) {
|
||||
messageText = candidate;
|
||||
outboundText = encoded;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (utf8.encode(outboundText).length > maxBytes) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(context.l10n.chat_messageTooLong(maxBytes)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
_textController.value = TextEditingValue(
|
||||
text: messageText,
|
||||
selection: TextSelection.collapsed(offset: messageText.length),
|
||||
);
|
||||
_textFieldFocusNode.requestFocus();
|
||||
}
|
||||
|
||||
/// Takes no [BuildContext]: it uses the [State]'s own, so the `mounted`
|
||||
/// checks around each `await` are the ones that actually govern it.
|
||||
Future<void> _showImageSendPreview() async {
|
||||
@@ -2110,6 +2220,20 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
),
|
||||
);
|
||||
}
|
||||
if (settings.uploadIpfsEndpoint.trim().isNotEmpty) {
|
||||
items.add(
|
||||
PopupMenuItem(
|
||||
value: 'ipfs-image',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.cloud_upload_outlined),
|
||||
const SizedBox(width: 12),
|
||||
Text(context.l10n.chat_sendImageIpfs),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final selfConnector = context
|
||||
.read<MeshCoreConnector>();
|
||||
if (hasShareableLocation(
|
||||
@@ -2159,6 +2283,9 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
||||
case 'meshcore-image':
|
||||
_showImageSendPreview();
|
||||
break;
|
||||
case 'ipfs-image':
|
||||
_sendImageViaIpfs();
|
||||
break;
|
||||
case 'send-my-location':
|
||||
await _shareMyLocation(
|
||||
context.read<MeshCoreConnector>(),
|
||||
|
||||
Reference in new issue
Block a user