Sha256: 3b675c275b22c6761c036aec427e1de00187e0418b3bbc1248db34aaec12b148

Contents?: true

Size: 1.1 KB

Versions: 103

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module ActionDispatch
  module Http
    class ContentDisposition # :nodoc:
      def self.format(disposition:, filename:)
        new(disposition: disposition, filename: filename).to_s
      end

      attr_reader :disposition, :filename

      def initialize(disposition:, filename:)
        @disposition = disposition
        @filename = filename
      end

      TRADITIONAL_ESCAPED_CHAR = /[^ A-Za-z0-9!\#$+.^_`|~-]/

      def ascii_filename
        'filename="' + percent_escape(I18n.transliterate(filename), TRADITIONAL_ESCAPED_CHAR) + '"'
      end

      RFC_5987_ESCAPED_CHAR = /[^A-Za-z0-9!\#$&+.^_`|~-]/

      def utf8_filename
        "filename*=UTF-8''" + percent_escape(filename, RFC_5987_ESCAPED_CHAR)
      end

      def to_s
        if filename
          "#{disposition}; #{ascii_filename}; #{utf8_filename}"
        else
          "#{disposition}"
        end
      end

      private
        def percent_escape(string, pattern)
          string.gsub(pattern) do |char|
            char.bytes.map { |byte| "%%%02X" % byte }.join
          end
        end
    end
  end
end

Version data entries

103 entries across 98 versions & 7 rubygems

Version Path
actionpack-7.1.5 lib/action_dispatch/http/content_disposition.rb
actionpack-7.1.4.2 lib/action_dispatch/http/content_disposition.rb
actionpack-7.0.8.6 lib/action_dispatch/http/content_disposition.rb
actionpack-6.1.7.10 lib/action_dispatch/http/content_disposition.rb
actionpack-6.1.7.9 lib/action_dispatch/http/content_disposition.rb
actionpack-7.1.4.1 lib/action_dispatch/http/content_disposition.rb
actionpack-7.0.8.5 lib/action_dispatch/http/content_disposition.rb
actionpack-7.1.4 lib/action_dispatch/http/content_disposition.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.0.8.4/lib/action_dispatch/http/content_disposition.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/actionpack-7.1.3.4/lib/action_dispatch/http/content_disposition.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/http/content_disposition.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/http/content_disposition.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/actionpack-7.0.5.1/lib/action_dispatch/http/content_disposition.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/actionpack-7.1.3.4/lib/action_dispatch/http/content_disposition.rb
actionpack-7.1.3.4 lib/action_dispatch/http/content_disposition.rb
actionpack-7.0.8.4 lib/action_dispatch/http/content_disposition.rb
actionpack-6.1.7.8 lib/action_dispatch/http/content_disposition.rb
actionpack-7.1.3.2 lib/action_dispatch/http/content_disposition.rb
actionpack-7.1.3.1 lib/action_dispatch/http/content_disposition.rb
actionpack-7.0.8.1 lib/action_dispatch/http/content_disposition.rb