Class: Nifty::Attachments::Middleware
- Inherits:
-
Object
- Object
- Nifty::Attachments::Middleware
- Defined in:
- lib/nifty/attachments/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
5 6 7 |
# File 'lib/nifty/attachments/middleware.rb', line 5 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/nifty/attachments/middleware.rb', line 9 def call(env) if env['PATH_INFO'] =~ /\A\/attachment\/([a-f0-9\-]{36})\/(.*)/ if = Nifty::Attachments::Attachment.find_by_token($1) [200, { 'Content-Length' => .data.bytesize.to_s, 'Content-Type' => .file_type, 'Cache-Control' => "public, maxage=#{1.year.to_i}", 'Content-Disposition' => "attachment; filename=\"#{.file_name}\"" }, [.data]] else [404, {}, ["Attachment not found"]] end else @app.call(env) end end |