Skip to content

Commit

Permalink
Merge branch 'main' into b5/gateway_templates
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 authored Oct 13, 2022
2 parents 1ba87b1 + ff4ef02 commit 5d7d67a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
/iroh_gateway/test_files/*
.env
Cargo.lock
foo
8 changes: 7 additions & 1 deletion iroh-gateway/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use iroh_metrics::{
observe, record,
};
use iroh_resolver::resolver::{
CidOrDomain, ContentLoader, Metadata, Out, OutMetrics, OutPrettyReader, Resolver, Source,
CidOrDomain, ContentLoader, Metadata, Out, OutMetrics, OutPrettyReader, OutType, Resolver,
Source,
};
use tokio::io::{AsyncReadExt, AsyncWrite};
use tokio_util::io::ReaderStream;
Expand All @@ -32,6 +33,7 @@ pub struct PrettyStreamBody<T: ContentLoader>(ReaderStream<OutPrettyReader<T>>,
pub enum FileResult<T: ContentLoader> {
File(PrettyStreamBody<T>),
Directory(Out),
Raw(PrettyStreamBody<T>),
}

impl<T: ContentLoader + std::marker::Unpin> http_body::Body for PrettyStreamBody<T> {
Expand Down Expand Up @@ -100,6 +102,10 @@ impl<T: ContentLoader + std::marker::Unpin> Client<T> {
let stream = ReaderStream::new(reader);
let body = PrettyStreamBody(stream, metadata.size);

if metadata.typ == OutType::Raw {
return Ok((FileResult::Raw(body), metadata));
}

Ok((FileResult::File(body), metadata))
}
}
Expand Down
19 changes: 17 additions & 2 deletions iroh-gateway/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ async fn serve_raw<T: ContentLoader + std::marker::Unpin>(
.map_err(|e| error(StatusCode::INTERNAL_SERVER_ERROR, &e, &state))?;

match body {
FileResult::File(body) => {
FileResult::File(body) | FileResult::Raw(body) => {
set_content_disposition_headers(
&mut headers,
format!("{}.bin", req.cid).as_str(),
Expand Down Expand Up @@ -399,7 +399,7 @@ async fn serve_car<T: ContentLoader + std::marker::Unpin>(
.map_err(|e| error(StatusCode::INTERNAL_SERVER_ERROR, &e, &state))?;

match body {
FileResult::File(body) => {
FileResult::File(body) | FileResult::Raw(body) => {
set_content_disposition_headers(
&mut headers,
format!("{}.car", req.cid).as_str(),
Expand Down Expand Up @@ -515,6 +515,21 @@ async fn serve_fs<T: ContentLoader + std::marker::Unpin>(
)),
}
}
FileResult::Raw(body) => {
// todo(arqu): error on no size
// todo(arqu): add lazy seeking
add_cache_control_headers(&mut headers, metadata.clone());
set_etag_headers(&mut headers, get_etag(&req.cid, Some(req.format.clone())));
add_ipfs_roots_headers(&mut headers, metadata);
let name = add_content_disposition_headers(
&mut headers,
&req.query_file_name,
&req.content_path,
req.download,
);
add_content_type_headers(&mut headers, &name);
response(StatusCode::OK, body, headers)
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions iroh-resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ pub enum OutType {

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum UnixfsType {
Raw,
Dir,
File,
Symlink,
Expand Down Expand Up @@ -963,7 +962,7 @@ impl<T: ContentLoader> Resolver<T> {
path: root_path,
size: Some(loaded_cid.data.len() as u64),
typ: OutType::Raw,
unixfs_type: Some(UnixfsType::Raw),
unixfs_type: None,
resolved_path: vec![cid],
source: loaded_cid.source,
};
Expand Down

0 comments on commit 5d7d67a

Please sign in to comment.