Sha256: 6766e7a9dbf833413da33d5981042fb817429d1b58d87b324a47bedaefa8f7f6

Contents?: true

Size: 1.82 KB

Versions: 28

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8
module SimpleCaptcha
  class Middleware
    include SimpleCaptcha::ImageHelpers
    
    DEFAULT_SEND_FILE_OPTIONS = {
      :type         => 'application/octet-stream'.freeze,
      :disposition  => 'attachment'.freeze,
    }.freeze
    
    def initialize(app, options={})
      @app = app
      self
    end
    
    def call(env) # :nodoc:
      if env["REQUEST_METHOD"] == "GET" && captcha_path?(env['PATH_INFO'])
        make_image(env)
      else
        @app.call(env)
      end
    end
    
    protected
      def make_image(env, headers = {}, status = 404)
        request = Rack::Request.new(env)
        code = request.params["code"]
        body = []
        
        if !code.blank? && Utils::simple_captcha_value(code)
          #status, headers, body = @app.call(env)
          #status = 200
          #body = generate_simple_captcha_image(code)
          #headers['Content-Type'] = 'image/jpeg'
          
          return send_file(generate_simple_captcha_image(code), :type => 'image/jpeg', :disposition => 'inline', :filename =>  'simple_captcha.jpg')
        end
        
        [status, headers, body]
      end
      
      def captcha_path?(request_path)
        request_path.include?('/simple_captcha')
      end
      
      def send_file(path, options = {})
        raise MissingFile, "Cannot read file #{path}" unless File.file?(path) and File.readable?(path)

        options[:filename] ||= File.basename(path) unless options[:url_based_filename]

        status = options[:status] || 200
        headers = {"Content-Disposition" => "#{options[:disposition]}; filename='#{options[:filename]}'", "Content-Type" => options[:type], 'Content-Transfer-Encoding' => 'binary', 'Cache-Control' => 'private'}
        response_body = File.open(path, "rb")
        
        [status, headers, response_body]
      end
  end
end

Version data entries

28 entries across 28 versions & 9 rubygems

Version Path
simple_captcha2-0.3.1 lib/simple_captcha/middleware.rb
simple_captcha2-0.2.2 lib/simple_captcha/middleware.rb
simple_captcha2-0.3.0 lib/simple_captcha/middleware.rb
simple_captcha2-0.2.1 lib/simple_captcha/middleware.rb
simple_captcha2-0.2.0 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.5.2 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.5.1 lib/simple_captcha/middleware.rb
pludoni-simple_captcha-0.1.6 lib/simple_captcha/middleware.rb
galetahub-simple_captcha-0.1.5 lib/simple_captcha/middleware.rb
galetahub-simple_captcha-0.1.4 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.4.6 lib/simple_captcha/middleware.rb
loyal_simple_captcha-0.0.1 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.4.5 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.4.4 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.4.3 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.4.1 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.3.1 lib/simple_captcha/middleware.rb
glebtv-simple_captcha-0.1.6 lib/simple_captcha/middleware.rb
foco-simple_captcha-0.1.3 lib/simple_captcha/middleware.rb
nahaylo-simple_captcha-0.2.0 lib/simple_captcha/middleware.rb