-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (cli): Add multibase: resource scheme URL fetch support
- Loading branch information
Showing
10 changed files
with
157 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2024-2025 The Enola <https://enola.dev> Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.enola.common.io.resource; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.io.ByteSource; | ||
import com.google.common.net.MediaType; | ||
|
||
import dev.enola.common.io.iri.URIs; | ||
import dev.enola.common.io.mediatype.MediaTypes; | ||
|
||
import io.ipfs.multibase.Multibase; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
/** | ||
* Resource I/O implementation based on a <a | ||
* href="https://github.com/multiformats/multibase">multibase: URLs</a> (invented by Enola.dev; not | ||
* standardized anywhere, yet). | ||
* | ||
* <p>@see {@link DataResource} for another similar URL scheme. | ||
*/ | ||
public class MultibaseResource extends BaseResource implements ReadableButNotWritableResource { | ||
|
||
private static final String SCHEME = "multibase"; | ||
|
||
public static class Provider implements ResourceProvider { | ||
@Override | ||
public Resource getResource(URI uri) { | ||
if (SCHEME.equals(uri.getScheme())) return new MultibaseResource(uri); | ||
else return null; | ||
} | ||
} | ||
|
||
private final ByteSource byteSource; | ||
|
||
public static Resource of(@Nullable String text, @Nullable MediaType mediaType) { | ||
var data = MediaTypes.toStringWithoutSpaces(mediaType) + "," + Strings.nullToEmpty(text); | ||
try { | ||
return new MultibaseResource(new URI(SCHEME, data, null)); | ||
} catch (URISyntaxException e) { | ||
throw new IllegalArgumentException(data, e); | ||
} | ||
} | ||
|
||
public static Resource of(@Nullable String text) { | ||
return of(text, null); | ||
} | ||
|
||
// TODO Rewrite using https://blog.jetbrains.com/idea/2024/02/constructor-makeover-in-java-22/ | ||
// once we're on Java >21 (and avoid having to match the RegExp twice, which is inefficient) | ||
private static URI checkSchema(URI uri) { | ||
if (!SCHEME.equals(uri.getScheme())) throw new IllegalArgumentException(uri.toString()); | ||
return uri; | ||
} | ||
|
||
public MultibaseResource(URI uri) { | ||
super(checkSchema(uri)); | ||
String data = URIs.dropQueryAndFragment(uri).getSchemeSpecificPart(); | ||
byteSource = ByteSource.wrap(Multibase.decode(data)); | ||
} | ||
|
||
@Override | ||
public ByteSource byteSource() { | ||
return byteSource; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
java/dev/enola/common/io/resource/MultibaseResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2024-2025 The Enola <https://enola.dev> Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.enola.common.io.resource; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
public class MultibaseResourceTest { | ||
|
||
@Test | ||
public void hex() throws IOException { | ||
assertThat(new MultibaseResource(URI.create("multibase:f0a3f")).byteSource().read()) | ||
.isEqualTo(new byte[] {0x0a, 0x3f}); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void spaceIsInvalid() { | ||
new DataResource(URI.create("multibase:f0 a3f")); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void empty() { | ||
new MultibaseResource(URI.create("")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters