Class: Attach::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
# File 'lib/attach/middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/attach/middleware.rb', line 10

def call(env)
  if env['PATH_INFO'] =~ /\A\/attachment\/([a-f0-9\-]{36})\/(.*)/
    if attachment = Attach::Attachment.find_by_token($1)
      [200, {
        'Content-Length' => attachment.file_size.to_s,
        'Content-Type' => attachment.file_type,
        'Cache-Control' => "#{attachment.cache_type || 'private'}, immutable, maxage=#{attachment.cache_max_age || 30.days.to_i}",
        'Content-Disposition' => "#{attachment.disposition || 'attachment'}; filename=\"#{attachment.file_name}\","
        },
      [attachment.binary]]
    else
      [404, {}, ["Attachment not found"]]
    end
  else
    @app.call(env)
  end
end