upload url image or file to ipfs

This commit is contained in:
ericz committed 2026-10-11 13:14:14 +02:00
1 parent 9609a8231d
commit a4a7e00732
33 files changed
+2594 -81

No files matched your search

+81 -14
View File
@@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
@@ -48,29 +50,94 @@ void main() {
);
});
test('parses direct ipfs cid urls', () async {
final attachment = await MessageUrlImageHelper.parse(
'my picture: ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly text',
);
test(
'does not resolve ipfs cid links with the default gateway deactivated',
() async {
expect(
await MessageUrlImageHelper.parse(
'my picture: ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly text',
),
isNull,
);
expect(
await MessageUrlImageHelper.parse(
'my picture: ipfs:bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly text',
),
isNull,
);
},
);
expect(attachment, isNotNull);
expect(
attachment,
'https://ipfs.io/ipfs/bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
);
});
test('parses direct ipfs cid urls without double slashes', () async {
test('uses the configured public gateway for direct ipfs cid urls', () async {
final attachment = await MessageUrlImageHelper.parse(
'my picture: ipfs:bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly text',
'ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
publicGateway: 'https://gateway.example.com',
);
expect(
attachment,
'https://ipfs.io/ipfs/bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
'https://gateway.example.com/ipfs/bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
);
});
test(
'does not resolve ipfs cid links when the download gateway is empty',
() async {
final attachment = await MessageUrlImageHelper.parse(
'ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
publicGateway: '',
);
expect(attachment, isNull);
},
);
test('does not upload when the upload endpoint is empty', () async {
var requestCount = 0;
MessageUrlImageHelper.httpClient = MockClient((request) async {
requestCount++;
return http.Response('{}', 200);
});
await expectLater(
MessageUrlImageHelper.uploadImage(
bytes: Uint8List.fromList(<int>[1, 2, 3]),
fileName: 'image.png',
mimeType: 'image/png',
uploadEndpoint: '',
uploadApiKey: '',
),
throwsArgumentError,
);
expect(requestCount, 0);
});
test(
'uploads and returns an ipfs scheme when upload gateway is empty',
() async {
MessageUrlImageHelper.httpClient = MockClient((request) async {
expect(request.method, 'POST');
return http.Response(
'{"Hash":"bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly"}',
200,
);
});
final message = await MessageUrlImageHelper.uploadImage(
bytes: Uint8List.fromList(<int>[1, 2, 3]),
fileName: 'image.png',
mimeType: 'image/png',
uploadEndpoint: 'http://127.0.0.1:5001/api/v0/add',
uploadApiKey: '',
);
expect(
message,
'ipfs://bafybeibq75ws6nwk6wi473dch42wwy6woyj4dwdfy7nfz3buqx2caieoly',
);
},
);
test('parses http image urls', () async {
final attachment = await MessageUrlImageHelper.parse(
'see this image https://example.com/picture.png?size=large additional text',
+17
View File
@@ -443,6 +443,23 @@ void main() {
expect(settings.evictDiscoveredContactsEnabled, isFalse);
});
test('public IPFS gateway defaults to deactivated and preserves empty', () {
expect(AppSettings.fromJson({}).publicIpfsGateway, isEmpty);
expect(
AppSettings.fromJson({'public_ipfs_gateway': ''}).publicIpfsGateway,
isEmpty,
);
});
test('empty upload gateway does not fall back to the download gateway', () {
final settings = AppSettings(
publicIpfsGateway: 'https://gateway.example.com/ipfs/',
uploadIpfsGateway: '',
);
expect(settings.effectiveUploadIpfsGateway, isEmpty);
});
test(
'fromJson with missing new fields uses defaults (backward compat)',
() {
@@ -61,34 +61,30 @@ int _readIntOption(Socket socket, int level, int option) {
}
void main() {
test(
'connect enables TCP keepalive with short probe timings',
() async {
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
final overrides = _CapturingConnectOverrides();
final service = TcpTransportService();
try {
await IOOverrides.runZoned(
() => service.connect(
host: InternetAddress.loopbackIPv4.address,
port: server.port,
),
socketConnect: overrides.socketConnect,
);
final socket = overrides.socket!;
// Linux values: SO_KEEPALIVE 9, TCP_KEEPIDLE 4, TCP_KEEPINTVL 5,
// TCP_KEEPCNT 6.
expect(_readIntOption(socket, RawSocketOption.levelSocket, 9), 1);
expect(_readIntOption(socket, RawSocketOption.levelTcp, 4), 30);
expect(_readIntOption(socket, RawSocketOption.levelTcp, 5), 10);
expect(_readIntOption(socket, RawSocketOption.levelTcp, 6), 3);
} finally {
await service.disconnect();
await server.close();
}
},
skip: Platform.isLinux ? false : 'Option numbers checked are Linux ones',
);
test('connect enables TCP keepalive with short probe timings', () async {
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
final overrides = _CapturingConnectOverrides();
final service = TcpTransportService();
try {
await IOOverrides.runZoned(
() => service.connect(
host: InternetAddress.loopbackIPv4.address,
port: server.port,
),
socketConnect: overrides.socketConnect,
);
final socket = overrides.socket!;
// Linux values: SO_KEEPALIVE 9, TCP_KEEPIDLE 4, TCP_KEEPINTVL 5,
// TCP_KEEPCNT 6.
expect(_readIntOption(socket, RawSocketOption.levelSocket, 9), 1);
expect(_readIntOption(socket, RawSocketOption.levelTcp, 4), 30);
expect(_readIntOption(socket, RawSocketOption.levelTcp, 5), 10);
expect(_readIntOption(socket, RawSocketOption.levelTcp, 6), 3);
} finally {
await service.disconnect();
await server.close();
}
}, skip: Platform.isLinux ? false : 'Option numbers checked are Linux ones');
test('connect/disconnect updates TCP transport state', () async {
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);