Class: Nifty::Attachments::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/nifty/attachments/middleware.rb

Instance Method Summary collapse

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 attachment = Nifty::Attachments::Attachment.find_by_token($1)
      [200, {
        'Content-Length' => attachment.data.bytesize.to_s,
        'Content-Type' => attachment.file_type,
        'Cache-Control' => "public, maxage=#{1.year.to_i}",
        'Content-Disposition' => "attachment; filename=\"#{attachment.file_name}\""
        },
      [attachment.data]]
    else
      [404, {}, ["Attachment not found"]]
    end
  else
    @app.call(env)
  end
end