Module: Roda::RodaPlugins::ResponseAttachment::ResponseMethods
- Defined in:
- lib/roda/plugins/response_attachment.rb
Instance Method Summary collapse
-
#attachment(filename = nil, disposition = 'attachment') ⇒ Object
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.
Instance Method Details
#attachment(filename = nil, disposition = 'attachment') ⇒ Object
Set the Content-Disposition to “attachment” with the specified filename, instructing the user agents to prompt to save.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/roda/plugins/response_attachment.rb', line 58 def (filename = nil, disposition='attachment') if filename param_filename = File.basename(filename) encoding = param_filename.encoding needs_encoding = param_filename.gsub!(/[^ 0-9a-zA-Z!\#$&\+\.\^_`\|~]+/, '-') params = "; filename=#{param_filename.inspect}" if needs_encoding && (encoding == UTF8_ENCODING || encoding == ISO88591_ENCODING) # File name contains non attr-char characters from RFC 5987 Section 3.2.1 encoded_filename = File.basename(filename).force_encoding(BINARY_ENCODING) # Similar regexp as above, but treat each byte separately, and encode # space characters, since those aren't allowed in attr-char encoded_filename.gsub!(/[^0-9a-zA-Z!\#$&\+\.\^_`\|~]/) do |c| "%%%X" % c.ord end encoded_params = "; filename*=#{encoding.to_s}''#{encoded_filename}" end unless @headers[RodaResponseHeaders::CONTENT_TYPE] ext = File.extname(filename) if !ext.empty? && (content_type = Rack::Mime.mime_type(ext, nil)) @headers[RodaResponseHeaders::CONTENT_TYPE] = content_type end end end @headers[RodaResponseHeaders::CONTENT_DISPOSITION] = "#{disposition}#{params}#{encoded_params}" end |