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

Add an Attachment type to axum-extra #2789

Merged
merged 12 commits into from
Jun 19, 2024
Merged

Add an Attachment type to axum-extra #2789

merged 12 commits into from
Jun 19, 2024

Conversation

joeydewaal
Copy link
Contributor

Refs #2768

Motivation

When trying to send an attachment from Axum it includes,

  • Setting a Content-Disposition header.
  • Using the provided Html, Css, JavaScript or Wasm types or setting a Content-Type header.

I think this could be made a bit easier.

Solution

Add an Attachment response type to axum-extra. This type adds a Content-Disposition header and optionally a Content-Type header to the response.

example:

async fn cargo_toml() -> Result<Attachment<String>, (StatusCode, String)> {
    let file_contents = tokio::fs::read_to_string("Cargo.toml").await
        .map_err(|err| (StatusCode::NOT_FOUND, format!("File not found: {err}")))?;

    Ok(Attachment::new(file_contents)
        .filename("Cargo.toml")
        .content_type("text/x-toml"))
}

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! A few small suggestions below.

axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/mod.rs Outdated Show resolved Hide resolved
@jplatte
Copy link
Member

jplatte commented Jun 17, 2024

CI failure seems unrelated, if you have an idea on how to fix it, a separate PR would be appreciated. Otherwise I'll try to find some time to fix it soon.

@joeydewaal
Copy link
Contributor Author

Thanks for your review and changes, I changed the code. I don't know why CI is failing if I have some time I'll try and fix it.

@jplatte
Copy link
Member

jplatte commented Jun 17, 2024

I looked and the rustdoc failure is really just 2 spaces of indentation missing. Since I can't approve / merge my own PRs, do you mind just adding that to this? It's such a trivial change that I don't mind it being rolled up into another PR.

@joeydewaal
Copy link
Contributor Author

Ofcourse!

@jplatte
Copy link
Member

jplatte commented Jun 17, 2024

Okay, looks like there's more to it. Sorry, I'll go fix it myself in the next couple of days (and I hope David will be around to merge not too long after).

@joeydewaal
Copy link
Contributor Author

Almost there, it's complaining about MakeErasedRouter is never constructed. Is it okay is I add an #[allow(dead_code)] to that type?

@jplatte
Copy link
Member

jplatte commented Jun 18, 2024

Sure, I suspect that there's more that should be done eventually, but to unblock CI that seems like the best solution.

axum-extra/src/routing/typed.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
axum-extra/src/response/attachment.rs Outdated Show resolved Hide resolved
joeydewaal and others added 3 commits June 19, 2024 12:59
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@jplatte jplatte merged commit fcb45b8 into tokio-rs:main Jun 19, 2024
18 checks passed
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

Successfully merging this pull request may close these issues.

2 participants