Skip to content

Commit

Permalink
Added faster detection for xlsx files
Browse files Browse the repository at this point in the history
  • Loading branch information
hdf1996 committed Feb 10, 2020
1 parent 8f07ddd commit 84e87c4
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/interactors/sheets/read_sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ def call # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
:path_for,
context.record.sheet_file.key
)
context.sheet_type = :xlsx
context.sheet_file =
RubyXL::Parser.parse(context.sheet_path)
file_type = `file --b --mime-type '#{context.sheet_path}'`.strip
Rails.logger.info file_type
raise Zip::Error unless file_type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' # rubocop:disable Metrics/LineLength

Rails.logger.info 'XLSX opened'
context.sheet_type = :xlsx

context.record[context.state_attribute] = :error
context.record.error_message = 'XLSX files not supported'
context.record.save!
Expand All @@ -24,7 +27,8 @@ def call # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
context.sheet_file = Spreadsheet.open(context.sheet_path)
context.current_sheet = context.sheet_file.worksheets[0]
end
rescue Ole::Storage::FormatError
rescue Ole::Storage::FormatError => e
Rails.logger.info e
Rails.logger.info 'Falling back to csv'
context.sheet_type = :csv
context.sheet_file = CSV.read(context.sheet_path)
Expand Down

0 comments on commit 84e87c4

Please sign in to comment.