warp icon indicating copy to clipboard operation
warp copied to clipboard

Make resp field on File public

Open Dissssy opened this issue 1 year ago • 2 comments

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 avatar May 06 '23 21:05 Dissssy

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.

seanmonstar avatar May 08 '23 13:05 seanmonstar

.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

Dissssy avatar May 12 '23 15:05 Dissssy