Skip to content

Instantly share code, notes, and snippets.

@hiendinhngoc
Created February 28, 2019 07:53
Show Gist options
  • Save hiendinhngoc/c705452fd32ad989d9ef7c0bf30eb163 to your computer and use it in GitHub Desktop.
Save hiendinhngoc/c705452fd32ad989d9ef7c0bf30eb163 to your computer and use it in GitHub Desktop.
def download_all_post_contents
posts = @campaign.posts
if posts.present?
folder = "/tmp"
file = "/tmp/archive#{Time.now.to_i}_.zip"
urls = []
posts.each do |post|
urls << post.filestack_image_url if post.filestack_image_url.present?
urls << post.photo_attributes('original')['url'] if post.photo.present?
urls << post.filestack_video_url if post.filestack_video_url.present?
urls << post.video_attributes('remote')['url'] if post.video.present?
end
urls = urls.collect {|url| download_url(url) }.compact
Zip::File.open(file, Zip::File::CREATE) do |zipfile|
urls.each do |filename|
zipfile.add(filename, File.join(folder, filename.path.split('/').last))
end
end
send_file(file, :type => 'application/zip', :filename => 'post_contents.zip')
else
redirect_to brands_campaign_path(@campaign), notice: 'This campaign has no post.'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment