Class: FormAttachment

Inherits:
ApplicationRecord show all
Includes:
SentryLogging, SetGuid
Defined in:
app/models/form_attachment.rb

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Methods inherited from ApplicationRecord

descendants_using_encryption, lockbox_options, #timestamp_attributes_for_update_in_model, #valid?

Instance Method Details

#get_attachment_uploaderObject (private)



60
61
62
# File 'app/models/form_attachment.rb', line 60

def get_attachment_uploader
  @au ||= self.class::ATTACHMENT_UPLOADER_CLASS.new(guid)
end

#get_fileObject



28
29
30
31
32
33
34
# File 'app/models/form_attachment.rb', line 28

def get_file
  attachment_uploader = get_attachment_uploader
  attachment_uploader.retrieve_from_store!(
    parsed_file_data['filename']
  )
  attachment_uploader.file
end

#parsed_file_dataObject



24
25
26
# File 'app/models/form_attachment.rb', line 24

def parsed_file_data
  @parsed_file_data ||= JSON.parse(file_data)
end

#set_file_data!(file, file_password = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'app/models/form_attachment.rb', line 14

def set_file_data!(file, file_password = nil)
  attachment_uploader = get_attachment_uploader
  file = unlock_pdf(file, file_password) if File.extname(file).downcase == '.pdf' && file_password.present?
  attachment_uploader.store!(file)
  self.file_data = { filename: attachment_uploader.filename }.to_json
rescue CarrierWave::IntegrityError => e
  log_exception_to_sentry(e, nil, nil, 'warn')
  raise Common::Exceptions::UnprocessableEntity.new(detail: e.message, source: 'FormAttachment.set_file_data')
end

#unlock_pdf(file, file_password) ⇒ Object (private)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/form_attachment.rb', line 38

def unlock_pdf(file, file_password)
  pdftk = PdfForms.new(Settings.binaries.pdftk)
  tmpf = Tempfile.new(['decrypted_form_attachment', '.pdf'])

  begin
    pdftk.call_pdftk(file.tempfile.path, 'input_pw', file_password, 'output', tmpf.path)
  rescue PdfForms::PdftkError => e
    file_regex = %r{/(?:\w+/)*[\w-]+\.pdf\b}i
    password_regex = /(input_pw).*?(output)/
    sanitized_message = e.message.gsub(file_regex, '[FILTERED FILENAME]').gsub(password_regex, '\1 [FILTERED] \2')
    log_message_to_sentry(sanitized_message, 'warn')
    raise Common::Exceptions::UnprocessableEntity.new(
      detail: I18n.t('errors.messages.uploads.pdf.incorrect_password'),
      source: 'FormAttachment.unlock_pdf'
    )
  end

  file.tempfile.unlink
  file.tempfile = tmpf
  file
end