Skip to content

Commit

Permalink
add response property
Browse files Browse the repository at this point in the history
  • Loading branch information
vnghia committed Oct 23, 2024
1 parent 3d796fa commit ae61c33
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion nghe-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(adt_const_params)]
#![feature(const_option)]

pub mod auth;
pub mod common;
Expand Down
4 changes: 0 additions & 4 deletions nghe-backend/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ impl<F: Mime> Property<F> {
pub fn signed_size(&self) -> i32 {
self.size as _
}

pub fn mime(&self) -> &'static str {
self.format.mime()
}
}

impl<F: Mime> File<F> {
Expand Down
28 changes: 23 additions & 5 deletions nghe-backend/src/response/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use axum_extra::body::AsyncReadBody;
use axum_extra::headers::{
AcceptRanges, CacheControl, ContentLength, ContentRange, ETag, HeaderMapExt,
};
use o2o::o2o;
use tokio::io::{AsyncRead, AsyncSeekExt, SeekFrom};
use typed_path::Utf8TypedPath;

Expand All @@ -17,6 +18,22 @@ pub struct Binary {
body: AsyncReadBody,
}

#[derive(o2o)]
#[from_owned(file::Property<F>)]
#[where_clause(F: file::Mime)]
pub struct Property<F> {
#[from(~.into())]
pub hash: Option<u64>,
pub size: u32,
pub format: F,
}

impl<F: file::Mime> Property<F> {
pub fn mime(&self) -> &'static str {
self.format.mime()
}
}

impl Binary {
const MAX_AGE: Duration = Duration::from_secs(31_536_000);

Expand All @@ -30,7 +47,7 @@ impl Binary {

pub async fn from_local<F: file::Mime>(
path: Utf8TypedPath<'_>,
property: file::Property<F>,
property: Property<F>,
offset: impl Into<Option<u64>> + Copy,
seekable: bool,
cacheable: bool,
Expand All @@ -46,7 +63,7 @@ impl Binary {

pub fn from_async_read<F: file::Mime>(
reader: impl AsyncRead + Send + 'static,
property: file::Property<F>,
property: Property<F>,
offset: impl Into<Option<u64>>,
seekable: bool,
cacheable: bool,
Expand All @@ -57,9 +74,10 @@ impl Binary {

let size = property.size.into();
header.typed_insert(ContentLength(size));
header.typed_insert(
property.hash.to_string().parse::<ETag>().map_err(color_eyre::Report::from)?,
);
if let Some(hash) = property.hash {
header
.typed_insert(hash.to_string().parse::<ETag>().map_err(color_eyre::Report::from)?);
}

let offset = offset.into().unwrap_or(0);
header.typed_insert(ContentRange::bytes(offset.., size).map_err(color_eyre::Report::from)?);
Expand Down

0 comments on commit ae61c33

Please sign in to comment.