Module: Shrine::Plugins::ValidateCorrectForm::AttacherMethods

Defined in:
lib/shrine/plugins/validate_correct_form.rb

Constant Summary collapse

WRONG_FORM =
'wrong_form'

Instance Method Summary collapse

Instance Method Details

#validate_correct_form(form_id: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/shrine/plugins/validate_correct_form.rb', line 11

def validate_correct_form(form_id: nil)
  return unless get.mime_type == Mime[:pdf].to_s && form_id

  image_path = Rails.root.join("#{Common::FileHelpers.random_file_path}.jpg").to_s
  file = get.download
  pdf = MiniMagick::Image.open(file.path)
  MiniMagick::Tool::Convert.new do |convert|
    convert.background 'white'
    convert.flatten
    convert.density 150
    convert.quality 100
    convert << pdf.pages.first.path
    convert << image_path
  end
  file_as_string = RTesseract.new(image_path).to_s

  record.warnings << WRONG_FORM unless file_as_string.include? form_id
end