diff options
| author | Alban Peignier | 2015-06-10 17:11:56 +0200 | 
|---|---|---|
| committer | Alban Peignier | 2015-06-10 17:11:56 +0200 | 
| commit | c5f647e1da50c9c3fe01a150e78d45276597c0fa (patch) | |
| tree | 7181e07d1d23f7825e8cd0520de3a906d4848526 | |
| parent | 421cbd7e00f3c736b134ea5714352e48a74501a8 (diff) | |
| download | chouette-core-c5f647e1da50c9c3fe01a150e78d45276597c0fa.tar.bz2 | |
Valid import file mime type. Refs #36456
| -rw-r--r-- | Gemfile | 2 | ||||
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | app/models/import_task.rb | 21 | ||||
| -rw-r--r-- | config/locales/import_tasks.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/import_tasks.fr.yml | 1 | 
5 files changed, 26 insertions, 1 deletions
| @@ -67,6 +67,8 @@ gem "map_layers", "0.0.4"  gem "georuby-ext", "0.0.5"  gem "georuby", "2.3.0" # Fix version for georuby-ext because api has changed +gem 'mimemagic' +  # User interface  gem 'language_engine', '0.0.6'  gem 'calendar_helper', "0.2.5" diff --git a/Gemfile.lock b/Gemfile.lock index cc75de155..fc6844c5d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -245,6 +245,7 @@ GEM        railties (>= 3.0.0, < 5.0.0)      method_source (0.8.2)      mime-types (2.4.3) +    mimemagic (0.3.0)      mini_portile (0.6.2)      minitest (5.6.1)      modernizr-rails (2.0.6) @@ -521,6 +522,7 @@ DEPENDENCIES    letter_opener    map_layers (= 0.0.4)    meta_request +  mimemagic    modernizr-rails (~> 2.0.6)    newrelic_rpm    ninoxe! diff --git a/app/models/import_task.rb b/app/models/import_task.rb index 760080c25..0e1ab65be 100644 --- a/app/models/import_task.rb +++ b/app/models/import_task.rb @@ -20,7 +20,7 @@ class ImportTask    validates_presence_of :user_name    validates_presence_of :name -  validate :validate_file_size +  validate :validate_file_size, :validate_file_content    def initialize( params = {} )      params.each {|k,v| send("#{k}=",v)} @@ -133,4 +133,23 @@ class ImportTask      end    end +  @@valid_mime_types = { +    neptune: %w{application/zip application/xml}, +    netex: %w{application/zip}, +    gtfs: %w{application/zip  text/plain} +  } +  cattr_accessor :valid_mime_types + +  def validate_file_content +    return unless resources.present? and resources.path.present? and File.exists? resources.path + +    mime_type = (File.open(resources.path) { |f| MimeMagic.by_magic f }).try :type +    expected_mime_types = valid_mime_types[data_format.to_sym] + +    unless expected_mime_types.include? mime_type +      message = I18n.t("activemodel.errors.models.import_task.attributes.resources.invalid_mime_type", mime_type: mime_type) +      errors.add(:resources, message) +    end +  end +  end diff --git a/config/locales/import_tasks.en.yml b/config/locales/import_tasks.en.yml index e519ccc25..8c43a7997 100644 --- a/config/locales/import_tasks.en.yml +++ b/config/locales/import_tasks.en.yml @@ -42,6 +42,7 @@ en:            attributes:              resources:                maximum_file_size: "File should smaller than %{maximum_file_size} (%{file_size} sent)" +              invalid_mime_type: "File doesn't use the expected format (%{mime_type} sent)"    formtastic:      titles:        import_task: diff --git a/config/locales/import_tasks.fr.yml b/config/locales/import_tasks.fr.yml index cbaef8c02..754bbe2ec 100644 --- a/config/locales/import_tasks.fr.yml +++ b/config/locales/import_tasks.fr.yml @@ -42,6 +42,7 @@ fr:            attributes:              resources:                maximum_file_size: "Le fichier ne peut dépasser %{maximum_file_size} (%{file_size} soumis)" +              invalid_mime_type: "Le fichier n'utilise pas le format attendu (%{mime_type} soumis)"    formtastic:      titles: | 
