import.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # frozen_string_literal: true
  2. require 'csv'
  3. # A non-activerecord helper class for csv upload
  4. class Admin::Import
  5. include ActiveModel::Model
  6. ROWS_PROCESSING_LIMIT = 20_000
  7. attr_accessor :data
  8. validates :data, presence: true
  9. validate :validate_data
  10. def data_file_name
  11. data.original_filename
  12. end
  13. def csv_rows
  14. csv_data.rewind
  15. csv_data.take(ROWS_PROCESSING_LIMIT + 1)
  16. end
  17. private
  18. def csv_data
  19. return @csv_data if defined?(@csv_data)
  20. csv_converter = lambda do |field, field_info|
  21. case field_info.header
  22. when '#domain', '#public_comment'
  23. field&.strip
  24. when '#severity'
  25. field&.strip&.to_sym
  26. when '#reject_media', '#reject_reports', '#obfuscate'
  27. ActiveModel::Type::Boolean.new.cast(field)
  28. else
  29. field
  30. end
  31. end
  32. @csv_data = CSV.open(data.path, encoding: 'UTF-8', skip_blanks: true, headers: true, converters: csv_converter)
  33. @csv_data.take(1) # Ensure the headers are read
  34. @csv_data = CSV.open(data.path, encoding: 'UTF-8', skip_blanks: true, headers: ['#domain'], converters: csv_converter) unless @csv_data.headers&.first == '#domain'
  35. @csv_data
  36. end
  37. def csv_row_count
  38. return @csv_row_count if defined?(@csv_row_count)
  39. csv_data.rewind
  40. @csv_row_count = csv_data.take(ROWS_PROCESSING_LIMIT + 2).count
  41. end
  42. def validate_data
  43. return if data.nil?
  44. errors.add(:data, I18n.t('imports.errors.over_rows_processing_limit', count: ROWS_PROCESSING_LIMIT)) if csv_row_count > ROWS_PROCESSING_LIMIT
  45. rescue CSV::MalformedCSVError => e
  46. errors.add(:data, I18n.t('imports.errors.invalid_csv_file', error: e.message))
  47. end
  48. end