Some time ago I wrote a question regarding the use of temporary files within a rails app. On thar particular case, I decided to use tempfile
This causes a problem if I also want to use the x-sendfile
directive (as a parameter in Rails 2, or as a configuration option in Rails 3) so that the file sending is handled by my web server directly, not my rails app.
So I thought about doing something like this:
require 'tempfile'
def foo()
# creates a temporary file in tmp/
Tempfile.open('prefix', "#{Rails.root}/tmp") do |f|
f.print('a temp message')
f.flush
send_file(f.path, :x_sendfile => true) # send_file f.path in rails 3
end
end
This setup has one issue: the file is deleted before being sent!
On one hand, tempfile
will delete the file as soon as the Tempfile.open
block is over. On the other, x-sendfile
makes the send_file call asynchronous - it returns very quickly, so the server hardly has time to send the file.
My best possible solution right now involves using non-temporary files (File instead of Tempfile), and then a cron task that erases the temp folder periodically. This is a bit inelegant since:
- I have to use my own tempfile naming scheme
- Files stay on the tmp folder longer than they are needed.
Is there a better setup? Or, is there at least a "success" callback on the asynchronous send_file
, so I can erase f when it's done?
Thanks a lot.