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:
Content-Length— exact when it is known (path, seekable stream)ETag/Last-Modified— derived from the file, or supplied viaFileBody::entity_tag/FileBody::last_modifiedfor streamsAccept-RangesandRangehandling (206/416) — when the source can seek andFileBody::enable_range_processingis left onIf-Range,If-None-Match,If-Modified-Since(304)
Build one with FileBody::path, FileBody::stream or
FileBody::seekable_stream, then hand it to
crate::action::FileResult::with_body.
Fields§
§source: FileSourceWhere 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: DispositionWhether the content should be shown inline or downloaded.
enable_range_processing: boolWhether 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
impl FileBody
Sourcepub fn path(path: impl Into<PathBuf>) -> FileBody
pub fn path(path: impl Into<PathBuf>) -> FileBody
Send the file at path — ASP.NET Core’s PhysicalFile(...).
Sourcepub fn stream(reader: impl AsyncRead + Send + Unpin + 'static) -> FileBody
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.
Sourcepub fn seekable_stream(
reader: impl AsyncRead + AsyncSeek + Send + Unpin + 'static,
len: u64,
) -> FileBody
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.
Sourcepub fn static_bytes(bytes: &'static [u8]) -> FileBody
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.
Sourcepub fn content_type(self, content_type: impl Into<String>) -> FileBody
pub fn content_type(self, content_type: impl Into<String>) -> FileBody
Override the media type instead of deriving it from the extension.
Sourcepub fn download_name(self, file_name: impl Into<String>) -> FileBody
pub fn download_name(self, file_name: impl Into<String>) -> FileBody
Send the file as a download named file_name.
Sourcepub fn inline_name(self, file_name: impl Into<String>) -> FileBody
pub fn inline_name(self, file_name: impl Into<String>) -> FileBody
Send the file inline under file_name.
Sourcepub fn enable_range_processing(self, enabled: bool) -> FileBody
pub fn enable_range_processing(self, enabled: bool) -> FileBody
Enable or disable Range processing.
Sourcepub fn last_modified(self, last_modified: SystemTime) -> FileBody
pub fn last_modified(self, last_modified: SystemTime) -> FileBody
Advertise an explicit Last-Modified.
Sourcepub fn entity_tag(self, entity_tag: impl Into<String>) -> FileBody
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.
Sourcepub fn is_range_capable(&self) -> bool
pub fn is_range_capable(&self) -> bool
Whether this source can seek, and therefore serve byte ranges.