Created
July 16, 2014 13:36
-
-
Save walterdavis/4cc538c03f6809447fc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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