File: file_response.rb

package info (click to toggle)
ruby-gitlab 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,660 kB
  • sloc: ruby: 12,582; makefile: 7; sh: 4
file content (48 lines) | stat: -rw-r--r-- 1,208 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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