Sha256: c289eaab7a39d1234d9380bae82e15c87751adb5bb31256d8eb9d3d93b7f1f98

Contents?: true

Size: 778 Bytes

Versions: 2

Compression:

Stored size: 778 Bytes

Contents

module Nifty
  module Attachments
    class Middleware

      def initialize(app)
        @app = app
      end

      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

    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/nifty-attachments-1.0.4/lib/nifty/attachments/middleware.rb
nifty-attachments-1.0.4 lib/nifty/attachments/middleware.rb