Class: ActiveStorage::Ocr::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/activestorage/ocr/configuration.rb

Overview

Configuration options for the OCR module.

Settings can be configured via environment variables or the configure block:

ActiveStorage::Ocr.configure do |config|
config.server_url = "http://localhost:9292"
config.timeout = 60
config.engine = :leptess  # Use Tesseract engine instead of default ocrs
config.preprocess = :aggressive  # Use aggressive preprocessing
end

Environment Variables

  • ACTIVESTORAGE_OCR_SERVER_URL - OCR server URL (default: http://localhost:9292)
  • ACTIVESTORAGE_OCR_TIMEOUT - Request timeout in seconds (default: 30)
  • ACTIVESTORAGE_OCR_OPEN_TIMEOUT - Connection timeout in seconds (default: 5)
  • ACTIVESTORAGE_OCR_ENGINE - OCR engine to use: ocrs (default) or leptess
  • ACTIVESTORAGE_OCR_PREPROCESS - Preprocessing preset: none, minimal, default, aggressive

Constant Summary collapse

VALID_ENGINES =

Valid OCR engine names

%i[ocrs leptess].freeze
VALID_PREPROCESS =

Valid preprocessing preset names

%i[none minimal default aggressive].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Configuration

Creates a new Configuration with default values.

Defaults are read from environment variables if set.



56
57
58
59
60
61
62
63
# File 'lib/activestorage/ocr/configuration.rb', line 56

def initialize
  @server_url = ENV.fetch("ACTIVESTORAGE_OCR_SERVER_URL", "http://localhost:9292")
  @timeout = ENV.fetch("ACTIVESTORAGE_OCR_TIMEOUT", 30).to_i
  @open_timeout = ENV.fetch("ACTIVESTORAGE_OCR_OPEN_TIMEOUT", 5).to_i
  @content_types = default_content_types
  self.engine = ENV.fetch("ACTIVESTORAGE_OCR_ENGINE", "ocrs").to_sym
  self.preprocess = ENV.fetch("ACTIVESTORAGE_OCR_PREPROCESS", "default").to_sym
end

Instance Attribute Details

#content_types ⇒ Object

Array of MIME types that the analyzer will process.



41
42
43
# File 'lib/activestorage/ocr/configuration.rb', line 41

def content_types
  @content_types
end

#engine ⇒ Object

The OCR engine to use (:ocrs or :leptess). Default is :ocrs (pure Rust, no dependencies). Use :leptess for Tesseract-based OCR (better for messy images).



46
47
48
# File 'lib/activestorage/ocr/configuration.rb', line 46

def engine
  @engine
end

#open_timeout ⇒ Object

Connection open timeout in seconds.



38
39
40
# File 'lib/activestorage/ocr/configuration.rb', line 38

def open_timeout
  @open_timeout
end

#preprocess ⇒ Object

The preprocessing preset to use (:none, :minimal, :default, :aggressive). Default is :default. Use :none to skip preprocessing, :aggressive for poor quality images.



51
52
53
# File 'lib/activestorage/ocr/configuration.rb', line 51

def preprocess
  @preprocess
end

#server_url ⇒ Object

The URL of the OCR server.



32
33
34
# File 'lib/activestorage/ocr/configuration.rb', line 32

def server_url
  @server_url
end

#timeout ⇒ Object

Request timeout in seconds.



35
36
37
# File 'lib/activestorage/ocr/configuration.rb', line 35

def timeout
  @timeout
end

Instance Method Details

#accept_content_type?(content_type) ⇒ Boolean

Checks if the given content type is supported.

Parameters

  • content_type - A MIME type string (e.g., "image/png")

Returns

true if the content type is in the supported list, false otherwise.

Returns:

  • (Boolean)


126
127
128
# File 'lib/activestorage/ocr/configuration.rb', line 126

def accept_content_type?(content_type)
  content_types.include?(content_type)
end

#default_content_types ⇒ Object

Returns the default list of supported content types.

Includes common image formats and PDF.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/activestorage/ocr/configuration.rb', line 105

def default_content_types
  %w[
    image/png
    image/jpeg
    image/gif
    image/bmp
    image/webp
    image/tiff
    application/pdf
  ]
end