From 9d0fba64c055efdde1a0fc843f42b1ff1e27ff86 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 11 Jun 2019 12:14:31 +0800 Subject: [PATCH] FIX: Use attachment format in user export system post take 2. --- app/jobs/regular/export_csv_file.rb | 19 +++++++++---------- config/locales/server.de.yml | 2 +- config/locales/server.en.yml | 2 +- config/locales/server.es.yml | 2 +- config/locales/server.fi.yml | 2 +- config/locales/server.fr.yml | 2 +- config/locales/server.hy.yml | 2 +- config/locales/server.sl.yml | 2 +- config/locales/server.ur.yml | 2 +- config/locales/server.zh_CN.yml | 2 +- config/locales/server.zh_TW.yml | 2 +- spec/jobs/export_csv_file_spec.rb | 19 ++++++++++++++++--- 12 files changed, 35 insertions(+), 23 deletions(-) diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index c3af9fd8349..8101b10ef36 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -63,9 +63,8 @@ module Jobs system('gzip', '-5', absolute_path) # create upload - download_link = nil + upload = nil compressed_file_path = "#{absolute_path}.gz" - file_size = number_to_human_size(File.size(compressed_file_path)) if File.exist?(compressed_file_path) File.open(compressed_file_path) do |file| @@ -78,16 +77,16 @@ module Jobs if upload.persisted? user_export.update_columns(upload_id: upload.id) - download_link = upload.url else Rails.logger.warn("Failed to upload the file #{Discourse.base_uri}/export_csv/#{file_name}.gz") end end + File.delete(compressed_file_path) end - ensure - post = notify_user(download_link, file_name, file_size, export_title) + post = notify_user(upload, export_title) + if user_export.present? && post.present? topic = post.topic user_export.update_columns(topic_id: topic.id) @@ -388,22 +387,22 @@ module Jobs screened_url_array end - def notify_user(download_link, file_name, file_size, export_title) + def notify_user(upload, export_title) post = nil + if @current_user - post = if download_link.present? + post = if upload SystemMessage.create_from_system_user( @current_user, :csv_export_succeeded, - download_link: download_link, - file_name: "#{file_name}.gz", - file_size: file_size, + download_link: "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{number_to_human_size(upload.filesize)})", export_title: export_title ) else SystemMessage.create_from_system_user(@current_user, :csv_export_failed) end end + post end end diff --git a/config/locales/server.de.yml b/config/locales/server.de.yml index 5407547c608..0c502bfa8ce 100644 --- a/config/locales/server.de.yml +++ b/config/locales/server.de.yml @@ -2347,7 +2347,7 @@ de: text_body_template: | Deine Daten wurden erfolgreich exportiert! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} Der obenstehende Download-Link wird 48 Stunden gültig sein. diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 6d316e4194e..cea98cf6448 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2657,7 +2657,7 @@ en: text_body_template: | Your data export was successful! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} The above download link will be valid for 48 hours. diff --git a/config/locales/server.es.yml b/config/locales/server.es.yml index a6ec7ef1810..76582497241 100644 --- a/config/locales/server.es.yml +++ b/config/locales/server.es.yml @@ -2369,7 +2369,7 @@ es: text_body_template: | ¡La exportación de los datos ha finalizado correctamente! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} El enlace de arriba funcionará durante las próximas 48 horas. diff --git a/config/locales/server.fi.yml b/config/locales/server.fi.yml index 8a649f3c78c..b0eeb487e59 100644 --- a/config/locales/server.fi.yml +++ b/config/locales/server.fi.yml @@ -2258,7 +2258,7 @@ fi: text_body_template: | Tietojen vienti onnistui! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} Yllä oleva latauslinkki toimii 48 tunnin ajan. diff --git a/config/locales/server.fr.yml b/config/locales/server.fr.yml index a25a367e254..61ced97c8be 100644 --- a/config/locales/server.fr.yml +++ b/config/locales/server.fr.yml @@ -2322,7 +2322,7 @@ fr: text_body_template: | L'exportation de vos données a réussi ! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} Le lien de téléchargement ci-dessus est valide 48 heures. diff --git a/config/locales/server.hy.yml b/config/locales/server.hy.yml index 3caac1a152c..1c83e48fa11 100644 --- a/config/locales/server.hy.yml +++ b/config/locales/server.hy.yml @@ -2185,7 +2185,7 @@ hy: text_body_template: | Ձեր տվյալների արտահանումը հաջող էր! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} Վերևի ներբեռնման հղումը վավեր կլինի 8 ժամ: diff --git a/config/locales/server.sl.yml b/config/locales/server.sl.yml index 4bd56562f6e..a58a180d2ca 100644 --- a/config/locales/server.sl.yml +++ b/config/locales/server.sl.yml @@ -1113,7 +1113,7 @@ sl: text_body_template: | Vaš izvoz podatkov je končan! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} Zgornja povezava za prenos bo aktivna naslednjih 48 ur. diff --git a/config/locales/server.ur.yml b/config/locales/server.ur.yml index 75bcd56118f..7df4c1e7586 100644 --- a/config/locales/server.ur.yml +++ b/config/locales/server.ur.yml @@ -2367,7 +2367,7 @@ ur: text_body_template: | آپ کا ڈَیٹا ایکسپورٹ کامیاب ہو گیا! :dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} درجہ بالا ڈاؤن لوڈ لِنک 48 گھنٹے تک درست رہے گا۔ diff --git a/config/locales/server.zh_CN.yml b/config/locales/server.zh_CN.yml index 1fe425e97f1..b9a4fa1767d 100644 --- a/config/locales/server.zh_CN.yml +++ b/config/locales/server.zh_CN.yml @@ -2299,7 +2299,7 @@ zh_CN: text_body_template: | 数据成功出成功!:dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} 以上的下载链接将在48小时后失效。 diff --git a/config/locales/server.zh_TW.yml b/config/locales/server.zh_TW.yml index 0a47e3c029a..f787b16bfc5 100644 --- a/config/locales/server.zh_TW.yml +++ b/config/locales/server.zh_TW.yml @@ -2266,7 +2266,7 @@ zh_TW: text_body_template: | 資料成功匯出了!:dvd: - ![%{file_name}|attachment](%{download_link}) (%{file_size}) + %{download_link} 以上的下載連結將在 48 小時後失效。 diff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rb index 941c86430cb..61b8b10f572 100644 --- a/spec/jobs/export_csv_file_spec.rb +++ b/spec/jobs/export_csv_file_spec.rb @@ -4,7 +4,7 @@ require 'rails_helper' describe Jobs::ExportCsvFile do - context '.execute' do + context '#execute' do fab!(:user) { Fabricate(:user, username: "john_doe") } it 'raises an error when the entity is missing' do @@ -13,14 +13,27 @@ describe Jobs::ExportCsvFile do it 'works' do begin - Jobs::ExportCsvFile.new.execute(user_id: user.id, entity: "user_archive") + expect do + Jobs::ExportCsvFile.new.execute( + user_id: user.id, + entity: "user_archive" + ) + end.to change { Upload.count }.by(1) system_message = user.topics_allowed.last + expect(system_message.title).to eq(I18n.t( "system_messages.csv_export_succeeded.subject_template", export_title: "User Archive" )) - expect(system_message.first_post.raw).to include("user-archive-john_doe-") + + upload = system_message.first_post.uploads.first + + expect(system_message.first_post.raw).to eq(I18n.t( + "system_messages.csv_export_succeeded.text_body_template", + download_link: "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.filesize} Bytes)" + ).chomp) + expect(system_message.id).to eq(UserExport.last.topic_id) expect(system_message.closed).to eq(true) ensure