Skip to content

Commit

Permalink
clean(core): Test ipfsGateway in IPFSResource constructor already (NO…
Browse files Browse the repository at this point in the history
…T lazily later only)
  • Loading branch information
vorburger committed Jan 3, 2025
1 parent f21ca11 commit edef03c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion java/dev/enola/cli/CommandWithResourceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public abstract class CommandWithResourceProvider implements CheckedRunnable {
// Do NOT specify any defaultValue such as "http://localhost:8080/ipfs/" here,
// nor "https://dweb.link/ipfs/" (that's worse, because non-local gateway require trust;
// see https://docs.enola.dev/use/fetch/#ipfs).
required = true,
required = false,
description = "See https://docs.enola.dev/use/fetch/#ipfs")
String ipfsGateway;

Expand Down
16 changes: 7 additions & 9 deletions java/dev/enola/common/io/resource/IPFSResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@
* <a href="https://ipfs.tech/">IPFS</a> Resource. TODO:
*
* <ol>
* <li>Does IPFS include MediaType? Otherwise hard-code it here...
* <li>Support IPLD <=> Thing API bridge...
* <li>Existing? https://github.com/ipld/java-cid and
* https://github.com/ipfs-shipyard/java-ipfs-http-client#dependencies
* com.github.ipfs:java-ipfs-api, com.github.multiformats:java-multiaddr; cid.jar,
* multibase.jar, multihash.jar ?
* <li>Support IPNS, but as ipns:// or as ipfs://ipns/ ? Also consider how that needs different
* caching than ipfs:
* <li>Support CAR files
* <li>Get Thing and add https://cid.ipfs.tech -like technical debugging info?
* <li>Support CAR files?
* <li>Support writing - via a WritableResource, or (probably) a separate API?
* <li>Support ipns://
* <li>Detect IPFS in other links, using https://github.com/ipfs-shipyard/is-ipfs's algorithm
* </ol>
*/
Expand Down Expand Up @@ -65,28 +63,28 @@ private static boolean isIPFS(URI uri) {
return "ipfs".equals(uri.getScheme());
}

private static void checkScheme(URI uri) {
private static void check(URI uri, String gateway) {
if (!isIPFS(uri)) throw new IllegalArgumentException(uri.toString());
if (Strings.isNullOrEmpty(gateway))
throw new IllegalStateException("IPFS HTTP Gateway is required");
}

private final ReadableResource httpResource;

public IPFSResource(URI uri, ResourceProvider httpResourceProvider, String gateway) {
super(uri);
checkScheme(uri);
check(uri, gateway);
this.httpResource = httpResourceProvider.getReadableResource(ipfs2http(uri, gateway));
}

public IPFSResource(
URI uri, MediaType mediaType, ResourceProvider httpResourceProvider, String gateway) {
super(uri, mediaType);
checkScheme(uri);
check(uri, gateway);
this.httpResource = httpResourceProvider.getReadableResource(ipfs2http(uri, gateway));
}

private String ipfs2http(URI ipfsURL, String gateway) {
if (Strings.isNullOrEmpty(gateway))
throw new IllegalStateException("IPFS Gateway is not configured");
return gateway + ipfsURL.getAuthority() + ipfsURL.getPath();
}

Expand Down
4 changes: 2 additions & 2 deletions java/dev/enola/common/io/resource/IPFSResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

public class IPFSResourceTest {

// TODO Document this...
// See https://docs.enola.dev/use/fetch/#ipfs
public static final String IPFS_GATEWAY = "https://dweb.link/ipfs/";

private static final ResourceProvider httpResourceProvider = new OkHttpResource.Provider();
Expand All @@ -59,7 +59,7 @@ public void vanGough() throws IOException {
assertThat(r.charSource().read()).startsWith("<html>");

// TODO mediaType determination only works on (some?) public gateways,
// but not with IPFS Desktop; note that we've hard-coded it above...
// but not with local IPFS Desktop; note that we've hard-coded it above...
// assertThat(r.mediaType()).isEqualTo(MediaType.HTML_UTF_8);
}

Expand Down

0 comments on commit edef03c

Please sign in to comment.