send_file under scorched?

Skip to first unread message

Hippo Man

Oct 20, 2019, 7:01:24 PM10/20/19
to Scorched
Given that the latest message here is from around 5 years ago, I'm not optimistic that anyone will read or respond to this question of mine. But just in case ...

Could someone point me to an example of getting `send_file` to work under `scorched`? I'm guessing that `Rack::Sendfile` might have to be installed as some sort of middleware, but I'm not sure ... and even if so, I can't fiture out how to do it.

A small piece of sample code of a `scorched` use of `send_file` is all that I'm looking for,

Thanks very much in advance.

Ted Milker

Oct 20, 2019, 7:26:56 PM10/20/19
to Scorched
You can load the Rack::Sendfile in your Controller with something like:

class MyController < Scorched::Controller
  middleware << proc do
    use Rack::Sendfile

Then according to the Rack::Sendfile docs you just need to return a response body that responds to_file.  You might be able to use'/file/to/send').  Your server has to be configured for sendfile support correctly too.

I didn't test this myself but if you're still having trouble tomorrow, I can do it at work tomorrow where I have everything setup to whip up a quick app and try sendfile for myself.

Tom Wardrop

Oct 20, 2019, 7:55:00 PM10/20/19
to Scorched
Hey Hippo Man,

There are all kinds of different scenarios in which you might need to return a file, and each scenario has it's own set of suitable solutions.  Scorched therefore doesn't offer a send file helper, as to it properly, you'd have to re-invent the wheel (i.e R)

For a file on the file system, in most cases you would want your web server to serve them natively (e.g, Nginx, Apache) as they'll always do a better job in terms of both performance and supporting the full HTTP feature set. You would typically redirect the user to the appropriate URL in that case.

For dynamic data, such as serving a file or image from a database, you'd probably be best to create your own little application-specific `send_file` helper. A basic example would be as follows:

def send_file(filename, data)
response.headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
response.headers['Content-Length'] = data.bytesize.to_s
halt data

This is normally an adequate solution for smaller files. For larger files, you'd likely want something that supports the Content-Range headers to allow pause/resume of downloads, which something like Rack::File supports.

Reply all
Reply to author
0 new messages