rails - x-sendfile + arquivos temporários
Algo tempo atrás eu escreviuma pergunt sobre o uso de arquivos temporários em um aplicativo rails. Nesse caso particular, decidi usar tempfile
Isso causa um problema se eu também quiser usar ox-sendfile
directiva como parâmetro no Rails 2 ou como opção de configuração no Rails 3) para que o envio do arquivo seja tratado diretamente pelo meu servidor da Web, não pelo meu aplicativo Rail
Então eu pensei em fazer algo assim:
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
<p>This setup has one issue: the file is deleted before being sent!</p><p>On one hand, <code>tempfile</code> will delete the file as soon as the <code>Tempfile.open</code> block is over. On the other, <code>x-sendfile</code> makes the send_file call asynchronous - it returns very quickly, so the server hardly has time to send the file.</p><p>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:</p>I have to use my own tempfile naming schemeFiles stay on the tmp folder longer than they are needed.<p>Is there a better setup? Or, is there at least a "success" callback on the asynchronous <code>send_file</code>, so I can erase f when it's done?</p>
Thanks a lot.