class Utopia::Static

A middleware which serves static files from the specified root directory.

A middleware which serves static files from the specified root directory.

A middleware which serves static files from the specified root directory.

Constants

ACCEPT_RANGES
CACHE_CONTROL
CONTENT_TYPE
DEFAULT_CACHE_CONTROL
ETAG
LAST_MODIFIED
MIME_TYPES

Default mime-types which are common for files served over HTTP:

Attributes

extensions[R]

Public Class Methods

new(app, root: Utopia::default_root, types: MIME_TYPES[:default], cache_control: DEFAULT_CACHE_CONTROL) click to toggle source

@param root [String] The root directory to serve files from. @param types [Array] The mime-types (and file extensions) to recognize/serve. @param cache_control [String] The cache-control header to set for static content.

# File lib/utopia/static.rb, line 37
def initialize(app, root: Utopia::default_root, types: MIME_TYPES[:default], cache_control: DEFAULT_CACHE_CONTROL)
        @app = app
        @root = root
        
        @extensions = MimeTypeLoader.extensions_for(types)
        
        @cache_control = cache_control
end

Public Instance Methods

call(env) click to toggle source
# File lib/utopia/static.rb, line 75
def call(env)
        path_info = env[Rack::PATH_INFO]
        extension = File.extname(path_info)

        if @extensions.key? extension.downcase
                path = Path[path_info].simplify
                
                if locale = env[Localization::CURRENT_LOCALE_KEY]
                        path.last.insert(path.last.rindex('.') || -1, ".#{locale}")
                end
                
                if file = fetch_file(path)
                        response_headers = {
                                LAST_MODIFIED => file.mtime_date,
                                CONTENT_TYPE => @extensions[extension],
                                CACHE_CONTROL => @cache_control,
                                ETAG => file.etag,
                                ACCEPT_RANGES => "bytes"
                        }

                        if file.modified?(env)
                                return file.serve(env, response_headers)
                        else
                                return [304, response_headers, []]
                        end
                end
        end

        # else if no file was found:
        return @app.call(env)
end
fetch_file(path) click to toggle source
# File lib/utopia/static.rb, line 56
def fetch_file(path)
        # We need file_path to be an absolute path for X-Sendfile to work correctly.
        file_path = File.join(@root, path.components)
        
        if File.exist?(file_path)
                return LocalFile.new(@root, path)
        else
                return nil
        end
end
freeze() click to toggle source
Calls superclass method
# File lib/utopia/static.rb, line 46
def freeze
        return self if frozen?
        
        @root.freeze
        @extensions.freeze
        @cache_control.freeze
        
        super
end