Skip to content
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

feat(upload): respect content-disposition response header during download #2168

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions plugins/upload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use tokio_util::codec::{BytesCodec, FramedRead};
use read_progress_stream::ReadProgressStream;

use std::collections::HashMap;
use std::path::Path;

type Result<T> = std::result::Result<T, Error>;

Expand Down Expand Up @@ -78,7 +79,7 @@ async fn download(
} else {
client.get(url)
};
// Loop trought the headers keys and values
// Loop through the headers keys and values
// and add them to the request object.
for (key, value) in headers {
request = request.header(&key, value);
Expand All @@ -93,7 +94,30 @@ async fn download(
}
let total = response.content_length().unwrap_or(0);

let mut file = BufWriter::new(File::create(file_path).await?);
// Check if file_path is a directory
let path = Path::new(file_path);
let mut final_file_path = file_path.to_string();
if path.is_dir() {
if let Some(content_disposition) = response.headers().get("content-disposition") {
if let Ok(content_disposition) = content_disposition.to_str() {
if let Some(filename) = content_disposition.split(';').find_map(|part| {
if part.trim().starts_with("filename=") {
Some(
part.trim()
.trim_start_matches("filename=")
.trim_matches('"'),
)
} else {
None
}
}) {
final_file_path = path.join(filename).to_string_lossy().to_string();
}
}
}
}

let mut file = BufWriter::new(File::create(&final_file_path).await?);
let mut stream = response.bytes_stream();

let mut stats = TransferStats::default();
Expand Down
Loading