pub struct Download { /* private fields */ }Expand description
Files sent as downloads: bytes made in the handler (a PDF, a CSV),
a file on disk (streamed), a key in Storage, or any stream.
use renox::Download;
async fn invoice(State(state): State<AppState>, Path(id): Path<i64>) -> Result<Download> {
let pdf: Vec<u8> = b"%PDF-1.7 ...".to_vec(); // made by your PDF library
Ok(Download::bytes(format!("invoice-{id}.pdf"), "application/pdf", pdf).inline())
}
async fn export(State(state): State<AppState>) -> Result<Download> {
Download::from_storage(&state.storage, "exports/sales.csv", "sales.csv").await
}
async fn backup() -> Result<Download> {
Download::file("storage/backup.db", "backup.db").await // streamed, not read into memory
}Implementations§
Source§impl Download
impl Download
Sourcepub fn bytes(
filename: impl Into<String>,
content_type: impl Into<String>,
data: impl Into<Bytes>,
) -> Self
pub fn bytes( filename: impl Into<String>, content_type: impl Into<String>, data: impl Into<Bytes>, ) -> Self
data as a file named filename.
Sourcepub async fn file(
path: impl AsRef<Path>,
filename: impl Into<String>,
) -> Result<Self>
pub async fn file( path: impl AsRef<Path>, filename: impl Into<String>, ) -> Result<Self>
A file on disk, streamed; its type comes from its extension. A missing file is a 404.
Sourcepub async fn from_storage(
storage: &Storage,
key: &str,
filename: impl Into<String>,
) -> Result<Self>
pub async fn from_storage( storage: &Storage, key: &str, filename: impl Into<String>, ) -> Result<Self>
A file in Storage (local disk or S3). A missing key is a 404.
Trait Implementations§
Source§impl IntoResponse for Download
impl IntoResponse for Download
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Auto Trait Implementations§
impl !RefUnwindSafe for Download
impl !Sync for Download
impl !UnwindSafe for Download
impl Freeze for Download
impl Send for Download
impl Unpin for Download
impl UnsafeUnpin for Download
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more