diff --git a/script/import_scripts/mbox.rb b/script/import_scripts/mbox.rb index 6ad01ea6748..cf50d1aed91 100755 --- a/script/import_scripts/mbox.rb +++ b/script/import_scripts/mbox.rb @@ -179,6 +179,22 @@ class ImportScripts::Mbox < ImportScripts::Base title = mail.subject + # import the attachments + mail.attachments.each do |attachment| + tmp = Tempfile.new("discourse-email-attachment") + begin + # read attachment + File.open(tmp.path, "w+b") { |f| f.write attachment.body.decoded } + # create the upload for the user + upload = Upload.create_for(user_id_from_imported_user_id(mail.from.first) || Discourse::SYSTEM_USER_ID, tmp, attachment.filename, tmp.size ) + if upload && upload.errors.empty? + raw << "\n\n#{receiver.attachment_markdown(upload)}\n\n" + end + ensure + tmp.try(:close!) rescue nil + end + end + { id: t['id'], title: clean_title(title), user_id: user_id_from_imported_user_id(mail.from.first) || Discourse::SYSTEM_USER_ID, @@ -218,6 +234,22 @@ class ImportScripts::Mbox < ImportScripts::Base selected = receiver.select_body raw = selected.force_encoding(selected.encoding).encode("UTF-8") + # import the attachments + mail.attachments.each do |attachment| + tmp = Tempfile.new("discourse-email-attachment") + begin + # read attachment + File.open(tmp.path, "w+b") { |f| f.write attachment.body.decoded } + # create the upload for the user + upload = Upload.create_for(user_id_from_imported_user_id(mail.from.first) || Discourse::SYSTEM_USER_ID, tmp, attachment.filename, tmp.size ) + if upload && upload.errors.empty? + raw << "\n\n#{receiver.attachment_markdown(upload)}\n\n" + end + ensure + tmp.try(:close!) rescue nil + end + end + { id: id, topic_id: topic_id, user_id: user_id_from_imported_user_id(mail.from.first) || Discourse::SYSTEM_USER_ID,