Validate the IPFS upload endpoint before uploading
Reject an empty or non-http(s) endpoint, missing Pinata/Filebase tokens, and a custom (Kubo) endpoint that doesn't end in /api/v0/add before any data is sent, and derive the files/cp URL from the add URL so Kubo behind a proxy path works.
This commit is contained in:
1 parent
abcf6c2eae
commit
3ed88a0fc0
2 files changed
+86
-32
No files matched your search
@@ -9,6 +9,8 @@ class IpfsUploadHelper {
|
|||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
static http.Client? httpClient;
|
static http.Client? httpClient;
|
||||||
|
|
||||||
|
static final _kuboAddPath = RegExp(r'/api/v0/add/?$');
|
||||||
|
|
||||||
static String mimeTypeForFileName(String fileName) {
|
static String mimeTypeForFileName(String fileName) {
|
||||||
switch (fileName.toLowerCase().split('.').last) {
|
switch (fileName.toLowerCase().split('.').last) {
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
@@ -53,11 +55,17 @@ class IpfsUploadHelper {
|
|||||||
}) async {
|
}) async {
|
||||||
final endpoint = uploadEndpoint.trim();
|
final endpoint = uploadEndpoint.trim();
|
||||||
if (endpoint.isEmpty) {
|
if (endpoint.isEmpty) {
|
||||||
throw ArgumentError('Configure an IPFS upload endpoint first.');
|
throw Exception('Configure an IPFS upload endpoint first.');
|
||||||
|
}
|
||||||
|
|
||||||
|
final endpointUri = Uri.tryParse(endpoint);
|
||||||
|
if (endpointUri == null ||
|
||||||
|
!(endpointUri.isScheme('http') || endpointUri.isScheme('https')) ||
|
||||||
|
endpointUri.host.isEmpty) {
|
||||||
|
throw Exception('The IPFS upload endpoint must be an http(s) URL.');
|
||||||
}
|
}
|
||||||
|
|
||||||
final token = uploadApiKey.trim();
|
final token = uploadApiKey.trim();
|
||||||
final endpointUri = Uri.parse(endpoint);
|
|
||||||
final endpointHost = endpointUri.host.toLowerCase();
|
final endpointHost = endpointUri.host.toLowerCase();
|
||||||
final isPinata =
|
final isPinata =
|
||||||
endpointHost.contains('pinata') ||
|
endpointHost.contains('pinata') ||
|
||||||
@@ -65,26 +73,28 @@ class IpfsUploadHelper {
|
|||||||
final isFilebase = endpointHost.contains('filebase');
|
final isFilebase = endpointHost.contains('filebase');
|
||||||
final isKubo = !isPinata && !isFilebase;
|
final isKubo = !isPinata && !isFilebase;
|
||||||
final safeFileName = _sanitizeUploadFileName(fileName);
|
final safeFileName = _sanitizeUploadFileName(fileName);
|
||||||
if (isFilebase && token.isEmpty) {
|
if (isPinata && token.isEmpty) {
|
||||||
throw ArgumentError('Filebase RPC requires an API token.');
|
throw Exception('Pinata requires an API key or JWT.');
|
||||||
}
|
}
|
||||||
|
if (isFilebase && token.isEmpty) {
|
||||||
final Uri uploadUri;
|
throw Exception('Filebase RPC requires an API token.');
|
||||||
if (isPinata) {
|
}
|
||||||
if (token.isEmpty) {
|
if (isKubo && !_kuboAddPath.hasMatch(endpointUri.path)) {
|
||||||
throw ArgumentError('Pinata requires an API key or JWT.');
|
throw Exception(
|
||||||
}
|
'Custom (Kubo) upload endpoint must end with /api/v0/add.',
|
||||||
uploadUri = endpointUri;
|
|
||||||
} else {
|
|
||||||
uploadUri = endpointUri.replace(
|
|
||||||
queryParameters: {
|
|
||||||
...endpointUri.queryParameters,
|
|
||||||
'cid-version': '1',
|
|
||||||
if (isKubo) 'pin': 'true',
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final uploadUri = isPinata
|
||||||
|
? endpointUri
|
||||||
|
: endpointUri.replace(
|
||||||
|
queryParameters: {
|
||||||
|
...endpointUri.queryParameters,
|
||||||
|
'cid-version': '1',
|
||||||
|
if (isKubo) 'pin': 'true',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
final request = http.MultipartRequest('POST', uploadUri)
|
final request = http.MultipartRequest('POST', uploadUri)
|
||||||
..files.add(
|
..files.add(
|
||||||
http.MultipartFile.fromBytes('file', bytes, filename: safeFileName),
|
http.MultipartFile.fromBytes('file', bytes, filename: safeFileName),
|
||||||
@@ -113,7 +123,7 @@ class IpfsUploadHelper {
|
|||||||
|
|
||||||
if (isKubo) {
|
if (isKubo) {
|
||||||
await _copyKuboFileToMfs(
|
await _copyKuboFileToMfs(
|
||||||
endpoint: endpoint,
|
addUri: endpointUri,
|
||||||
cid: cid,
|
cid: cid,
|
||||||
fileName: safeFileName,
|
fileName: safeFileName,
|
||||||
authorization: token.isEmpty ? null : 'Bearer $token',
|
authorization: token.isEmpty ? null : 'Bearer $token',
|
||||||
@@ -125,23 +135,14 @@ class IpfsUploadHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static Future<void> _copyKuboFileToMfs({
|
static Future<void> _copyKuboFileToMfs({
|
||||||
required String endpoint,
|
required Uri addUri,
|
||||||
required String cid,
|
required String cid,
|
||||||
required String fileName,
|
required String fileName,
|
||||||
required String? authorization,
|
required String? authorization,
|
||||||
}) async {
|
}) async {
|
||||||
final uploadUri = Uri.parse(endpoint);
|
|
||||||
final copyPath = uploadUri.path.replaceFirst(
|
|
||||||
RegExp(r'/add/?$'),
|
|
||||||
'/files/cp',
|
|
||||||
);
|
|
||||||
if (copyPath == uploadUri.path) {
|
|
||||||
throw ArgumentError('Kubo upload endpoint must end with /add.');
|
|
||||||
}
|
|
||||||
|
|
||||||
final mfsPath = '/meshcore_uploads/$cid-$fileName';
|
final mfsPath = '/meshcore_uploads/$cid-$fileName';
|
||||||
final copyUri = uploadUri.replace(
|
final copyUri = addUri.replace(
|
||||||
path: copyPath,
|
path: addUri.path.replaceFirst(_kuboAddPath, '/api/v0/files/cp'),
|
||||||
query:
|
query:
|
||||||
'arg=${Uri.encodeQueryComponent('/ipfs/$cid')}'
|
'arg=${Uri.encodeQueryComponent('/ipfs/$cid')}'
|
||||||
'&arg=${Uri.encodeQueryComponent(mfsPath)}&parents=true',
|
'&arg=${Uri.encodeQueryComponent(mfsPath)}&parents=true',
|
||||||
|
|||||||
@@ -24,11 +24,64 @@ void main() {
|
|||||||
uploadEndpoint: '',
|
uploadEndpoint: '',
|
||||||
uploadApiKey: '',
|
uploadApiKey: '',
|
||||||
),
|
),
|
||||||
throwsArgumentError,
|
throwsException,
|
||||||
);
|
);
|
||||||
expect(requestCount, 0);
|
expect(requestCount, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (final endpoint in [
|
||||||
|
'not a url',
|
||||||
|
'ftp://host/api/v0/add',
|
||||||
|
'https://host/ipfs-upload',
|
||||||
|
'https://uploads.example.com/v1/files',
|
||||||
|
]) {
|
||||||
|
test('rejects $endpoint before uploading', () async {
|
||||||
|
var requestCount = 0;
|
||||||
|
IpfsUploadHelper.httpClient = MockClient((request) async {
|
||||||
|
requestCount++;
|
||||||
|
return http.Response('{"Hash":"bafytest"}', 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
await expectLater(
|
||||||
|
IpfsUploadHelper.uploadImage(
|
||||||
|
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
||||||
|
fileName: 'image.png',
|
||||||
|
mimeType: 'image/png',
|
||||||
|
uploadEndpoint: endpoint,
|
||||||
|
uploadApiKey: 'token',
|
||||||
|
),
|
||||||
|
throwsA(isA<Exception>()),
|
||||||
|
);
|
||||||
|
expect(requestCount, 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
test('copies to MFS through a proxied Kubo endpoint', () async {
|
||||||
|
final urls = <Uri>[];
|
||||||
|
IpfsUploadHelper.httpClient = MockClient((request) async {
|
||||||
|
urls.add(request.url);
|
||||||
|
return http.Response('{"Hash":"bafytest"}', 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
final message = await IpfsUploadHelper.uploadImage(
|
||||||
|
bytes: Uint8List.fromList(<int>[1, 2, 3]),
|
||||||
|
fileName: 'image.png',
|
||||||
|
mimeType: 'image/png',
|
||||||
|
uploadEndpoint: 'https://host/ipfs-upload/api/v0/add',
|
||||||
|
uploadApiKey: '',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(message, 'ipfs://bafytest');
|
||||||
|
expect(urls.map((url) => url.path), [
|
||||||
|
'/ipfs-upload/api/v0/add',
|
||||||
|
'/ipfs-upload/api/v0/files/cp',
|
||||||
|
]);
|
||||||
|
expect(urls.last.queryParametersAll['arg'], [
|
||||||
|
'/ipfs/bafytest',
|
||||||
|
'/meshcore_uploads/bafytest-image.png',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
test(
|
test(
|
||||||
'uploads and returns an ipfs scheme when upload gateway is empty',
|
'uploads and returns an ipfs scheme when upload gateway is empty',
|
||||||
() async {
|
() async {
|
||||||
|
|||||||
Reference in new issue
Block a user