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

How best to set mime type on file Part? #2244

Open
rogusdev opened this issue Apr 11, 2024 · 0 comments
Open

How best to set mime type on file Part? #2244

rogusdev opened this issue Apr 11, 2024 · 0 comments

Comments

@rogusdev
Copy link

rogusdev commented Apr 11, 2024

What is the recommend way to set a mime type on a multipart file Part, given that it might fail? The fact that there is no try_mime or something means that I when build a Part and attempt to set its mime type, I then might need to completely rebuild it, e.g.:

let mut part = Part::bytes(bytes.clone()).file_name(filename.clone());
if let Some(mime_type) = mime_guess::from_path(filename.clone()).first_raw() {
    match part.mime_str(mime_type) {
        Ok(part2) => part = part2,
        Err(_) => part = Part::bytes(bytes).file_name(filename),
    }
}
form = form.part(key, part);

(To be clear: this code compiles, but I did not actually end up testing it. I found this disagreeable enough to scrap it entirely.)

All I want is something like:

form = form.part(key, Part::bytes(bytes).file_name(filename).set_mime_best_guess());

Where it assigns a mime type from the filename, only if it can find a valid one -- the reqwest code already uses mime_guess for mime anyway -- and if I should still do the guessing myself, then I would like the set_mime() fn to be pub, since it's already there anyway.

Relevant code sections:

I'm happy to put up a PR implementing my idea, if given the blessing to do so. And, certainly, if there is a better recommended approach, I will gladly use that. Thank you!

@rogusdev rogusdev changed the title How best to set mime type on file part? How best to set mime type on file Part? Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant