Test IpfsUploadHelper directly and build the upload request once
Drop the test-only upload facade on MessageUrlImageHelper that overwrote the shared HTTP client, move its tests to IpfsUploadHelper, and build the multipart request with the final upload URI instead of cloning it.
This commit is contained in:
1 parent
7e7e7a48db
commit
c2d18b431e
4 files changed
+109
-107
No files matched your search
@@ -6,6 +6,7 @@ import 'package:http/http.dart' as http;
|
||||
import '../models/app_settings.dart';
|
||||
|
||||
class IpfsUploadHelper {
|
||||
@visibleForTesting
|
||||
static http.Client? httpClient;
|
||||
|
||||
static String mimeTypeForFileName(String fileName) {
|
||||
@@ -69,40 +70,37 @@ class IpfsUploadHelper {
|
||||
}
|
||||
|
||||
final Uri uploadUri;
|
||||
final request = http.MultipartRequest('POST', endpointUri)
|
||||
..files.add(
|
||||
http.MultipartFile.fromBytes('file', bytes, filename: safeFileName),
|
||||
);
|
||||
|
||||
if (isPinata) {
|
||||
if (token.isEmpty) {
|
||||
throw ArgumentError('Pinata requires an API key or JWT.');
|
||||
}
|
||||
request.headers['Authorization'] = 'Bearer $token';
|
||||
request.fields['pinataOptions'] = jsonEncode({'cidVersion': 1});
|
||||
request.fields['pinataMetadata'] = jsonEncode({'name': safeFileName});
|
||||
uploadUri = endpointUri;
|
||||
} else {
|
||||
final parameters = <String, String>{'cid-version': '1'};
|
||||
if (isKubo) parameters['pin'] = 'true';
|
||||
uploadUri = endpointUri.replace(
|
||||
queryParameters: {...endpointUri.queryParameters, ...parameters},
|
||||
queryParameters: {
|
||||
...endpointUri.queryParameters,
|
||||
'cid-version': '1',
|
||||
if (isKubo) 'pin': 'true',
|
||||
},
|
||||
);
|
||||
if (token.isNotEmpty) {
|
||||
request.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
}
|
||||
|
||||
final finalRequest = request.url == uploadUri
|
||||
? request
|
||||
: (http.MultipartRequest('POST', uploadUri)
|
||||
..headers.addAll(request.headers)
|
||||
..fields.addAll(request.fields)
|
||||
..files.addAll(request.files));
|
||||
final request = http.MultipartRequest('POST', uploadUri)
|
||||
..files.add(
|
||||
http.MultipartFile.fromBytes('file', bytes, filename: safeFileName),
|
||||
);
|
||||
if (token.isNotEmpty) {
|
||||
request.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
if (isPinata) {
|
||||
request.fields['pinataOptions'] = jsonEncode({'cidVersion': 1});
|
||||
request.fields['pinataMetadata'] = jsonEncode({'name': safeFileName});
|
||||
}
|
||||
|
||||
final client = httpClient;
|
||||
final response = await (client == null
|
||||
? finalRequest.send()
|
||||
: client.send(finalRequest));
|
||||
? request.send()
|
||||
: client.send(request));
|
||||
final body = await response.stream.bytesToString();
|
||||
if (response.statusCode < 200 || response.statusCode >= 300) {
|
||||
throw Exception('IPFS upload failed (${response.statusCode}): $body');
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'ipfs_upload_helper.dart';
|
||||
import '../models/app_settings.dart';
|
||||
|
||||
/// Finds and renders image URLs embedded in ordinary text messages.
|
||||
@@ -26,41 +24,6 @@ class MessageUrlImageHelper {
|
||||
@visibleForTesting
|
||||
static http.Client? httpClient;
|
||||
|
||||
/// Compatibility facade for the dedicated IPFS upload helper.
|
||||
static Future<String> uploadImageWithSettings({
|
||||
required AppSettings settings,
|
||||
required Uint8List bytes,
|
||||
required String fileName,
|
||||
required String mimeType,
|
||||
}) {
|
||||
IpfsUploadHelper.httpClient = httpClient;
|
||||
return IpfsUploadHelper.uploadImageWithSettings(
|
||||
settings: settings,
|
||||
bytes: bytes,
|
||||
fileName: fileName,
|
||||
mimeType: mimeType,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<String> uploadImage({
|
||||
required Uint8List bytes,
|
||||
required String fileName,
|
||||
required String mimeType,
|
||||
required String uploadEndpoint,
|
||||
required String uploadApiKey,
|
||||
String uploadGateway = '',
|
||||
}) async {
|
||||
IpfsUploadHelper.httpClient = httpClient;
|
||||
return IpfsUploadHelper.uploadImage(
|
||||
bytes: bytes,
|
||||
fileName: fileName,
|
||||
mimeType: mimeType,
|
||||
uploadEndpoint: uploadEndpoint,
|
||||
uploadApiKey: uploadApiKey,
|
||||
uploadGateway: uploadGateway,
|
||||
);
|
||||
}
|
||||
|
||||
static final RegExp _pyxPattern = RegExp(
|
||||
r'''https?://pyx\.li/\?i=([A-Za-z0-9_-]+)''',
|
||||
caseSensitive: false,
|
||||
|
||||
Reference in new issue
Block a user