Kashiftufail
Kashiftufail

Reputation: 10885

How create zip file using rubyzip on heroku

I am working with creating zip file after read users cv's. Code is working fine on local machine. But there is error when deploy this code on heroku.

   def get_stream(contractors)
    #t = Tempfile.new("my-temp-filename-#{Time.now}")  #this code on local
    t = Tempfile.new("#{Rails.root}/tmp/my-temp-filename-#{Time.now}") #this line on heroku break
    begin
      Zip::OutputStream.open(t.path) do |zos|
        contractors.each_with_index do |contractor, index|
          if contractor.cv.path
            zos.put_next_entry("#{index+1}_#{contractor.full_name.parameterize.underscore}.#{contractor.cv_file_name.split(".").last}")
            zos.print IO.read(contractor.cv.path)
         end
     end
   end
  send_file t.path, :type => 'application/zip', :filename => "Contractors_cvs.zip", :x_sendfile => true
    t.close
  rescue
    t.close
  end

end

while heroku logs --app just show me "Completed 500 Internal Server Error in 56ms"

Any help please.

Thanks

Upvotes: 0

Views: 2857

Answers (1)

Kashiftufail
Kashiftufail

Reputation: 10885

Finally this works for me on both local and heroku.

     def get_stream(contractors)
       begin
        Zip::File.open("#{Rails.root}/tmp/zipfile_name.zip", Zip::File::CREATE) do |zipfile|
        contractors.each_with_index do |filename, index|
      zipfile.add(filename.try(:cv_file_name), filename.try(:cv).try(:path)) if filename.present?
            end
           end

      send_file "#{Rails.root}/tmp/zipfile_name.zip", :type => 'application/zip', :filename => "Contractor_cvs.zip", :x_sendfile => true
  File.delete("#{Rails.root}/tmp/zipfile_name.zip")

       rescue         
       end
     end

Upvotes: 2

Related Questions