Skip to content

Instantly share code, notes, and snippets.

@marcomontes
Created June 11, 2015 14:43
Show Gist options
  • Save marcomontes/342112b70ea9322d34b5 to your computer and use it in GitHub Desktop.
Save marcomontes/342112b70ea9322d34b5 to your computer and use it in GitHub Desktop.
Get images from Class, pack into Zip file and upload to S3. Notify user by mail with url for zip file
class << self
def download_zip(user, photo_ids)
photos = SurveyPhoto.find photo_ids.gsub(' ', ',').split(",")
zipfile_name = File.join(Rails.root, "tmp", "archive.zip")
Zip::ZipOutputStream.open(zipfile_name) do |zipfile|
photos.each do |photo|
if photo.image.present?
zipfile.put_next_entry("#{Time.now.strftime('%M%S')}-#{photo[:image]}")
zipfile.write photo.image.file.read
end
end
end
storage = Fog::Storage.new(:provider => 'AWS', :aws_access_key_id => ENV['AWS_ACCESS_KEY'], :aws_secret_access_key => ENV['AWS_SECRET_KEY'])
bucket = ENV['AWS_BUCKET']
web_file_name = "fotos-#{Time.now.strftime('%d-%m-%y_%H-%M')}.zip"
storage.put_object(
bucket,
File.join("zip_photos/", web_file_name),
File.open(zipfile_name),
{'x-amz-acl' => 'public-read'}
)
File.delete(zipfile_name)
download_link = "https://#{bucket}.s3.amazonaws.com/zip_photos/#{web_file_name}"
UserMailer.download_photos(user.email, download_link).deliver
end
handle_asynchronously :download_zip
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment