Sha256: 2531caefd82bc37120a5b84490e224be678fde110f984207d806eef55cd2b451

Contents?: true

Size: 1.18 KB

Versions: 27

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module Gitlab
  # Wrapper class of file response.
  class FileResponse
    HEADER_CONTENT_DISPOSITION = 'Content-Disposition'

    attr_reader :filename

    def initialize(file)
      @file = file
    end

    # @return [bool] Always false
    def empty?
      false
    end

    # @return [Hash] A hash consisting of filename and io object
    def to_hash
      { filename: @filename, data: @file }
    end
    alias to_h to_hash

    # @return [String] Formatted string with the class name, object id and filename.
    def inspect
      "#<#{self.class}:#{object_id} {filename: #{filename.inspect}}>"
    end

    def method_missing(name, *args, &block)
      if @file.respond_to?(name)
        @file.send(name, *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      super || @file.respond_to?(method_name, include_private)
    end

    # Parse filename from the 'Content Disposition' header.
    def parse_headers!(headers)
      @filename = headers[HEADER_CONTENT_DISPOSITION].split('filename=')[1]
      @filename = @filename[1...-1] if @filename[0] == '"' # Unquote filenames
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
gitlab-5.1.0 lib/gitlab/file_response.rb
gitlab-5.0.0 lib/gitlab/file_response.rb
gitlab-4.20.1 lib/gitlab/file_response.rb
gitlab-4.20.0 lib/gitlab/file_response.rb
fs-gitlab-4.19.3 lib/gitlab/file_response.rb
fs-gitlab-4.19.2 lib/gitlab/file_response.rb
fs-gitlab-4.19.1 lib/gitlab/file_response.rb
gitlab-4.19.0 lib/gitlab/file_response.rb
fs-gitlab-4.18.2 lib/gitlab/file_response.rb
fs-gitlab-4.18.1 lib/gitlab/file_response.rb
gitlab-4.18.0 lib/gitlab/file_response.rb
gitlab-4.17.0 lib/gitlab/file_response.rb
gitlab-4.16.1 lib/gitlab/file_response.rb
gitlab-4.16.0 lib/gitlab/file_response.rb
gitlab-4.15.0 lib/gitlab/file_response.rb
gitlab-4.14.1 lib/gitlab/file_response.rb
gitlab-4.14.0 lib/gitlab/file_response.rb
gitlab-4.13.1 lib/gitlab/file_response.rb
gitlab-4.13.0 lib/gitlab/file_response.rb
gitlab-4.12.0 lib/gitlab/file_response.rb