<commit_before># encoding: UTF-8

require 'fileutils'
require 'active_record'
require_relative '../../services/carto/visualizations_export_service_2'
require_relative '../../../services/sql-api/sql_api'
require_relative './visualization'
require_dependency 'carto/visualization_exporter'

module Carto
  class VisualizationExport < ::ActiveRecord::Base
    include VisualizationExporter
    include Carto::VisualizationsExportService2Validator

    belongs_to :visualization, class_name: Carto::Visualization
    belongs_to :user, class_name: Carto::User
    belongs_to :log, class_name: Carto::Log

    validate :visualization_exportable_by_user?, if: :new_record?
    validate :user_tables_ids_valid?, if: :new_record?
    validate :valid_visualization_type?, if: :new_record?

    STATE_PENDING = 'pending'.freeze
    STATE_EXPORTING = 'exporting'.freeze
    STATE_UPLOADING = 'uploading'.freeze
    STATE_COMPLETE = 'complete'.freeze
    STATE_FAILURE = 'failure'.freeze

    def exporter_folder
      ensure_folder(exporter_config['exporter_temporal_folder'] || DEFAULT_EXPORTER_TMP_FOLDER)
    end

    def run_export!(file_upload_helper: default_file_upload_helper, download_path: nil)
      logger = Carto::Log.new(type: 'visualization_export')

      logger.append('Exporting')
      update_attributes(state: STATE_EXPORTING, log: logger)
      filepath = export(visualization, user, user_tables_ids: user_tables_ids)

      logger.append('Uploading')
      update_attributes(state: STATE_UPLOADING, file: filepath)

      file = CartoDB::FileUploadFile.new(filepath)

      s3_config = Cartodb.config[:exporter]['s3'] || {}
      s3_config['content-disposition'] = %{attachment;filename="#{file.original_filename}"}

      results = file_upload_helper.upload_file_to_storage(
        file_param: file,
        s3_config: s3_config,
        allow_spaces: true,
        force_s3_upload: true
      )

      if results[:file_uri].present?
        logger.append("By file_upload_helper: #{results[:file_uri]}, #{filepath}, (ignored: #{results[:file_path]})")
        export_url = results[:file_uri]
        export_file = filepath
      else<commit_msg>utf-8 encoding at filename* header fixes #10701<commit_after># encoding: UTF-8

require 'fileutils'
require 'active_record'
require_relative '../../services/carto/visualizations_export_service_2'
require_relative '../../../services/sql-api/sql_api'
require_relative './visualization'
require_dependency 'carto/visualization_exporter'

module Carto
  class VisualizationExport < ::ActiveRecord::Base
    include VisualizationExporter
    include Carto::VisualizationsExportService2Validator

    belongs_to :visualization, class_name: Carto::Visualization
    belongs_to :user, class_name: Carto::User
    belongs_to :log, class_name: Carto::Log

    validate :visualization_exportable_by_user?, if: :new_record?
    validate :user_tables_ids_valid?, if: :new_record?
    validate :valid_visualization_type?, if: :new_record?

    STATE_PENDING = 'pending'.freeze
    STATE_EXPORTING = 'exporting'.freeze
    STATE_UPLOADING = 'uploading'.freeze
    STATE_COMPLETE = 'complete'.freeze
    STATE_FAILURE = 'failure'.freeze

    def exporter_folder
      ensure_folder(exporter_config['exporter_temporal_folder'] || DEFAULT_EXPORTER_TMP_FOLDER)
    end

    def run_export!(file_upload_helper: default_file_upload_helper, download_path: nil)
      logger = Carto::Log.new(type: 'visualization_export')

      logger.append('Exporting')
      update_attributes(state: STATE_EXPORTING, log: logger)
      filepath = export(visualization, user, user_tables_ids: user_tables_ids)

      logger.append('Uploading')
      update_attributes(state: STATE_UPLOADING, file: filepath)

      file = CartoDB::FileUploadFile.new(filepath)

      s3_config = Cartodb.config[:exporter]['s3'] || {}
      s3_config['content-disposition'] = %{attachment;filename="#{URI::encode(file.original_filename.force_encoding('iso-8859-1'))}";filename*=utf-8''#{URI::encode(file.original_filename)}}

      results = file_upload_helper.upload_file_to_storage(
        file_param: file,
        s3_config: s3_config,
        allow_spaces: true,
        force_s3_upload: true
      )

      if results[:file_uri].present?
        logger.append("By file_upload_helper: #{results[:file_uri]}, #{filepath}, (ignored: #{results[:file_path]})")
        export_url = results[:file_uri]
        export_file = filepath
      else
{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.006747638326585695, 'mean_word_length': 4.607818411097099, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1546538', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1370, 'n_words': 235}