Class: FileServer

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffold/lib/middleware/file_server.rb

Constant Summary collapse

MIME_TYPES =
{
  '.txt' => 'text/plain',
  '.jpg' => 'image/jpeg',
  '.zip' => 'application/zip'
}

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileServer

Returns a new instance of FileServer.



9
10
11
# File 'lib/scaffold/lib/middleware/file_server.rb', line 9

def initialize(root)
  @root = root
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scaffold/lib/middleware/file_server.rb', line 13

def call(env)
  res = Rack::Response.new
  file_name = requested_file_name(env)

  if File.exist?(file_name)
    serve_file(file_name, res)
  else
    res.status = 404
    res.write("File not found")
  end
  res
end