Class: Useless::Rack::Base::Files
- Inherits:
-
Object
- Object
- Useless::Rack::Base::Files
- Defined in:
- lib/useless/rack/base/files.rb
Overview
Useless::Base::Files simply retrieves files from Useless::FS and serves them up.
Class Method Summary collapse
-
.url_for(id) ⇒ Object
Provide a helper method for file URLs to keep things consistent.
Instance Method Summary collapse
Class Method Details
.url_for(id) ⇒ Object
Provide a helper method for file URLs to keep things consistent.
10 11 12 |
# File 'lib/useless/rack/base/files.rb', line 10 def self.url_for(id) "http://useless.io/files/#{id}" end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/useless/rack/base/files.rb', line 14 def call(env) # The file ID is everything after the initial '/' in the path id = env['PATH_INFO'][1..-1] # Retrieve the file from FS file = env['useless.fs'].get(BSON::ObjectId(id)) # and serve it up with the associated content type return [200, {'Content-Type' => file.content_type}, file.read] # Two things can go wrong, and they'll both raise an error: # * the specified ID is not a valid object ID # * there is no file corresponding to the ID in the FS rescue BSON::InvalidObjectId, Useless::FS::FileNotFound [404, {'Content-Type' => 'text/plain'}, "File not found: #{id}"] end |