Class: Utopia::Middleware::Static
- Inherits:
-
Object
- Object
- Utopia::Middleware::Static
- Defined in:
- lib/utopia/middleware/static.rb
Defined Under Namespace
Classes: FileReader
Constant Summary collapse
- MIME_TYPES =
{ :xiph => { "ogx" => "application/ogg", "ogv" => "video/ogg", "oga" => "audio/ogg", "ogg" => "audio/ogg", "spx" => "audio/ogg", "flac" => "audio/flac", "anx" => "application/annodex", "axa" => "audio/annodex", "xspf" => "application/xspf+xml", }, :media => [ :xiph, "mp3", "mp4", "wav", "aiff", ["aac", "audio/x-aac"], "mov", "avi", "wmv", "mpg" ], :text => [ "html", "css", "js", "txt", "rtf", "xml", "pdf" ], :archive => [ "zip", "tar", "tgz", "tar.gz", "tar.bz2", ["dmg", "application/x-apple-diskimage"], ["torrent", "application/x-bittorrent"] ], :images => [ "png", "gif", "jpeg", "tiff" ], :default => [ :media, :text, :archive, :images ] }
Instance Attribute Summary collapse
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #fetch_file(path) ⇒ Object
-
#initialize(app, options = {}) ⇒ Static
constructor
A new instance of Static.
- #lookup_relative_file(path) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Static
Returns a new instance of Static.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/utopia/middleware/static.rb', line 134 def initialize(app, = {}) @app = app @root = [:root] || Utopia::Middleware::default_root if [:types] @extensions = load_mime_types([:types]) else @extensions = load_mime_types(MIME_TYPES[:default]) end @cache_control = [:cache_control] || "public, max-age=3600" LOG.info "** #{self.class.name}: Running in #{@root} with #{extensions.size} filetypes" # LOG.info @extensions.inspect end |
Instance Attribute Details
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
191 192 193 |
# File 'lib/utopia/middleware/static.rb', line 191 def extensions @extensions end |
Instance Method Details
#call(env) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/utopia/middleware/static.rb', line 193 def call(env) request = Rack::Request.new(env) ext = File.extname(request.path_info) if @extensions.key? ext.downcase path = Path.create(request.path_info).simplify if file = fetch_file(path) response_headers = { "Last-Modified" => file.mtime_date, "Content-Type" => @extensions[ext], "Cache-Control" => @cache_control, "ETag" => file.etag } if file.modified?(env) response_headers["Content-Length"] = file.size.to_s return [200, response_headers, file] else return [304, response_headers, []] end elsif redirect = lookup_relative_file(path) return [307, {"Location" => redirect}, []] end end # else if no file was found: return @app.call(env) end |
#fetch_file(path) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/utopia/middleware/static.rb', line 150 def fetch_file(path) file_path = File.join(@root, path.components) if File.exist?(file_path) return FileReader.new(file_path) else return nil end end |
#lookup_relative_file(path) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/utopia/middleware/static.rb', line 159 def lookup_relative_file(path) file = nil name = path.basename if split = path.split("@rel@") path = split[0] name = split[1].components # Fix a problem if the browser request has multiple @rel@ # This normally indicates a browser bug.. :( name.delete("@rel@") else path = path.dirname # Relative lookups are not done unless explicitly required by @rel@ # ... but they do work. This is a performance optimization. return nil end # LOG.debug("Searching for #{name.inspect} starting in #{path.components}") path.ascend do |parent_path| file_path = File.join(@root, parent_path.components, name) # LOG.debug("File path: #{file_path}") if File.exist?(file_path) return (parent_path + name).to_s end end return nil end |