From bf62a7fedbd074479e0efa370fa36ed844c4518a Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Wed, 30 Nov 2022 10:04:51 +0000 Subject: [PATCH] swap ruby-filemagic for file command --- Gemfile | 2 -- Gemfile.lock | 2 -- app/models/forms/bulk_upload_lettings/upload_your_file.rb | 7 ++++++- app/models/forms/bulk_upload_sales/upload_your_file.rb | 7 ++++++- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index a4a56c939..02bcdf3b7 100644 --- a/Gemfile +++ b/Gemfile @@ -61,8 +61,6 @@ gem "auto_strip_attributes" # Use sidekiq for background processing gem "sidekiq" -gem "ruby-filemagic" - group :development, :test do # Check gems for known vulnerabilities gem "bundler-audit" diff --git a/Gemfile.lock b/Gemfile.lock index 082b7874d..b509fb950 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -370,7 +370,6 @@ GEM rubocop (~> 1.0) rubocop-rspec (2.7.0) rubocop (~> 1.19) - ruby-filemagic (0.7.3) ruby-progressbar (1.11.0) ruby2_keywords (0.0.5) rubyzip (2.3.2) @@ -476,7 +475,6 @@ DEPENDENCIES rubocop-govuk (= 4.3.0) rubocop-performance rubocop-rails - ruby-filemagic selenium-webdriver sentry-rails sentry-ruby diff --git a/app/models/forms/bulk_upload_lettings/upload_your_file.rb b/app/models/forms/bulk_upload_lettings/upload_your_file.rb index 06c7183d1..25e8a73f4 100644 --- a/app/models/forms/bulk_upload_lettings/upload_your_file.rb +++ b/app/models/forms/bulk_upload_lettings/upload_your_file.rb @@ -1,3 +1,5 @@ +require "shellwords" + module Forms module BulkUploadLettings class UploadYourFile @@ -55,7 +57,10 @@ module Forms def validate_file_is_csv return unless file - unless FileMagic.new(FileMagic::MAGIC_MIME).file(file.path).include?("text/csv") + argv = %W[file --mime-type -- #{file.path}] + output = `#{argv.shelljoin}` + + unless output.include?("text/csv") errors.add(:file, :not_csv) end end diff --git a/app/models/forms/bulk_upload_sales/upload_your_file.rb b/app/models/forms/bulk_upload_sales/upload_your_file.rb index 6b5b7a629..3632734ab 100644 --- a/app/models/forms/bulk_upload_sales/upload_your_file.rb +++ b/app/models/forms/bulk_upload_sales/upload_your_file.rb @@ -1,3 +1,5 @@ +require "shellwords" + module Forms module BulkUploadSales class UploadYourFile @@ -55,7 +57,10 @@ module Forms def validate_file_is_csv return unless file - unless FileMagic.new(FileMagic::MAGIC_MIME).file(file.path).include?("text/csv") + argv = %W[file --mime-type -- #{file.path}] + output = `#{argv.shelljoin}` + + unless output.include?("text/csv") errors.add(:file, :not_csv) end end