mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:36:10 +02:00
Attempt to guess the content type of a file when opening with --raw (#13521)
# Description Attempt to guess the content type of a file when opening with --raw and set it in the pipeline metadata. <img width="644" alt="Screenshot 2024-08-02 at 11 30 10" src="https://github.com/user-attachments/assets/071f0967-c4dd-405a-b8c8-f7aa073efa98"> # User-Facing Changes - Content of files can be directly piped into commands like `http post` with the content type set appropriately when using `--raw`.
This commit is contained in:
@ -146,11 +146,19 @@ impl Command for Open {
|
||||
}
|
||||
};
|
||||
|
||||
let content_type = if raw {
|
||||
path.extension()
|
||||
.map(|ext| ext.to_string_lossy().to_string())
|
||||
.and_then(|ref s| detect_content_type(s))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let stream = PipelineData::ByteStream(
|
||||
ByteStream::file(file, call_span, engine_state.signals().clone()),
|
||||
Some(PipelineMetadata {
|
||||
data_source: DataSource::FilePath(path.to_path_buf()),
|
||||
content_type: None,
|
||||
content_type,
|
||||
}),
|
||||
);
|
||||
|
||||
@ -268,3 +276,22 @@ fn extract_extensions(filename: &str) -> Vec<String> {
|
||||
|
||||
extensions
|
||||
}
|
||||
|
||||
fn detect_content_type(extension: &str) -> Option<String> {
|
||||
// This will allow the overriding of metadata to be consistent with
|
||||
// the content type
|
||||
match extension {
|
||||
// Per RFC-9512, application/yaml should be used
|
||||
"yaml" | "yml" => Some("application/yaml".to_string()),
|
||||
_ => mime_guess::from_ext(extension)
|
||||
.first()
|
||||
.map(|mime| mime.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
#[test]
|
||||
fn test_content_type() {}
|
||||
}
|
||||
|
Reference in New Issue
Block a user