From abcf6c2eae57c4058b58076efcd43c1ea2cb3af5 Mon Sep 17 00:00:00 2001 From: zjs81 Date: Sat, 24 Oct 2026 21:52:43 -0700 Subject: [PATCH] Treat an existing Kubo MFS entry as a successful share The MFS name includes the CID, so 'already has entry' means the same content is already there; re-sharing a file no longer fails. --- lib/helpers/ipfs_upload_helper.dart | 4 +- test/helpers/ipfs_upload_helper_test.dart | 48 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/lib/helpers/ipfs_upload_helper.dart b/lib/helpers/ipfs_upload_helper.dart index 42c2dc91..3011fe3a 100644 --- a/lib/helpers/ipfs_upload_helper.dart +++ b/lib/helpers/ipfs_upload_helper.dart @@ -153,7 +153,9 @@ class IpfsUploadHelper { final response = await (client == null ? http.post(copyUri, headers: headers) : client.post(copyUri, headers: headers)); - if (response.statusCode < 200 || response.statusCode >= 300) { + final alreadyCopied = response.body.contains('already has entry'); + if ((response.statusCode < 200 || response.statusCode >= 300) && + !alreadyCopied) { throw Exception( 'Kubo MFS import failed (${response.statusCode}): ${response.body}', ); diff --git a/test/helpers/ipfs_upload_helper_test.dart b/test/helpers/ipfs_upload_helper_test.dart index f49a5d5c..a5987d18 100644 --- a/test/helpers/ipfs_upload_helper_test.dart +++ b/test/helpers/ipfs_upload_helper_test.dart @@ -62,6 +62,54 @@ void main() { }, ); + test( + 're-sharing a file already in Kubo MFS still returns the link', + () async { + final paths = []; + IpfsUploadHelper.httpClient = MockClient((request) async { + paths.add(request.url.path); + if (request.url.path.endsWith('/files/cp')) { + return http.Response( + '{"Message":"cp: cannot put node in path /meshcore_uploads/bafytest-image.png: directory already has entry by that name","Code":0,"Type":"error"}', + 500, + ); + } + return http.Response('{"Hash":"bafytest"}', 200); + }); + + final message = await IpfsUploadHelper.uploadImage( + bytes: Uint8List.fromList([1, 2, 3]), + fileName: 'image.png', + mimeType: 'image/png', + uploadEndpoint: 'http://127.0.0.1:5001/api/v0/add', + uploadApiKey: '', + ); + + expect(message, 'ipfs://bafytest'); + expect(paths, ['/api/v0/add', '/api/v0/files/cp']); + }, + ); + + test('other Kubo MFS errors still fail the upload', () async { + IpfsUploadHelper.httpClient = MockClient((request) async { + if (request.url.path.endsWith('/files/cp')) { + return http.Response('{"Message":"permission denied"}', 500); + } + return http.Response('{"Hash":"bafytest"}', 200); + }); + + await expectLater( + IpfsUploadHelper.uploadImage( + bytes: Uint8List.fromList([1, 2, 3]), + fileName: 'image.png', + mimeType: 'image/png', + uploadEndpoint: 'http://127.0.0.1:5001/api/v0/add', + uploadApiKey: '', + ), + throwsException, + ); + }); + test('sends the Pinata request to the endpoint unchanged', () async { late http.BaseRequest upload; IpfsUploadHelper.httpClient = MockClient((request) async {