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
@@ -24,11 +24,64 @@ void main() {
|
||||
uploadEndpoint: '',
|
||||
uploadApiKey: '',
|
||||
),
|
||||
throwsArgumentError,
|
||||
throwsException,
|
||||
);
|
||||
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(
|
||||
'uploads and returns an ipfs scheme when upload gateway is empty',
|
||||
() async {
|
||||
|
||||
Reference in new issue
Block a user