Skip to content

Instantly share code, notes, and snippets.

@abelorian
Last active December 8, 2020 15:55
Show Gist options
  • Save abelorian/71c611ca057a63b1c22abb7ac3e90d74 to your computer and use it in GitHub Desktop.
Save abelorian/71c611ca057a63b1c22abb7ac3e90d74 to your computer and use it in GitHub Desktop.
Zip files from remote server (S3)
protect_from_forgery :except => :get_job_resumes #Optional
require 'zip'
def get_job_resumes
@job = Job.find(params[:job_id])
@file = "#{Rails.root}/files/cvs.zip"
@applications = Application.get_applicants_all(@job.id) # app context method
Zip::ZipOutputStream.open(@file) do |zos|
@applications.each do |application|
user = application.user
if user.account.resume.present? #check if file exists
zos.put_next_entry(user.account.resume.filename) #filename
# user.account.resume.url is a S3 file url
zos.print(URI.parse(user.account.resume.url).read) if user.account.resume_ok?
end
end
end
send_file(@file, filename: ""+@job.slug+"_cvs.zip", type: "application/octet-stream")
end
# Prevent 404 error
def resume_ok?
begin
return false if resume.url.nil?
(URI.parse(resume.url).read)
return true
rescue OpenURI::HTTPError => e
return false
end
end
# Requisites
gem 'rubyzip'
gem 'zip-zip'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment