Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
Boilerplate until the let_chains feature is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptex-github committed Dec 21, 2021
1 parent 7a82f6e commit 31f32bf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
68 changes: 40 additions & 28 deletions cdn_server/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,54 @@ use crate::node::get_node_ip;
pub async fn download(
Path((node, filename)): Path<(String, String)>,
) -> Result<Response<BoxBody>, CdnError> {
let mut content_type: String;

let mut decoded: Vec<u8>;

let mut status: StatusCode;
if *CACHE {
if let Some(file_) = get_from_cache(&filename) {
let content_type = tree_magic::from_u8(&file_);
let decoded = file_;

let resp = Response::builder()
.status(StatusCode::FOUND)
.header(
CONTENT_TYPE,
HeaderValue::from_str(content_type.as_str())
.unwrap_or(HeaderValue::from_static("application/octet-stream")),
)
.body(body::boxed(body::Full::from(decoded)))
.unwrap_or_else(|e| {
// this should only be reachable if a invalid HTTP code is passed in
unreachable!(
"got an error while attempting to construct HTTP response for ServerError: {}",
e
)
});

return Ok(resp);
}
}

if *CACHE && let Some(file_) = get_from_cache(&filename) {
content_type = tree_magic::from_u8(&file_);
decoded = file_;
status = StatusCode::FOUND;
} else {
let node_ip = get_node_ip(node).await?;
let node_ip = get_node_ip(node).await?;

let file = get_file(node_ip, filename).await?;
let file = get_file(node_ip, filename.clone()).await?;

let mut decoder = ZstdDecoder::new(Vec::new());
decoder
.write_all(&file)
.await
.map_err(|e| CdnError::FailedToDeCompress(e))?;
decoder
.shutdown()
.await
.map_err(|e| CdnError::FailedToDeCompress(e))?;
let mut decoder = ZstdDecoder::new(Vec::new());
decoder
.write_all(&file)
.await
.map_err(|e| CdnError::FailedToDeCompress(e))?;
decoder
.shutdown()
.await
.map_err(|e| CdnError::FailedToDeCompress(e))?;

decoded = decoder.into_inner();
content_type = tree_magic::from_u8(&decoded);
status = StatusCode::OK;
let decoded = decoder.into_inner();
let content_type = tree_magic::from_u8(&decoded);

if *CACHE {
insert_into_cache(filename, decoded.clone(), decoded.len() as i64).await;
}
if *CACHE {
insert_into_cache(filename, decoded.clone(), decoded.len() as i64).await;
}

let resp = Response::builder()
.status(status)
.status(StatusCode::OK)
.header(
CONTENT_TYPE,
HeaderValue::from_str(content_type.as_str())
Expand Down
1 change: 0 additions & 1 deletion cdn_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![feature(async_closure)]
#![feature(once_cell)]
#![feature(let_chains)]

#[macro_use]
extern crate lazy_static;
Expand Down

0 comments on commit 31f32bf

Please sign in to comment.