<commit_before>require "virus_scanner"

module VirusScannerCallbacks
  def mounted_file_namespace
    if self.class.to_s == "FormAnswerAttachment"
      "file"
    else
      "attachment"
    end
  end

  def foreign_key_id
    self.class.to_s.underscore + "_id"
  end

  def common_attrs
    ops = {
      filename: self.send(mounted_file_namespace).current_path,
      uuid: SecureRandom.uuid,
      status: (ENV["DISABLE_VIRUS_SCANNER"] == "true" ? "clean" : "scanning")
    }

    ops[foreign_key_id.to_sym] = self.id

    ops
  end

  def virus_scan
    unless Rails.env.test?
      scan = Scan.create!(common_attrs)

      unless ENV["DISABLE_VIRUS_SCANNER"] == "true"
        response = ::VirusScanner::File.scan_url(
          scan.uuid,
          self.send(mounted_file_namespace).url)

        scan.update(vs_id: response["id"], status: response["status"])
      end
    end
  end
end<commit_msg>[QAE1179] VirusScanner: rspec fixes<commit_after>require "virus_scanner"

module VirusScannerCallbacks
  def mounted_file_namespace
    if self.class.to_s == "FormAnswerAttachment"
      "file"
    else
      "attachment"
    end
  end

  def foreign_key_id
    self.class.to_s.underscore + "_id"
  end

  def common_attrs
    ops = {
      filename: self.send(mounted_file_namespace).current_path,
      uuid: SecureRandom.uuid,
      status: (ENV["DISABLE_VIRUS_SCANNER"] == "true" ? "clean" : "scanning")
    }

    if Rails.env.test? && ops[:filename].blank?
      # In Tests we can do not upload real files
      # So, this stub is fixing validation errors in tests
      ops[:filename] = "test.jpg"
    end

    ops[foreign_key_id.to_sym] = self.id

    ops
  end

  def virus_scan
    scan = Scan.create!(common_attrs)

    unless ENV["DISABLE_VIRUS_SCANNER"] == "true"
      response = ::VirusScanner::File.scan_url(
        scan.uuid,
        self.send(mounted_file_namespace).url)

      scan.update(vs_id: response["id"], status: response["status"])
    end
  end
end
{'fraction_non_alphanumeric': 0.10987903225806452, 'fraction_numerical': 0.0020161290322580645, 'mean_word_length': 2.954183266932271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4660868', 'n_tokens_mistral': 696, 'n_tokens_neox': 665, 'n_words': 135}