Send_file for temporary file in Sinatra

I am trying to use the send_file command built into Sanatra, but it does not seem to work for tempfiles.

I basically do the following to record an mp3 album:

 get '/example' do songs = ... file_name = "zip_test.zip" t = Tempfile.new(['temp_zip', '.zip']) # t = File.new("testfile.zip", "w") Zip::ZipOutputStream.open(t.path) do |z| songs.each do |song| name = song.name name += ".mp3" unless name.end_with?(".mp3") z.put_next_entry(name) z.print(open(song.url) {|f| f.read }) p song.name + ' added to file' end end p t.path p t.size send_file t.path, :type => 'application/zip', :disposition => 'attachment', :filename => file_name, :stream => false t.close t.unlink end 

When I use t = File.new(...) everything works as expected, but I do not want to use File as it will have problems with concurrency.

When I use t = Tempfile.new(...) , I get:

 !! Unexpected error while processing request: The file identified by body.to_path does not exist` 

Edit: It seems like the problem is that I am sending multiple files. If I just send one song, the Tempfile system also works.

+4
source share
1 answer

I suppose you have a typo in one of your song names or maybe in one of the last parts of the .url song? I accepted your code , and if all the songs exist, sending zip as a temporary file works fine.

0
source

Source: https://habr.com/ru/post/1469340/


All Articles