pub struct FileBytesStream { /* private fields */ }
Expand description

Wraps a tokio::fs::File, and implements a stream of Bytess.

Implementations§

Create a new stream from the given file.

Create a new stream from the given file, reading up to limit bytes.

Examples found in repository?
src/util/file_bytes_stream.rs (line 98)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    pub fn new(file: File, range: HttpRange) -> FileBytesStreamRange {
        FileBytesStreamRange {
            file_stream: FileBytesStream::new_with_limit(file, range.length),
            seek_state: FileSeekState::NeedSeek,
            start_offset: range.start,
        }
    }

    fn without_initial_range(file: File) -> FileBytesStreamRange {
        FileBytesStreamRange {
            file_stream: FileBytesStream::new_with_limit(file, 0),
            seek_state: FileSeekState::NeedSeek,
            start_offset: 0,
        }
    }
More examples
Hide additional examples
src/util/file_response_builder.rs (line 259)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    pub fn build(
        &self,
        file: File,
        metadata: Metadata,
        content_type: String,
    ) -> Result<Response<Body>> {
        let mut res = ResponseBuilder::new();

        // Set `Last-Modified` and check `If-Modified-Since`.
        let modified = metadata.modified().ok().filter(|v| {
            v.duration_since(UNIX_EPOCH)
                .ok()
                .filter(|v| v >= &MIN_VALID_MTIME)
                .is_some()
        });

        // default to false when specified, either the etag or last_modified will set
        // it to true later.
        let mut range_cond_ok = self.if_range.is_none();
        if let Some(modified) = modified {
            if let Ok(modified_unix) = modified.duration_since(UNIX_EPOCH) {
                // Compare whole seconds only, because the HTTP date-time
                // format also does not contain a fractional part.
                if let Some(Ok(ims_unix)) =
                    self.if_modified_since.map(|v| v.duration_since(UNIX_EPOCH))
                {
                    if modified_unix.as_secs() <= ims_unix.as_secs() {
                        return ResponseBuilder::new()
                            .status(StatusCode::NOT_MODIFIED)
                            .body(Body::empty());
                    }
                }

                let etag = format!(
                    "W/\"{0:x}-{1:x}.{2:x}\"",
                    metadata.len(),
                    modified_unix.as_secs(),
                    modified_unix.subsec_nanos()
                );
                if let Some(ref v) = self.if_range {
                    if *v == etag {
                        range_cond_ok = true;
                    }
                }

                res = res.header(header::ETAG, etag);
            }

            let last_modified_formatted = httpdate::fmt_http_date(modified);
            if let Some(ref v) = self.if_range {
                if *v == last_modified_formatted {
                    range_cond_ok = true;
                }
            }

            res = res
                .header(header::LAST_MODIFIED, last_modified_formatted)
                .header(header::ACCEPT_RANGES, "bytes");
        }

        // Build remaining headers.
        if let Some(seconds) = self.cache_headers {
            res = res.header(
                header::CACHE_CONTROL,
                format!("public, max-age={}", seconds),
            );
        }

        if self.is_head {
            res = res.header(header::CONTENT_LENGTH, format!("{}", metadata.len()));
            return res.status(StatusCode::OK).body(Body::empty());
        }

        let ranges = self.range.as_ref().filter(|_| range_cond_ok).and_then(|r| {
            match HttpRange::parse(r, metadata.len()) {
                Ok(r) => Some(Ok(r)),
                Err(HttpRangeParseError::NoOverlap) => Some(Err(())),
                Err(HttpRangeParseError::InvalidRange) => None,
            }
        });

        if let Some(ranges) = ranges {
            let ranges = match ranges {
                Ok(r) => r,
                Err(()) => {
                    return res
                        .status(StatusCode::RANGE_NOT_SATISFIABLE)
                        .body(Body::empty());
                }
            };

            if ranges.len() == 1 {
                let single_span = ranges[0];
                res = res
                    .header(
                        header::CONTENT_RANGE,
                        content_range_header(&single_span, metadata.len()),
                    )
                    .header(header::CONTENT_LENGTH, format!("{}", single_span.length));

                let body_stream = FileBytesStreamRange::new(file, single_span);
                return res
                    .status(StatusCode::PARTIAL_CONTENT)
                    .body(body_stream.into_body());
            } else if ranges.len() > 1 {
                let mut boundary_tmp = [0u8; BOUNDARY_LENGTH];

                let mut rng = thread_rng();
                for v in boundary_tmp.iter_mut() {
                    // won't panic since BOUNDARY_CHARS is non-empty
                    *v = *BOUNDARY_CHARS.choose(&mut rng).unwrap();
                }

                // won't panic because boundary_tmp is guaranteed to be all ASCII
                let boundary = std::str::from_utf8(&boundary_tmp[..]).unwrap().to_string();

                res = res.header(
                    hyper::header::CONTENT_TYPE,
                    format!("multipart/byteranges; boundary={}", boundary),
                );

                let mut body_stream =
                    FileBytesStreamMultiRange::new(file, ranges, boundary, metadata.len());
                if !content_type.is_empty() {
                    body_stream.set_content_type(&content_type);
                }

                res = res.header(
                    hyper::header::CONTENT_LENGTH,
                    format!("{}", body_stream.compute_length()),
                );

                return res
                    .status(StatusCode::PARTIAL_CONTENT)
                    .body(body_stream.into_body());
            }
        }

        res = res.header(header::CONTENT_LENGTH, format!("{}", metadata.len()));
        if !content_type.is_empty() {
            res = res.header(header::CONTENT_TYPE, content_type);
        }

        // Stream the body.
        res.status(StatusCode::OK)
            .body(FileBytesStream::new_with_limit(file, metadata.len()).into_body())
    }

