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

Make resp field on File public #1038

Open
Dissssy opened this issue May 6, 2023 · 2 comments
Open

Make resp field on File public #1038

Dissssy opened this issue May 6, 2023 · 2 comments
Labels
feature New feature or request

Comments

@Dissssy
Copy link

Dissssy commented May 6, 2023

One very useful thing when you're dealing with and serving user uploaded content is the ability to, depending on the type of file, either enforce a download of said file, or if it's something safe like an image, allow it to be embedded into the page (or in the case of direct viewing, be the page, whatever the browser does.)
I was looking into doing some Content-Type filtering on my user uploaded files route using .map() however was disappointed to find the resp field private.

Ideally change this line to pub. There might also be other places where this type of thing would be useful but this is one I just ran into.

As for alternatives, I found one but it's a bit of a hack, I made my own copy of the File struct as well as its contained data within my server crate and made use of the unsafe std::mem::transmute function to convert between them. It works but it's not pretty.

Thanks for your time! I'd submit a PR myself but my experience with github ends with pushing to my own repositories 😅 so extended apologies if I didn't tag correctly as well!

@Dissssy Dissssy added the feature New feature or request label May 6, 2023
@seanmonstar
Copy link
Owner

I made my own copy of the File struct as well as its contained data within my server crate and made use of the unsafe std::mem::transmute function to convert between them. It works but it's not pretty.

This is a very unsafe thing to do. If warp every changes the fields, you will misinterpret the memory into wrong types, causing memory unsafety. Even if warp doesn't, Rust doesn't promise the layout and representation between structs is the same, it is allowed to optimize them differently depending on local context.


I suppose you could just call into_response(), and you'll then have an http::Response that you can manipulate further.

@Dissssy
Copy link
Author

Dissssy commented May 12, 2023

.into_response() would work? I think I figured I would lose the file data if I converted it into a response.

Yeah I know my original "solution" is godawful and I hate using it anywhere, but at the time I figured it was basically my only option tbh. but, huh, hindsight is 20/20 I suppose.

Edit: I just checked, there is no into_response for warp::fs::File which is the struct that is available when you call map on warp::fs::dir().

Nevermind I spelled it wrong or something the first time. I'm half tempted to just delete everything lmao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants