Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/gofer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ std = [
"percent-encoding?/std",
"thiserror/std",
]
all = ["data", "file", "ftp", "http", "https", "stdin"]
all = ["data", "file", "ftp", "http", "https", "ipfs", "stdin"]
unstable = ["ftps", "git", "scp"]

# Protocols:
Expand All @@ -34,6 +34,7 @@ ftps = ["ftp", "suppaftp?/rustls"]
git = ["dep:gix-protocol"]
http = ["dep:reqwest", "reqwest?/blocking"]
https = ["http", "reqwest?/default", "reqwest?/rustls-tls-native-roots"]
ipfs = ["dep:reqwest", "reqwest?/blocking"]
scp = ["dep:ssh2"]
stdin = ["std"]

Expand Down
12 changes: 12 additions & 0 deletions lib/gofer/examples/ipfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This is free and unencumbered software released into the public domain.

use std::{boxed::Box, error::Error, io::stdout, result::Result};

pub fn main() -> Result<(), Box<dyn Error>> {
let mut output = stdout().lock();
let mut input =
gofer::open("ipfs://bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3m")?;
std::io::copy(&mut input, &mut output)?;

Ok(())
}
21 changes: 18 additions & 3 deletions lib/gofer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,25 @@ pub enum Error {
)
)]
FailedHttpRequest(#[from] reqwest::Error),

#[cfg(feature = "ipfs")]
#[error("invalid IPFS URL: {0}")]
#[cfg_attr(
feature = "miette",
diagnostic(
code(gofer::invalid_ipfs_url),
help("it seems that the URL is malformed in some way"),
url(docsrs),
)
)]
InvalidIpfsUrl(String),
}

#[cfg(feature = "std")]
impl Into<std::io::Error> for Error {
fn into(self) -> std::io::Error {
impl From<Error> for std::io::Error {
fn from(value: Error) -> Self {
use std::io::ErrorKind;
match self {
match value {
Error::InvalidUrl(e) => std::io::Error::new(ErrorKind::InvalidInput, e),
Error::UnknownScheme(s) => std::io::Error::new(ErrorKind::InvalidInput, s),

Expand All @@ -142,6 +154,9 @@ impl Into<std::io::Error> for Error {

#[cfg(any(feature = "http", feature = "https"))]
Error::FailedHttpRequest(_e) => std::io::Error::from(ErrorKind::Other), // FIXME

#[cfg(feature = "ipfs")]
Error::InvalidIpfsUrl(u) => std::io::Error::new(ErrorKind::InvalidInput, u.as_str()),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/gofer/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub fn open(url: impl AsRef<str>) -> Result<Box<dyn Read>> {
#[cfg(feature = "https")]
UrlScheme::Https => crate::schemes::http::open(&url, true),

#[cfg(feature = "ipfs")]
UrlScheme::Ipfs => crate::schemes::ipfs::open(&url),

#[cfg(feature = "scp")]
UrlScheme::Scp => crate::schemes::scp::open(&url),

Expand Down
3 changes: 3 additions & 0 deletions lib/gofer/src/schemes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub mod git;
#[cfg(any(feature = "http", feature = "https"))]
pub mod http;

#[cfg(feature = "ipfs")]
pub mod ipfs;

#[cfg(feature = "scp")]
pub mod scp;

Expand Down
28 changes: 28 additions & 0 deletions lib/gofer/src/schemes/ipfs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This is free and unencumbered software released into the public domain.

use crate::{Error, Read, Result, Url};
use reqwest::{blocking::ClientBuilder, redirect};

static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
static GATEWAY: &str = "https://ipfs.io";

/// See: https://en.wikipedia.org/wiki/InterPlanetary_File_System
pub fn open<'a, 'b>(url: &'a Url<'b>) -> Result<Box<dyn Read>> {
// See: https://docs.rs/reqwest/latest/reqwest/blocking/struct.ClientBuilder.html
let client = ClientBuilder::new()
.user_agent(USER_AGENT)
.redirect(redirect::Policy::default())
.https_only(true);

let url = url
.as_str()
.strip_prefix("ipfs://")
.ok_or_else(|| Error::InvalidIpfsUrl(url.to_string()))
.map(|id| format!("{}/ipfs/{}", GATEWAY, id))?;

// See: https://docs.rs/reqwest/latest/reqwest/blocking/struct.Client.html#method.get
// See: https://docs.rs/reqwest/latest/reqwest/blocking/struct.RequestBuilder.html
let response = client.build()?.get(url).send()?;

Ok(Box::new(response))
}
Loading