Skip to content

Instantly share code, notes, and snippets.

@chaserx
Created March 29, 2012 03:35
Show Gist options
  • Save chaserx/2233042 to your computer and use it in GitHub Desktop.
Save chaserx/2233042 to your computer and use it in GitHub Desktop.
S3 Attachments in paperclip to zip file
format.zip {
registrations_with_attachments = Registration.find(:all, :conditions => "abstract_file_name IS NOT NULL")
if registrations_with_attachments.size > 0
headers['Cache-Control'] = 'no-cache'
tmp_filename = "#{Rails.root.to_s}/tmp/tmp_zip_" <<
Time.now.to_f.to_s <<
".zip"
# zipruby gem
Zip::Archive.open(tmp_filename, Zip::CREATE) do |zip|
#get all of the attachments
# attempt to get files stored on S3
# apparently works thanks to Vlad Romascanu;
# http://stackoverflow.com/questions/2338758/zip-up-all-paperclip-attachments-stored-on-s3
registrations_with_attachments.each { |e| zip.add_file("abstracts/#{e.abstract.original_filename}", e.abstract.to_file.path) }
# Should note that these files in S3 bucket are publicly accessible.
send_data(File.open(tmp_filename, "rb+").read, :type => 'application/zip', :disposition => 'attachment', :filename => tmp_filename.to_s)
File.delete tmp_filename
else
redirect_to registrations_path, :notice => "No attachments to zip up"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment