Skip to main content

FileStream

Struct FileStream 

Source
pub struct FileStream<S> {
    pub stream: S,
    pub file_name: Option<String>,
    pub content_size: Option<u64>,
    pub etag: Option<String>,
    pub last_modified: Option<SystemTime>,
    pub content_type: Option<String>,
}
Available on crate feature file-stream only.
Expand description

HTTP file stream with metadata support for efficient file delivery.

FileStream wraps any stream that produces bytes and associates it with optional metadata like filename and content size. This enables proper HTTP headers to be set for file downloads, including Content-Disposition for filename suggestions and Content-Length for known file sizes. The implementation supports both regular responses and HTTP range requests for partial content delivery.

Fields§

§stream: S

The underlying byte stream

§file_name: Option<String>

Optional filename for Content-Disposition header

§content_size: Option<u64>

Optional content size for Content-Length header

§etag: Option<String>

Optional pre-computed strong ETag value (without quotes).

§last_modified: Option<SystemTime>

Optional last-modified timestamp.

§content_type: Option<String>

Optional content-type override (defaults to application/octet-stream).

Implementations§

Source§

impl<S> FileStream<S>
where S: TryStream + Send + 'static, <S as TryStream>::Ok: Into<Bytes>, <S as TryStream>::Error: Into<Box<dyn Error + Sync + Send>>,

Source

pub fn new( stream: S, file_name: Option<String>, content_size: Option<u64>, ) -> FileStream<S>

Creates a new file stream with the provided metadata.

Source

pub fn with_etag(self, etag: impl Into<String>) -> FileStream<S>

Attach an ETag validator. The value must be fully formed per RFC 9110 §8.8.3 — i.e. quoted ("abc") for a strong validator or weak-prefixed (W/"abc") for a weak one. Use weak_etag_from_metadata to derive a weak validator from (size, mtime).

Source

pub fn with_last_modified(self, ts: SystemTime) -> FileStream<S>

Attach a Last-Modified timestamp.

Source

pub fn with_content_type(self, ct: impl Into<String>) -> FileStream<S>

Override the response Content-Type (defaults to application/octet-stream).

Source

pub async fn from_path<P>( path: P, ) -> Result<FileStream<Once<Ready<Result<Bytes, Error>>>>, Error>
where P: AsRef<Path>,

Available on crate feature compio only.

Creates a file stream from a file system path with automatic metadata detection (compio variant).

⚠️ Memory-DoS warning: the compio backend loads the entire file into a single Bytes allocation up front (compio::fs::read), unlike the tokio variant which streams chunks via ReaderStream. Do not expose this to untrusted requesters with arbitrary file paths; a multi-GB file (or many concurrent multi-GB requests) will allocate the full payload in RAM and can exhaust the process. For untrusted requests on the compio runtime, gate by max-file-size at the route level or write a custom positional-read streamer using compio::fs::File::read_at until tako-streams ships a streaming compio variant (tracked for 2.x).

Source

pub fn into_range_response( self, start: u64, end: u64, total_size: u64, ) -> Response<TakoBody>

Creates an HTTP 206 Partial Content response for range requests.

Caller contract: start <= end < total_size. Violating the inequality used to panic on end - start + 1; we now return a 416 Range Not Satisfiable response with a Content-Range: bytes */{total_size} header per RFC 9110 §15.5.17 instead, so a buggy caller produces a spec-conformant error rather than crashing the worker.

Source

pub async fn try_range_response<P>( path: P, start: u64, end: u64, ) -> Result<Response<TakoBody>, Error>
where P: AsRef<Path>,

Available on crate feature compio only.

Try to create a range response for a file stream (compio variant).

⚠️ Same memory-DoS caveat as FileStream::from_path (compio): the whole file is read into a single buffer before the requested range is sliced out, instead of doing a positional read_at(start, len). Do not expose to untrusted requesters on arbitrary file paths; gate by file-size at the route level or switch to the tokio backend for streaming.

Trait Implementations§

Source§

impl<S> Responder for FileStream<S>
where S: TryStream + Send + 'static, <S as TryStream>::Ok: Into<Bytes>, <S as TryStream>::Error: Into<Box<dyn Error + Sync + Send>>,

Source§

fn into_response(self) -> Response<TakoBody>

Converts the file stream into an HTTP response with appropriate headers.

Auto Trait Implementations§

§

impl<S> Freeze for FileStream<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for FileStream<S>
where S: RefUnwindSafe,

§

impl<S> Send for FileStream<S>
where S: Send,

§

impl<S> Sync for FileStream<S>
where S: Sync,

§

impl<S> Unpin for FileStream<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for FileStream<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for FileStream<S>
where S: UnwindSafe,

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> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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