Created
December 12, 2008 16:40
-
-
Save ahoward/35184 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
def cache document | |
FileUtils.mkdir_p File.join(RAILS_ROOT, 'private') | |
basename = File.basename(document.filename) | |
pathname = File.join(RAILS_ROOT, 'private', document.id.to_s, basename) | |
cached = | |
begin | |
File.stat(pathname).mtime <= document.created_at | |
rescue | |
false | |
end | |
unless cached | |
dirname, basename = File.split pathname | |
FileUtils.mkdir_p dirname | |
open(pathname, 'w') do |f| | |
f.write document.current_data | |
log "cached #{ pathname }" | |
end | |
end | |
pathname | |
end | |
def inline! | |
disposition = params[:disposition] || 'attachment'; | |
if RAILS_ENV == 'production' | |
begin | |
x_send_file cache(@document), :disposition => disposition | |
rescue Object => e | |
send_data(@document.current_data, { | |
:filename => @document.filename, | |
:type => @document.content_type, | |
:disposition => disposition, | |
}) | |
end | |
else | |
send_data(@document.current_data, { | |
:filename => @document.filename, | |
:type => @document.content_type, | |
:disposition => disposition, | |
}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment