Added file cache, can setup how many images you want generate by ,

RuCaptcha will use cache for next requests.
  When you restart Rails processes it will generate new again and clean the old caches.
This commit is contained in:
Jason Lee committed 2015-10-30 11:32:02 +08:00
1 parent 13cd85dc6e
commit c46f88e84f
15 files changed
+204 -78

No files matched your search

+13 -7
View File
@@ -5,6 +5,7 @@ require_relative 'rucaptcha/version'
require_relative 'rucaptcha/configuration'
require_relative 'rucaptcha/controller_helpers'
require_relative 'rucaptcha/view_helpers'
require_relative 'rucaptcha/cache'
require_relative 'rucaptcha/captcha'
require_relative 'rucaptcha/engine'
@@ -13,19 +14,24 @@ module RuCaptcha
def config
return @config if defined?(@config)
@config = Configuration.new
@config.len = 4
@config.width = 150
@config.height = 48
@config.implode = 0.4
@config.len = 4
@config.width = 150
@config.height = 48
@config.implode = 0.4
@config.cache_limit = 100
@config
end
def configure(&block)
config.instance_exec(&block)
# enable cache if cache_limit less than 1
if config.cache_limit >= 1
RuCaptcha::Captcha.send(:include, RuCaptcha::Cache)
end
end
end
end
ActionController::Base.send :include, RuCaptcha::ControllerHelpers
ActionView::Base.send :include, RuCaptcha::ViewHelpers
ActionController::Base.send(:include, RuCaptcha::ControllerHelpers)
ActionView::Base.send(:include, RuCaptcha::ViewHelpers)
+44
View File
@@ -0,0 +1,44 @@
module RuCaptcha
# File Cache
module Cache
extend ActiveSupport::Concern
included do
class << self
alias_method_chain :create, :cache
alias_method_chain :random_chars, :cache
end
end
module ClassMethods
def create_with_cache(code)
cache.fetch(code) do
create_without_cache(code)
end
end
def random_chars_with_cache
if cached_codes.length >= RuCaptcha.config.cache_limit
return cached_codes.sample
else
code = random_chars_without_cache
cached_codes << code
return code
end
end
def cache
return @cache if defined?(@cache)
cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha')
@cache = ActiveSupport::Cache::FileStore.new(cache_path)
@cache.clear
@cache
end
def cached_codes
@cached_codes ||= []
end
end
end
end
+44 -35
View File
@@ -2,44 +2,53 @@ require 'posix-spawn'
module RuCaptcha
class Captcha
def self.rand_color
r = rand(129).to_s(8).to_i
rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
class << self
def rand_color
rgb = [rand(100).to_s(8), rand(100).to_s(8), rand(100).to_s(8)]
"rgba(#{rgb.join(',')},1)"
end
def self.create(code)
size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
font_size = (RuCaptcha.config.height * 0.8).to_i
half_width = RuCaptcha.config.width / 2
half_height = RuCaptcha.config.height / 2
line_color = rand_color
chars = code.split('')
text_opts = []
text_top = (RuCaptcha.config.height - font_size) / 2
text_padding = 5
text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2
text_left = 5
chars.each_with_index do |char, i|
text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"')
"rgba(#{rgb.join(',')},1)"
end
command = <<-CODE
convert -size #{size} #{text_opts.join(' ')} \
-draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \
-draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
-draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
-wave #{rand(2) + 2}x#{rand(2) + 1} \
-gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \
-implode #{RuCaptcha.config.implode} label:- png:-
CODE
command.strip!
# puts command
pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
Process.waitpid(pid)
stdout.read
def random_chars
chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
chars
end
# Create Captcha image by code
def create(code)
size = "#{RuCaptcha.config.width}x#{RuCaptcha.config.height}"
font_size = (RuCaptcha.config.height * 0.8).to_i
half_width = RuCaptcha.config.width / 2
half_height = RuCaptcha.config.height / 2
line_color = rand_color
chars = code.split('')
text_opts = []
text_top = (RuCaptcha.config.height - font_size) / 2
text_padding = 5
text_width = (RuCaptcha.config.width / chars.size) - text_padding * 2
text_left = 5
chars.each_with_index do |char, i|
text_opts << %(-fill '#{rand_color}' -draw 'text #{(text_left + text_width) * i + text_left},#{text_top} "#{char}"')
end
command = <<-CODE
convert -size #{size} #{text_opts.join(' ')} \
-draw 'stroke #{line_color} line #{rand(10)},#{rand(20)} #{half_width + rand(half_width)},#{rand(half_height)}' \
-draw 'stroke #{line_color} line #{rand(10)},#{rand(25)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
-draw 'stroke #{line_color} line #{rand(10)},#{rand(30)} #{half_width + rand(half_width)},#{half_height + rand(half_height)}' \
-wave #{rand(2) + 2}x#{rand(2) + 1} \
-gravity NorthWest -sketch 1x10+#{rand(1)} -pointsize #{font_size} -weight 700 \
-implode #{RuCaptcha.config.implode} label:- png:-
CODE
command.strip!
# puts command
pid, stdin, stdout, stderr = POSIX::Spawn.popen4(command)
Process.waitpid(pid)
stdout.read
end
end
end
end
+11 -1
View File
@@ -1,5 +1,15 @@
module RuCaptcha
class Configuration
attr_accessor :width, :height, :font_size, :len, :implode
# Image width, default 150
attr_accessor :width
# Image height, default 48
attr_accessor :height
# Number of chars, default 4
attr_accessor :len
# implode, default 0.4
attr_accessor :implode
# Number of Captcha codes limit
# set 0 to disable limit and file cache, default: 100
attr_accessor :cache_limit
end
end
+2 -8
View File
@@ -7,14 +7,8 @@ module RuCaptcha
end
def generate_rucaptcha
session[:_rucaptcha] = random_rucaptcha_chars
return RuCaptcha::Captcha.create(session[:_rucaptcha])
end
def random_rucaptcha_chars
chars = SecureRandom.hex(RuCaptcha.config.len / 2).downcase
chars.gsub!(/[0ol1]/i, (rand(8) + 2).to_s)
chars
session[:_rucaptcha] = RuCaptcha::Captcha.random_chars
RuCaptcha::Captcha.create(session[:_rucaptcha])
end
def verify_rucaptcha?(resource = nil)
+1 -1
View File
@@ -1,3 +1,3 @@
module RuCaptcha
VERSION = '0.1.4'
VERSION = '0.2.0'
end