Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created December 12, 2008 16:40
Show Gist options
  • Save ahoward/35184 to your computer and use it in GitHub Desktop.
Save ahoward/35184 to your computer and use it in GitHub Desktop.
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