-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an encapsulated file stream in axum-extra to make it more conveni… #3047
Open
YanHeDoki
wants to merge
8
commits into
tokio-rs:main
Choose a base branch
from
YanHeDoki:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+434
−10
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
566a323
Add an encapsulated file stream in axum-extra to make it more conveni…
YanHeDoki f0df9ce
Correction of ci checks
YanHeDoki 4b45d89
fix clippy warnings
YanHeDoki 13547b6
Fix review warnings
YanHeDoki 6967a97
fix dependency tables check
YanHeDoki 1f2d314
Add a from_path function to make it easier to return a stream of file…
YanHeDoki f3fd30b
Corrected the features format, the example creates the file first to …
YanHeDoki bcfaf3d
fix ci warnings
YanHeDoki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
use axum::{ | ||
body, | ||
response::{IntoResponse, Response}, | ||
BoxError, | ||
}; | ||
use bytes::Bytes; | ||
use futures_util::TryStream; | ||
use http::{header, StatusCode}; | ||
use std::{io, path::PathBuf}; | ||
use tokio::fs::File; | ||
use tokio_util::io::ReaderStream; | ||
|
||
/// Alias for `tokio_util::io::ReaderStream<File>`. | ||
pub type AsyncReaderStream = ReaderStream<File>; | ||
|
||
/// Encapsulate the file stream. | ||
/// The encapsulated file stream construct requires passing in a stream | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use axum::{ | ||
/// http::StatusCode, | ||
/// response::{Response, IntoResponse}, | ||
/// Router, | ||
/// routing::get | ||
/// }; | ||
/// use axum_extra::response::file_stream::FileStream; | ||
/// use tokio::fs::File; | ||
/// use tokio_util::io::ReaderStream; | ||
/// async fn file_stream() -> Result<Response, (StatusCode, String)> { | ||
/// let stream=ReaderStream::new(File::open("test.txt").await.map_err(|e| (StatusCode::NOT_FOUND, format!("File not found: {e}")))?); | ||
/// let file_stream_resp = FileStream::new(stream) | ||
/// .file_name("test.txt"); | ||
// | ||
/// Ok(file_stream_resp.into_response()) | ||
/// } | ||
/// let app = Router::new().route("/FileStreamDownload", get(file_stream)); | ||
/// # let _: Router = app; | ||
/// ``` | ||
#[derive(Debug)] | ||
pub struct FileStream<S> | ||
where | ||
S: TryStream + Send + 'static, | ||
S::Ok: Into<Bytes>, | ||
S::Error: Into<BoxError>, | ||
{ | ||
/// stream. | ||
pub stream: S, | ||
/// The file name of the file. | ||
pub file_name: Option<String>, | ||
/// The size of the file. | ||
pub content_size: Option<u64>, | ||
} | ||
|
||
impl<S> FileStream<S> | ||
where | ||
S: TryStream + Send + 'static, | ||
S::Ok: Into<Bytes>, | ||
S::Error: Into<BoxError>, | ||
{ | ||
/// Create a file stream. | ||
pub fn new(stream: S) -> Self { | ||
Self { | ||
stream, | ||
file_name: None, | ||
content_size: None, | ||
} | ||
} | ||
|
||
/// Create a file stream from a file path. | ||
/// # Examples | ||
/// ``` | ||
/// use axum::{ | ||
/// http::StatusCode, | ||
/// response::{Response, IntoResponse}, | ||
/// Router, | ||
/// routing::get | ||
/// }; | ||
/// use axum_extra::response::file_stream::FileStream; | ||
/// use std::path::PathBuf; | ||
/// use tokio::fs::File; | ||
/// use tokio_util::io::ReaderStream; | ||
/// async fn file_stream() -> Response { | ||
/// FileStream::<ReaderStream<File>>::from_path(PathBuf::from("test.txt")) | ||
/// .await | ||
/// .map_err(|e| (StatusCode::NOT_FOUND, format!("File not found: {e}"))) | ||
/// .into_response() | ||
/// } | ||
/// let app = Router::new().route("/FileStreamDownload", get(file_stream)); | ||
/// # let _: Router = app; | ||
/// ``` | ||
pub async fn from_path(path: PathBuf) -> io::Result<FileStream<AsyncReaderStream>> { | ||
// open file | ||
let file = File::open(&path).await?; | ||
let mut content_size = None; | ||
let mut file_name = None; | ||
|
||
// get file metadata length | ||
if let Ok(metadata) = file.metadata().await { | ||
content_size = Some(metadata.len()); | ||
} | ||
|
||
// get file name | ||
if let Some(file_name_os) = path.file_name() { | ||
if let Some(file_name_str) = file_name_os.to_str() { | ||
file_name = Some(file_name_str.to_owned()); | ||
} | ||
} | ||
|
||
// return FileStream | ||
Ok(FileStream { | ||
stream: ReaderStream::new(file), | ||
file_name, | ||
content_size, | ||
}) | ||
} | ||
|
||
/// Set the file name of the file. | ||
pub fn file_name<T: Into<String>>(mut self, file_name: T) -> Self { | ||
self.file_name = Some(file_name.into()); | ||
self | ||
} | ||
|
||
/// Set the size of the file. | ||
pub fn content_size<T: Into<u64>>(mut self, len: T) -> Self { | ||
self.content_size = Some(len.into()); | ||
self | ||
} | ||
} | ||
|
||
impl<S> IntoResponse for FileStream<S> | ||
where | ||
S: TryStream + Send + 'static, | ||
S::Ok: Into<Bytes>, | ||
S::Error: Into<BoxError>, | ||
{ | ||
fn into_response(self) -> Response { | ||
let mut resp = Response::builder().header(header::CONTENT_TYPE, "application/octet-stream"); | ||
|
||
if let Some(file_name) = self.file_name { | ||
resp = resp.header( | ||
header::CONTENT_DISPOSITION, | ||
format!("attachment; filename=\"{}\"", file_name), | ||
); | ||
}; | ||
|
||
if let Some(content_size) = self.content_size { | ||
resp = resp.header(header::CONTENT_LENGTH, content_size); | ||
}; | ||
|
||
resp.body(body::Body::from_stream(self.stream)) | ||
.unwrap_or_else(|e| { | ||
( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("build FileStream responsec error:{}", e), | ||
) | ||
.into_response() | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use axum::{extract::Request, routing::get, Router}; | ||
use body::Body; | ||
use http_body_util::BodyExt; | ||
use std::io::Cursor; | ||
use tokio_util::io::ReaderStream; | ||
use tower::ServiceExt; | ||
|
||
#[tokio::test] | ||
async fn response_file_stream() -> Result<(), Box<dyn std::error::Error>> { | ||
let app = Router::new().route( | ||
"/file", | ||
get(|| async { | ||
// Simulating a file stream | ||
let file_content = b"Hello, this is the simulated file content!".to_vec(); | ||
let size = file_content.len() as u64; | ||
let reader = Cursor::new(file_content); | ||
|
||
// response file stream | ||
let stream = ReaderStream::new(reader); | ||
FileStream::new(stream) | ||
.file_name("test") | ||
.content_size(size) | ||
.into_response() | ||
}), | ||
); | ||
|
||
// Simulating a GET request | ||
let response = app | ||
.oneshot(Request::builder().uri("/file").body(Body::empty())?) | ||
.await?; | ||
|
||
// Validate Response Status Code | ||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
// Validate Response Headers | ||
assert_eq!( | ||
response.headers().get("content-type").unwrap(), | ||
"application/octet-stream" | ||
); | ||
assert_eq!( | ||
response.headers().get("content-disposition").unwrap(), | ||
"attachment; filename=\"test\"" | ||
); | ||
assert_eq!(response.headers().get("content-length").unwrap(), "42"); | ||
|
||
// Validate Response Body | ||
let body: &[u8] = &response.into_body().collect().await?.to_bytes(); | ||
assert_eq!( | ||
std::str::from_utf8(body)?, | ||
"Hello, this is the simulated file content!" | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn response_from_path() -> Result<(), Box<dyn std::error::Error>> { | ||
let app = Router::new().route( | ||
"/from_path", | ||
get(move || async move { | ||
FileStream::<AsyncReaderStream>::from_path("CHANGELOG.md".into()) | ||
.await | ||
.unwrap() | ||
.into_response() | ||
}), | ||
); | ||
|
||
// Simulating a GET request | ||
let response = app | ||
.oneshot( | ||
Request::builder() | ||
.uri("/from_path") | ||
.body(Body::empty()) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
// Validate Response Status Code | ||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
// Validate Response Headers | ||
assert_eq!( | ||
response.headers().get("content-type").unwrap(), | ||
"application/octet-stream" | ||
); | ||
assert_eq!( | ||
response.headers().get("content-disposition").unwrap(), | ||
"attachment; filename=\"CHANGELOG.md\"" | ||
); | ||
|
||
let file = File::open("CHANGELOG.md").await.unwrap(); | ||
// get file size | ||
let content_length = file.metadata().await.unwrap().len(); | ||
|
||
assert_eq!( | ||
response | ||
.headers() | ||
.get("content-length") | ||
.unwrap() | ||
.to_str() | ||
.unwrap(), | ||
content_length.to_string() | ||
); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a
from_file
function where we read the file, set the name and the content size?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a from_path function to make it easier to return a stream of files with file name and content size.