Create a Hyper Body from this stream.

Examples found in repository?
src/util/file_response_builder.rs (line 259)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
    pub fn build(
        &self,
        file: File,
        metadata: Metadata,
        content_type: String,
    ) -> Result<Response<Body>> {
        let mut res = ResponseBuilder::new();

        // Set `Last-Modified` and check `If-Modified-Since`.
        let modified = metadata.modified().ok().filter(|v| {
            v.duration_since(UNIX_EPOCH)
                .ok()
                .filter(|v| v >= &MIN_VALID_MTIME)
                .is_some()
        });

        // default to false when specified, either the etag or last_modified will set
        // it to true later.
        let mut range_cond_ok = self.if_range.is_none();
        if let Some(modified) = modified {
            if let Ok(modified_unix) = modified.duration_since(UNIX_EPOCH) {
                // Compare whole seconds only, because the HTTP date-time
                // format also does not contain a fractional part.
                if let Some(Ok(ims_unix)) =
                    self.if_modified_since.map(|v| v.duration_since(UNIX_EPOCH))
                {
                    if modified_unix.as_secs() <= ims_unix.as_secs() {
                        return ResponseBuilder::new()
                            .status(StatusCode::NOT_MODIFIED)
                            .body(Body::empty());
                    }
                }

                let etag = format!(
                    "W/\"{0:x}-{1:x}.{2:x}\"",
                    metadata.len(),
                    modified_unix.as_secs(),
                    modified_unix.subsec_nanos()
                );
                if let Some(ref v) = self.if_range {
                    if *v == etag {
                        range_cond_ok = true;
                    }
                }

                res = res.header(header::ETAG, etag);
            }

            let last_modified_formatted = httpdate::fmt_http_date(modified);
            if let Some(ref v) = self.if_range {
                if *v == last_modified_formatted {
                    range_cond_ok = true;
                }
            }

            res = res
                .header(header::LAST_MODIFIED, last_modified_formatted)
                .header(header::ACCEPT_RANGES, "bytes");
        }

        // Build remaining headers.
        if let Some(seconds) = self.cache_headers {
            res = res.header(
                header::CACHE_CONTROL,
                format!("public, max-age={}", seconds),
            );
        }

        if self.is_head {
            res = res.header(header::CONTENT_LENGTH, format!("{}", metadata.len()));
            return res.status(StatusCode::OK).body(Body::empty());
        }

        let ranges = self.range.as_ref().filter(|_| range_cond_ok).and_then(|r| {
            match HttpRange::parse(r, metadata.len()) {
                Ok(r) => Some(Ok(r)),
                Err(HttpRangeParseError::NoOverlap) => Some(Err(())),
                Err(HttpRangeParseError::InvalidRange) => None,
            }
        });

        if let Some(ranges) = ranges {
            let ranges = match ranges {
                Ok(r) => r,
                Err(()) => {
                    return res
                        .status(StatusCode::RANGE_NOT_SATISFIABLE)
                        .body(Body::empty());
                }
            };

            if ranges.len() == 1 {
                let single_span = ranges[0];
                res = res
                    .header(
                        header::CONTENT_RANGE,
                        content_range_header(&single_span, metadata.len()),
                    )
                    .header(header::CONTENT_LENGTH, format!("{}", single_span.length));

                let body_stream = FileBytesStreamRange::new(file, single_span);
                return res
                    .status(StatusCode::PARTIAL_CONTENT)
                    .body(body_stream.into_body());
            } else if ranges.len() > 1 {
                let mut boundary_tmp = [0u8; BOUNDARY_LENGTH];

                let mut rng = thread_rng();
                for v in boundary_tmp.iter_mut() {
                    // won't panic since BOUNDARY_CHARS is non-empty
                    *v = *BOUNDARY_CHARS.choose(&mut rng).unwrap();
                }

                // won't panic because boundary_tmp is guaranteed to be all ASCII
                let boundary = std::str::from_utf8(&boundary_tmp[..]).unwrap().to_string();

                res = res.header(
                    hyper::header::CONTENT_TYPE,
                    format!("multipart/byteranges; boundary={}", boundary),
                );

                let mut body_stream =
                    FileBytesStreamMultiRange::new(file, ranges, boundary, metadata.len());
                if !content_type.is_empty() {
                    body_stream.set_content_type(&content_type);
                }

                res = res.header(
                    hyper::header::CONTENT_LENGTH,
                    format!("{}", body_stream.compute_length()),
                );

                return res
                    .status(StatusCode::PARTIAL_CONTENT)
                    .body(body_stream.into_body());
            }
        }

        res = res.header(header::CONTENT_LENGTH, format!("{}", metadata.len()));
        if !content_type.is_empty() {
            res = res.header(header::CONTENT_TYPE, content_type);
        }

        // Stream the body.
        res.status(StatusCode::OK)
            .body(FileBytesStream::new_with_limit(file, metadata.len()).into_body())
    }

