class Racket::Helpers::File::Response
Class for sending files.
Public Class Methods
new(utils, file, options)
click to toggle source
# File lib/racket/helpers/file.rb, line 26 def initialize(utils, file, options) @utils = utils @file = @utils.build_path(file) @options = options @response = Racket::Response.new build end
Public Instance Methods
to_a()
click to toggle source
Returns the current object as a Rack response array.
@return [Array]
# File lib/racket/helpers/file.rb, line 37 def to_a @response.to_a end
Private Instance Methods
build()
click to toggle source
# File lib/racket/helpers/file.rb, line 43 def build if Racket::Utils::FileSystem.file_readable?(@file) then build_success else build_failure end end
build_failure()
click to toggle source
# File lib/racket/helpers/file.rb, line 49 def build_failure @response.status = 404 @response.headers['Content-Type'] = 'text/plain' @response.write(Rack::Utils::HTTP_STATUS_CODES[@response.status]) end
build_success()
click to toggle source
# File lib/racket/helpers/file.rb, line 55 def build_success @response.status = 200 set_mime_type set_content_disposition @response.write(::File.read(@file)) end
set_content_disposition()
click to toggle source
# File lib/racket/helpers/file.rb, line 62 def set_content_disposition # Set Content-Disposition (and a file name) if the file should be downloaded # instead of displayed inline. return unless @options.fetch(:download, false) content_disposition = 'attachment' filename = @options.fetch(:filename, nil).to_s content_disposition << format('; filename="%s"', filename) unless filename.empty? @response.headers['Content-Disposition'] = content_disposition end
set_mime_type()
click to toggle source
# File lib/racket/helpers/file.rb, line 72 def set_mime_type mime_type = @options.fetch(:mime_type, nil) # Calculate MIME type if it was not already specified. mime_type = Rack::Mime.mime_type(::File.extname(@file)) unless mime_type @response.headers['Content-Type'] = mime_type end