aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZog2018-04-03 14:45:43 +0200
committerZog2018-04-03 14:48:15 +0200
commit3af91a4f5c6d761e444170908836166f115569b2 (patch)
tree3843909d0b6ab722c2df3ee0c979049b86f2ffa3
parent361ad6302d717acff9431d51126d8bf3a6143503 (diff)
downloadchouette-core-3af91a4f5c6d761e444170908836166f115569b2.tar.bz2
Refs #6218; Upload files to exporters6218-upload_files_to_exporters
-rw-r--r--app/controllers/exports_controller.rb3
-rw-r--r--app/models/export/base.rb20
-rw-r--r--app/models/export/simple_exporter/base.rb4
-rw-r--r--app/views/shared/iev_interfaces/_messages.html.slim2
4 files changed, 25 insertions, 4 deletions
diff --git a/app/controllers/exports_controller.rb b/app/controllers/exports_controller.rb
index a5282a514..c89da5000 100644
--- a/app/controllers/exports_controller.rb
+++ b/app/controllers/exports_controller.rb
@@ -3,13 +3,14 @@ class ExportsController < ChouetteController
include RansackDateFilter
include IevInterfaces
skip_before_action :authenticate_user!, only: [:upload]
+ skip_before_action :verify_authenticity_token, only: [:upload]
defaults resource_class: Export::Base, collection_name: 'exports', instance_name: 'export'
def upload
if params[:token] == resource.token_upload
resource.file = params[:file]
resource.save!
- redirect_to [resource.workbench, resource]
+ render json: {status: :ok}
else
user_not_authorized
end
diff --git a/app/models/export/base.rb b/app/models/export/base.rb
index 6cf4c6b02..c65539635 100644
--- a/app/models/export/base.rb
+++ b/app/models/export/base.rb
@@ -1,4 +1,8 @@
+require 'net/http/post/multipart'
+
class Export::Base < ActiveRecord::Base
+ include Rails.application.routes.url_helpers
+
self.table_name = "exports"
belongs_to :referential
@@ -21,6 +25,22 @@ class Export::Base < ActiveRecord::Base
%w(zip csv json)
end
+ def upload_file file
+ url = URI.parse upload_workbench_export_url(self.workbench_id, self.id, host: Rails.application.config.rails_host)
+ res = nil
+ filename = File.basename(file.path)
+ content_type = MIME::Types.type_for(filename).first&.content_type
+ File.open(file.path) do |file_content|
+ req = Net::HTTP::Post::Multipart.new url.path,
+ file: UploadIO.new(file_content, content_type, filename),
+ token: self.token_upload
+ res = Net::HTTP.start(url.host, url.port) do |http|
+ http.request(req)
+ end
+ end
+ res
+ end
+
if Rails.env.development?
def self.force_load_descendants
path = Rails.root.join 'app/models/export'
diff --git a/app/models/export/simple_exporter/base.rb b/app/models/export/simple_exporter/base.rb
index 4e6e8eba4..e77e23468 100644
--- a/app/models/export/simple_exporter/base.rb
+++ b/app/models/export/simple_exporter/base.rb
@@ -48,15 +48,15 @@ class Export::SimpleExporter::Base < Export::Base
exporter.export
set_status_from_exporter
convert_exporter_journal_to_messages
- self.file = tmp
self.save!
+ upload_file tmp
end
def set_status_from_exporter
if exporter.status.to_s == "error"
self.status = :failed
elsif exporter.status.to_s == "success"
- self.status = :successful
+ self.status = :successful
else
self.status = :warning
end
diff --git a/app/views/shared/iev_interfaces/_messages.html.slim b/app/views/shared/iev_interfaces/_messages.html.slim
index 022f4ee01..14157a88d 100644
--- a/app/views/shared/iev_interfaces/_messages.html.slim
+++ b/app/views/shared/iev_interfaces/_messages.html.slim
@@ -9,6 +9,6 @@
- else
.col-md-6= export_message_content message
.col-md-6
- - if message.criticity != "info"
+ - if message.resource_attributes
pre
= JSON.pretty_generate message.resource_attributes || {}