Trait Implementations§

Values yielded by the stream.
Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
Returns the bounds on the remaining length of the stream. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

Creates a future that resolves to the next item in the stream. Read more
Converts this stream into a future of (next_item, tail_of_stream). If the stream terminates, then the next item is None. Read more
Maps this stream’s items to a different type, returning a new stream of the resulting type. Read more
Creates a stream which gives the current iteration count as well as the next value. Read more
Filters the values produced by this stream according to the provided asynchronous predicate. Read more
Filters the values produced by this stream while simultaneously mapping them to a different type according to the provided asynchronous closure. Read more
Computes from this stream’s items new items of a different type using an asynchronous closure. Read more
Transforms a stream into a collection, returning a future representing the result of that computation. Read more
Converts a stream of pairs into a future, which resolves to pair of containers. Read more
Concatenate all items of a stream into a single extendable destination, returning a future representing the end result. Read more
Drives the stream to completion, counting the number of items. Read more
Repeats a stream endlessly. Read more
Execute an accumulating asynchronous computation over a stream, collecting all the values into one final result. Read more
Execute predicate over asynchronous stream, and return true if any element in stream satisfied a predicate. Read more
Execute predicate over asynchronous stream, and return true if all element in stream satisfied a predicate. Read more
Flattens a stream of streams into just one continuous stream. Read more
Flattens a stream of streams into just one continuous stream. Polls inner streams concurrently. Read more
Maps a stream like StreamExt::map but flattens nested Streams. Read more
Maps a stream like StreamExt::map but flattens nested Streams and polls them concurrently, yielding items in any order, as they made available. Read more
Combinator similar to StreamExt::fold that holds internal state and produces a new stream. Read more
Skip elements on this stream while the provided asynchronous predicate resolves to true. Read more
Take elements from this stream while the provided asynchronous predicate resolves to true. Read more
Take elements from this stream until the provided future resolves. Read more
Runs this stream to completion, executing the provided asynchronous closure for each element on the stream. Read more
Runs this stream to completion, executing the provided asynchronous closure for each element on the stream concurrently as elements become available. Read more
Creates a new stream of at most n items of the underlying stream. Read more
Creates a new stream which skips n items of the underlying stream. Read more
Fuse a stream such that poll_next will never again be called once it has finished. This method can be used to turn any Stream into a FusedStream. Read more
Borrows a stream, rather than consuming it. Read more
Catches unwinding panics while polling the stream. Read more
Wrap the stream in a Box, pinning it. Read more
Wrap the stream in a Box, pinning it. Read more
An adaptor for creating a buffered list of pending futures. Read more
An adaptor for creating a buffered list of pending futures (unordered). Read more
An adapter for zipping two streams together. Read more
Adapter for chaining two streams. Read more
Creates a new stream which exposes a peek method. Read more
An adaptor for chunking up items of the stream inside a vector. Read more
An adaptor for chunking up ready items of the stream inside a vector. Read more
Do something with each item of this stream, afterwards passing it on. Read more
Wrap this stream in an Either stream, making it the left-hand variant of that Either. Read more
Wrap this stream in an Either stream, making it the right-hand variant of that Either. Read more
A convenience method for calling [Stream::poll_next] on Unpin stream types.
Returns a Future that resolves when the next item in this stream is ready. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
The type of successful values yielded by this future
The type of failures yielded by this future
Poll this TryStream as if it were a Stream. Read more
Wraps the current stream in a new stream which converts the error type into the one provided. Read more
Wraps the current stream in a new stream which maps the success value using the provided closure. Read more
Wraps the current stream in a new stream which maps the error value using the provided closure. Read more
Chain on a computation for when a value is ready, passing the successful results to the provided closure f. Read more
Chain on a computation for when an error happens, passing the erroneous result to the provided closure f. Read more
Do something with the success value of this stream, afterwards passing it on. Read more
Do something with the error value of this stream, afterwards passing it on. Read more
Wraps a [TryStream] into a type that implements Stream Read more
Creates a future that attempts to resolve the next item in the stream. If an error is encountered before the next item, the error is returned instead. Read more
Attempts to run this stream to completion, executing the provided asynchronous closure for each element on the stream. Read more
Skip elements on this stream while the provided asynchronous predicate resolves to true. Read more
Take elements on this stream while the provided asynchronous predicate resolves to true. Read more
Attempts to run this stream to completion, executing the provided asynchronous closure for each element on the stream concurrently as elements become available, exiting as soon as an error occurs. Read more
Attempt to transform a stream into a collection, returning a future representing the result of that computation. Read more
An adaptor for chunking up successful items of the stream inside a vector. Read more
Attempt to filter the values produced by this stream according to the provided asynchronous closure. Read more
Attempt to filter the values produced by this stream while simultaneously mapping them to a different type according to the provided asynchronous closure. Read more
Flattens a stream of streams into just one continuous stream. Read more
Attempt to execute an accumulating asynchronous computation over a stream, collecting all the values into one final result. Read more
Attempt to concatenate all items of a stream into a single extendable destination, returning a future representing the end result. Read more
Attempt to execute several futures from a stream concurrently (unordered). Read more
Attempt to execute several futures from a stream concurrently. Read more
A convenience method for calling [TryStream::try_poll_next] on Unpin stream types.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more