use super::{FetchError, FetchRequest, Fetched, Fetcher, HttpFetcher, ProgressFn};
use crate::types::Address;
use async_trait::async_trait;
use std::sync::atomic::{AtomicU64, Ordering};
pub const DEFAULT_IPFS_GATEWAY: &str = "https://ipfs.io";
const IPFS_SCHEME: &str = "ipfs";
pub struct IpfsGatewayFetcher {
gateways: Vec<String>,
http: HttpFetcher,
schemes: Vec<String>,
}
impl IpfsGatewayFetcher {
pub fn new(gateway: impl Into<String>) -> Self {
Self::with_gateways([gateway.into()])
}
pub fn with_gateways<I, S>(gateways: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let gateways = gateways
.into_iter()
.map(Into::into)
.map(|gateway: String| gateway.trim_end_matches('/').to_owned())
.collect();
Self {
gateways,
http: HttpFetcher::new(),
schemes: vec![IPFS_SCHEME.to_owned()],
}
}
pub fn with_default_gateway() -> Self {
Self::new(DEFAULT_IPFS_GATEWAY)
}
pub fn with_http_fetcher(mut self, http: HttpFetcher) -> Self {
self.http = http;
self
}
pub fn gateway(&self) -> &str {
self.gateways.first().map(String::as_str).unwrap_or("")
}
pub fn gateways(&self) -> &[String] {
&self.gateways
}
pub fn resolve(&self, uri: &str) -> Result<String, FetchError> {
Self::resolve_with(self.gateway(), uri)
}
fn resolve_with(gateway: &str, uri: &str) -> Result<String, FetchError> {
let rest = uri
.strip_prefix("ipfs://")
.ok_or_else(|| FetchError::InvalidAddress(format!("not an ipfs URI: `{uri}`")))?;
let rest = rest.trim_start_matches('/');
if rest.is_empty() {
return Err(FetchError::InvalidAddress(format!(
"ipfs URI has no content identifier: `{uri}`"
)));
}
Ok(format!("{gateway}/ipfs/{rest}"))
}
fn rewrite(gateway: &str, request: &FetchRequest) -> Result<FetchRequest, FetchError> {
let resolved = Self::resolve_with(gateway, &request.address.uri)?;
let scheme = if resolved.starts_with("http://") {
"http"
} else {
"https"
};
Ok(FetchRequest {
address: Address::new(scheme, resolved),
destination: request.destination.clone(),
expected_sha256: request.expected_sha256.clone(),
})
}
}
#[async_trait]
impl Fetcher for IpfsGatewayFetcher {
fn name(&self) -> &str {
"ipfs-gateway"
}
fn schemes(&self) -> &[String] {
&self.schemes
}
async fn fetch(&self, request: &FetchRequest) -> Result<Fetched, FetchError> {
self.fetch_with_progress(request, &|_: u64| {}).await
}
async fn fetch_with_progress(
&self,
request: &FetchRequest,
progress: &ProgressFn<'_>,
) -> Result<Fetched, FetchError> {
if self.gateways.is_empty() {
return Err(FetchError::InvalidAddress(
"no ipfs gateway configured".to_owned(),
));
}
let high_water = AtomicU64::new(0);
let mut errors = Vec::new();
for gateway in &self.gateways {
let rewritten = Self::rewrite(gateway, request)?;
let monotonic = |bytes: u64| {
let previous = high_water.fetch_max(bytes, Ordering::Relaxed);
if bytes > previous {
progress(bytes);
}
};
match self.http.fetch_with_progress(&rewritten, &monotonic).await {
Ok(fetched) => return Ok(fetched),
Err(error) => errors.push(error),
}
}
let count = errors.len();
Err(FetchError::AllFailed { count, errors })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolves_cid_without_path() {
let fetcher = IpfsGatewayFetcher::new("https://ipfs.io");
assert_eq!(
fetcher.resolve("ipfs://bafyabc").expect("resolve"),
"https://ipfs.io/ipfs/bafyabc"
);
}
#[test]
fn resolves_cid_with_path_and_query() {
let fetcher = IpfsGatewayFetcher::new("https://ipfs.io");
assert_eq!(
fetcher
.resolve("ipfs://bafyabc/docs/report.pdf?filename=report.pdf")
.expect("resolve"),
"https://ipfs.io/ipfs/bafyabc/docs/report.pdf?filename=report.pdf"
);
}
#[test]
fn tolerates_trailing_gateway_slash_and_leading_cid_slash() {
let fetcher = IpfsGatewayFetcher::new("https://ipfs.io/");
assert_eq!(
fetcher.resolve("ipfs:///bafyabc").expect("resolve"),
"https://ipfs.io/ipfs/bafyabc"
);
assert_eq!(fetcher.gateway(), "https://ipfs.io");
}
#[test]
fn keeps_gateways_in_preference_order() {
let fetcher =
IpfsGatewayFetcher::with_gateways(["http://127.0.0.1:8080/", "https://ipfs.io"]);
assert_eq!(
fetcher.gateways(),
["http://127.0.0.1:8080", "https://ipfs.io"]
);
assert_eq!(fetcher.gateway(), "http://127.0.0.1:8080");
}
#[test]
fn rejects_wrong_scheme_and_empty_cid() {
let fetcher = IpfsGatewayFetcher::new("https://ipfs.io");
assert!(matches!(
fetcher.resolve("https://example.org/file"),
Err(FetchError::InvalidAddress(_))
));
assert!(matches!(
fetcher.resolve("ipfs://"),
Err(FetchError::InvalidAddress(_))
));
assert!(matches!(
fetcher.resolve("ipfs:///"),
Err(FetchError::InvalidAddress(_))
));
}
}