Skip to main content

FileBody

Struct FileBody 

Source
pub struct FileBody {
    pub source: FileSource,
    pub content_type: Option<String>,
    pub file_name: Option<String>,
    pub disposition: Disposition,
    pub enable_range_processing: bool,
    pub last_modified: Option<SystemTime>,
    pub entity_tag: Option<String>,
}
Expand description

A file response — the rust-webx counterpart of ASP.NET Core’s File(...) / PhysicalFile(...) / Results.File(...).

The host streams the content and fills in the HTTP metadata:

Build one with FileBody::path, FileBody::stream or FileBody::seekable_stream, then hand it to crate::action::FileResult::with_body.

Fields§

§source: FileSource

Where the bytes come from.

§content_type: Option<String>

Media type. When None the host infers it from the path extension, or falls back to application/octet-stream for streams.

§file_name: Option<String>

Name used in Content-Disposition.

§disposition: Disposition

Whether the content should be shown inline or downloaded.

§enable_range_processing: bool

Whether Range requests should be served (default true).

Turn it off to force a single full response — useful when range requests would multiply backend reads.

§last_modified: Option<SystemTime>

Last-Modified to advertise.

Derived from the file for FileSource::Path; required for a stream if you want If-Modified-Since to work.

§entity_tag: Option<String>

ETag to advertise.

Derived from size and mtime for FileSource::Path; required for a stream if you want If-None-Match to work.

Implementations§

Source§

impl FileBody

Source

pub fn path(path: impl Into<PathBuf>) -> FileBody

Send the file at path — ASP.NET Core’s PhysicalFile(...).

Source

pub fn stream(reader: impl AsyncRead + Send + Unpin + 'static) -> FileBody

Send an async reader of unknown length — ASP.NET Core’s File(Stream, ...).

The response uses chunked transfer encoding and does not serve Range. If you know the length and can seek, use FileBody::seekable_stream so clients can resume and cache.

Source

pub fn seekable_stream( reader: impl AsyncRead + AsyncSeek + Send + Unpin + 'static, len: u64, ) -> FileBody

Send an async reader of known length that supports seeking.

This is the stream form that keeps every HTTP nicety: exact Content-Length, Range/206, and — with FileBody::entity_tag — conditional requests.

Source

pub fn static_bytes(bytes: &'static [u8]) -> FileBody

Send bytes compiled into the executable — ASP.NET Core’s embedded ManifestEmbeddedFileProvider.

The slice is borrowed, never copied, and is served as a seekable source, so Content-Length, Range/206 and conditional requests all work. Build one with include_bytes!-backed tables, such as the ones produced by webx::builder().web_root(...).build() and included with webx::spa::embed_assets!().

The extension-derived media type is not available for in-memory bytes, so set FileBody::content_type explicitly. There is no mtime either, so supply FileBody::entity_tag (a content hash is the usual choice) if clients should be able to revalidate.

Source

pub fn content_type(self, content_type: impl Into<String>) -> FileBody

Override the media type instead of deriving it from the extension.

Source

pub fn download_name(self, file_name: impl Into<String>) -> FileBody

Send the file as a download named file_name.

Source

pub fn inline_name(self, file_name: impl Into<String>) -> FileBody

Send the file inline under file_name.

Source

pub fn enable_range_processing(self, enabled: bool) -> FileBody

Enable or disable Range processing.

Source

pub fn last_modified(self, last_modified: SystemTime) -> FileBody

Advertise an explicit Last-Modified.

Source

pub fn entity_tag(self, entity_tag: impl Into<String>) -> FileBody

Advertise an explicit ETag.

Include the quotes: "abc123". A strong tag lets clients resume with If-Range.

Source

pub fn is_range_capable(&self) -> bool

Whether this source can seek, and therefore serve byte ranges.

Source

pub fn known_len(&self) -> Option<u64>

The length in bytes, when it is known without touching the filesystem.

Trait Implementations§

Source§

impl Debug for FileBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<FileBody> for ResponseBody

Source§

fn from(file: FileBody) -> ResponseBody

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more