about summary refs log tree commit diff
path: root/app/models/admin/import.rb
blob: 0fd4bdb824ea902d4d7cb164c34f84f268d6bdea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

require 'csv'

# A non-activerecord helper class for csv upload
class Admin::Import
  include ActiveModel::Model

  ROWS_PROCESSING_LIMIT = 20_000

  attr_accessor :data

  validates :data, presence: true
  validate :validate_data

  def data_file_name
    data.original_filename
  end

  def csv_rows
    csv_data.rewind

    csv_data.take(ROWS_PROCESSING_LIMIT + 1)
  end

  private

  def csv_data
    return @csv_data if defined?(@csv_data)

    csv_converter = lambda do |field, field_info|
      case field_info.header
      when '#domain', '#public_comment'
        field&.strip
      when '#severity'
        field&.strip&.to_sym
      when '#reject_media', '#reject_reports', '#obfuscate'
        ActiveModel::Type::Boolean.new.cast(field)
      else
        field
      end
    end

    @csv_data = CSV.open(data.path, encoding: 'UTF-8', skip_blanks: true, headers: true, converters: csv_converter)
    @csv_data.take(1) # Ensure the headers are read
    @csv_data = CSV.open(data.path, encoding: 'UTF-8', skip_blanks: true, headers: ['#domain'], converters: csv_converter) unless @csv_data.headers&.first == '#domain'
    @csv_data
  end

  def csv_row_count
    return @csv_row_count if defined?(@csv_row_count)

    csv_data.rewind
    @csv_row_count = csv_data.take(ROWS_PROCESSING_LIMIT + 2).count
  end

  def validate_data
    return if data.nil?

    errors.add(:data, I18n.t('imports.errors.over_rows_processing_limit', count: ROWS_PROCESSING_LIMIT)) if csv_row_count > ROWS_PROCESSING_LIMIT
  rescue CSV::MalformedCSVError => e
    errors.add(:data, I18n.t('imports.errors.invalid_csv_file', error: e.message))
  end
end