Skip to content

Instantly share code, notes, and snippets.

@walterdavis
Created July 16, 2014 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save walterdavis/4cc538c03f6809447fc3 to your computer and use it in GitHub Desktop.
Save walterdavis/4cc538c03f6809447fc3 to your computer and use it in GitHub Desktop.
# routes.rb
match 'ads/:id/present/*path.:format', :to => 'ads#present'
# ads_controller.rb
def present
@ad = Ad.find(params[:id])
path = "#{params[:path]}.#{params[:format]}"
send_data(@ad.get_file(path), :filename => path, :disposition => 'inline')
end
# ad.rb
def get_file(path)
tmp = Tempfile.new('unzip', :encoding => 'ascii-8bit')
source = open(unit.path, 'rb').read # unit is the paperclip attribute
begin
tmp.write( source )
zipfile = Zip::ZipFile.open(tmp)
zipfile.each do |entry|
if path == entry.name
return zipfile.read(entry.name)
end
end
end
ensure
tmp.close
tmp.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment