<commit_before>class AutomateImportService
  def cancel_import(import_file_upload_id)
    import_file_upload = ImportFileUpload.find(import_file_upload_id)

    destroy_queued_deletion(import_file_upload.id)
    import_file_upload.destroy
  end

  def import_datastore(import_file_upload,
                       domain_name_to_import_from,
                       domain_name_to_import_to,
                       namespace_or_class_list)
    File.open("automate_temporary_zip.zip", "wb") { |file| file.write(import_file_upload.binary_blob.binary) }

    import_options = {
      "import_as" => domain_name_to_import_to.presence || domain_name_to_import_from,
      "overwrite" => true,
      "zip_file"  => "automate_temporary_zip.zip",
      "tenant_id" => User.current_user.current_tenant.id
    }
    ae_import = MiqAeImport.new(domain_name_to_import_from, import_options)

    namespace_list = namespace_or_class_list.reject do |namespace_or_class|
      namespace_or_class.match(/\.class/)
    end

    class_list = namespace_or_class_list.select do |namespace_or_class|
      namespace_or_class.match(/\.class/)
    end

    ae_import.remove_unrelated_entries(domain_name_to_import_from)
    reject_unrelated_namespaces(ae_import, domain_name_to_import_from, namespace_list)
    reject_unrelated_classes(ae_import, domain_name_to_import_from, class_list)

    result = ae_import.import

    result.nil? ? nil : ae_import.import_stats
    ensure
      File.delete("automate_temporary_zip.zip")
  end

  def store_for_import(file_contents)
    import_file_upload = ImportFileUpload.create
    import_file_upload.store_binary_data_as_yml(file_contents, "Automation import")
    import_file_upload
  ensure
    queue_deletion(import_file_upload.id)
  end

  private
<commit_msg>Refactoring ae_import temporary file system.<commit_after>class AutomateImportService
  def cancel_import(import_file_upload_id)
    import_file_upload = ImportFileUpload.find(import_file_upload_id)

    destroy_queued_deletion(import_file_upload.id)
    import_file_upload.destroy
  end

  def import_datastore(import_file_upload,
                       domain_name_to_import_from,
                       domain_name_to_import_to,
                       namespace_or_class_list)
    Tempfile.open(['automate_temporary_zip', '.zip']) do |zip_file|
      zip_file.binmode
      zip_file.write(import_file_upload.binary_blob.binary)
      zip_file.flush

      import_options = {
        "import_as" => domain_name_to_import_to.presence || domain_name_to_import_from,
        "overwrite" => true,
        "zip_file"  => zip_file.path,
        "tenant_id" => User.current_user.current_tenant.id
      }
      ae_import = MiqAeImport.new(domain_name_to_import_from, import_options)

      namespace_list = namespace_or_class_list.select do |namespace_or_class|
        namespace_or_class.match(/\.class/)
      end

      class_list = namespace_or_class_list.select do |namespace_or_class|
        namespace_or_class.match(/\.class/)
      end

      ae_import.remove_unrelated_entries(domain_name_to_import_from)
      reject_unrelated_namespaces(ae_import, domain_name_to_import_from, namespace_list)
      reject_unrelated_classes(ae_import, domain_name_to_import_from, class_list)

      result = ae_import.import

      result.nil? ? nil : ae_import.import_stats
    end
  end

  def store_for_import(file_contents)
    import_file_upload = ImportFileUpload.create
    import_file_upload.store_binary_data_as_yml(file_contents, "Automation import")
    import_file_upload
  ensure
    queue_deletion(import_file_upload.id)
  end

  private

{'fraction_non_alphanumeric': 0.07099447513812154, 'fraction_numerical': 0.0, 'mean_word_length': 3.8669354838709675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3939120', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1167, 'n_words': 146}