diff options
| author | cedricnjanga | 2017-10-23 17:07:14 +0200 |
|---|---|---|
| committer | cedricnjanga | 2017-10-23 17:07:14 +0200 |
| commit | 4e3fefe39274424c14c12cd93016fdf495e7cc08 (patch) | |
| tree | 5c9765dcdedfc849c2c6b523768fa99b8a69db73 | |
| parent | 34e17a46d1564aafb05a4fb7e123188df58bc297 (diff) | |
| download | chouette-core-4e3fefe39274424c14c12cd93016fdf495e7cc08.tar.bz2 | |
Refs #4762 Add file extension checking before importing (standard app and api)
| -rw-r--r-- | app/controllers/api/v1/imports_controller.rb | 7 | ||||
| -rw-r--r-- | app/controllers/imports_controller.rb | 7 | ||||
| -rw-r--r-- | app/models/import.rb | 6 | ||||
| -rw-r--r-- | app/views/imports/_form.html.slim | 2 | ||||
| -rw-r--r-- | config/locales/imports.en.yml | 4 | ||||
| -rw-r--r-- | config/locales/imports.fr.yml | 4 |
6 files changed, 28 insertions, 2 deletions
diff --git a/app/controllers/api/v1/imports_controller.rb b/app/controllers/api/v1/imports_controller.rb index 6050418d8..197e23337 100644 --- a/app/controllers/api/v1/imports_controller.rb +++ b/app/controllers/api/v1/imports_controller.rb @@ -5,7 +5,12 @@ class Api::V1::ImportsController < Api::V1::IbooController def create args = workbench_import_params.merge(creator: 'Webservice') @import = parent.workbench_imports.create(args) - create! + if @import.valid? + create! + else + binding.pry + render json: { status: "error", messages: @import.errors.full_messages } + end end private diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index 3c52dc935..66dccc243 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -38,6 +38,13 @@ class ImportsController < BreadcrumbController end end + def create + create! do |success, failure| + success.html { redirect_to workbench_imports_path } + failure.html { flash.now[:error] = @import.errors[:wrong_file_extension][0] if @import.file.file; render :new } + end + end + def download if params[:token] == resource.token_download send_file resource.file.path diff --git a/app/models/import.rb b/app/models/import.rb index 64f713914..72d9f2ce6 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -19,6 +19,12 @@ class Import < ActiveRecord::Base validates :file, presence: true validates_presence_of :workbench, :creator + validate def file_extension_must_be_zip + if self.file.file + errors.add(:wrong_file_extension, I18n.t('activerecord.errors.models.imports.wrong_file_extension')) unless self.file.file.extension == "zip" + end + end + before_create :initialize_fields def self.model_name diff --git a/app/views/imports/_form.html.slim b/app/views/imports/_form.html.slim index 0fbf578be..95d97c534 100644 --- a/app/views/imports/_form.html.slim +++ b/app/views/imports/_form.html.slim @@ -9,6 +9,6 @@ .form-group = form.label :file, t('activerecord.attributes.import.resources'), class: 'control-label col-sm-4 col-xs-5' .col-sm-8.col-xs-7 - = form.input_field :file, label: false, class: 'form-control' + = form.input :file, label: false, class: 'form-control' = form.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'wb_import_form' diff --git a/config/locales/imports.en.yml b/config/locales/imports.en.yml index 9bf877c86..f3bcad9e9 100644 --- a/config/locales/imports.en.yml +++ b/config/locales/imports.en.yml @@ -53,6 +53,10 @@ en: zero: "import" one: "NeTEx import" other: "imports" + errors: + models: + imports: + wrong_file_extension: "The imported file must be a zip file" attributes: import: resources: "File to import" diff --git a/config/locales/imports.fr.yml b/config/locales/imports.fr.yml index 6998c89d2..6e74fa33c 100644 --- a/config/locales/imports.fr.yml +++ b/config/locales/imports.fr.yml @@ -53,6 +53,10 @@ fr: zero: "import" one: "import NeTEx" other: "imports" + errors: + models: + imports: + wrong_file_extension: "Le fichier importé doit être au format zip" attributes: import: resources: "Fichier à importer" |
