diff --git a/app/controllers/discourse_data_explorer/query_controller.rb b/app/controllers/discourse_data_explorer/query_controller.rb index 160c02a0..6b6ecac8 100644 --- a/app/controllers/discourse_data_explorer/query_controller.rb +++ b/app/controllers/discourse_data_explorer/query_controller.rb @@ -179,12 +179,14 @@ def run render json: { success: false, errors: [err_msg] }, status: 422 else - filename = + content_disposition = "attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, "discourse")}-#{Date.today}.dcqresult" respond_to do |format| format.json do - response.headers["Content-Disposition"] = "#{filename}.json" if params[:download] + response.headers["Content-Disposition"] = "#{content_disposition}.json" if params[ + :download + ] render json: ResultFormatConverter.convert( @@ -195,7 +197,7 @@ def run ) end format.csv do - response.headers["Content-Disposition"] = "#{filename}.csv" + response.headers["Content-Disposition"] = "#{content_disposition}.csv" render plain: ResultFormatConverter.convert(:csv, result) end diff --git a/lib/report_generator.rb b/lib/report_generator.rb index 4ecb4c63..2a2fd90d 100644 --- a/lib/report_generator.rb +++ b/lib/report_generator.rb @@ -15,7 +15,7 @@ def self.generate(query_id, query_params, recipients, opts = {}) return [] if opts[:skip_empty] && result[:pg_result].values.empty? table = ResultToMarkdown.convert(result[:pg_result]) - build_report_pms(query, table, recipients, opts: { attach_csv: opts[:attach_csv], result: }) + build_report_pms(query, table, recipients, attach_csv:, result:) end private @@ -40,14 +40,14 @@ def self.params_to_hash(query_params) params_hash end - def self.build_report_pms(query, table = "", targets = [], opts: {}) + def self.build_report_pms(query, table = "", targets = [], attach_csv: false, result: nil) pms = [] upload = - if opts[:attach_csv] + if attach_csv tmp_filename = "#{query.slug}@#{Slug.for(Discourse.current_hostname, "discourse")}-#{Date.today}.dcqresult.csv" tmp = Tempfile.new(tmp_filename) - tmp.write(ResultFormatConverter.convert(:csv, opts[:result])) + tmp.write(ResultFormatConverter.convert(:csv, result)) tmp.rewind UploadCreator.new(tmp, tmp_filename, type: "csv_export").create_for( Discourse.system_user.id,