@@ -372,7 +372,30 @@ impl Body {
372
372
{
373
373
let path = path. as_ref ( ) ;
374
374
let file = async_std:: fs:: File :: open ( path) . await ?;
375
- Self :: from_file ( file, path) . await
375
+ Self :: from_file_with_path ( file, path) . await
376
+ }
377
+
378
+ /// Create a `Body` from an already-open file.
379
+ ///
380
+ /// The Mime type is sniffed from the file contents if possible, otherwise
381
+ /// is set to `application/octet-stream`.
382
+ ///
383
+ /// # Examples
384
+ ///
385
+ /// ```no_run
386
+ /// # fn main() -> http_types::Result<()> { async_std::task::block_on(async {
387
+ /// use http_types::{Body, Response, StatusCode};
388
+ ///
389
+ /// let mut res = Response::new(StatusCode::Ok);
390
+ /// let path = std::path::Path::new("/path/to/file");
391
+ /// let file = async_std::fs::File::open(path).await?;
392
+ /// res.set_body(Body::from_file(file).await?);
393
+ /// # Ok(()) }) }
394
+ /// ```
395
+ #[ cfg( all( feature = "fs" , not( target_os = "unknown" ) ) ) ]
396
+ #[ inline]
397
+ pub async fn from_file ( file : async_std:: fs:: File ) -> io:: Result < Self > {
398
+ Self :: from_file_with_path ( file, std:: path:: Path :: new ( "" ) ) . await
376
399
}
377
400
378
401
/// Create a `Body` from an already-open file.
@@ -393,11 +416,11 @@ impl Body {
393
416
/// let mut res = Response::new(StatusCode::Ok);
394
417
/// let path = std::path::Path::new("/path/to/file");
395
418
/// let file = async_std::fs::File::open(path).await?;
396
- /// res.set_body(Body::from_file (file, path).await?);
419
+ /// res.set_body(Body::from_file_with_path (file, path).await?);
397
420
/// # Ok(()) }) }
398
421
/// ```
399
422
#[ cfg( all( feature = "fs" , not( target_os = "unknown" ) ) ) ]
400
- pub async fn from_file (
423
+ pub async fn from_file_with_path (
401
424
mut file : async_std:: fs:: File ,
402
425
path : & std:: path:: Path ,
403
426
) -> io:: Result < Self > {
0 commit comments