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.



8
9
10
# File 'lib/attach/middleware.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  unless env['PATH_INFO'] =~ /\A\/attachment\/([a-f0-9-]{36})\/(.*)/
    return @app.call(env)
  end

  attachment = Attach::Attachment.where(serve: true).find_by(token: Regexp.last_match(1))
  if attachment.nil?
    return [404, {}, ['Attachment not found']]
  end

  [200, headers_for_attachment(attachment), [attachment.blob.read]]
end