From d2a6af63bd4053ae984c9acea39cea84ac5607ad Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Thu, 17 Jul 2014 12:56:22 +0200 Subject: [PATCH] refactor adding attachments in message_gateway and remove gsub magic with correct tempfile parameter --- app/models/message_gateway.rb | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/app/models/message_gateway.rb b/app/models/message_gateway.rb index a3eeb154..ed46fa1e 100644 --- a/app/models/message_gateway.rb +++ b/app/models/message_gateway.rb @@ -18,16 +18,9 @@ class MessageGateway < ActionMailer::Base if saved Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}" - attachment = todo.attachments.build - tmp = Tempfile.new(['attachment', '.eml'], cr_newline: false) - tmp.write email.raw_source.gsub(/\r\n?/, "\n") # replace \r with \n - Rails.logger.info "Saved received email to #{tmp.path}" - attachment.file = tmp - tmp.close - saved = attachment.save! - tmp.unlink + saved = attach_email_to_todo(todo, email) - if saved + if saved Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}" end end @@ -37,6 +30,23 @@ class MessageGateway < ActionMailer::Base private + def attach_email_to_todo(todo, email) + attachment = todo.attachments.build + + # create temp file + tmp = Tempfile.new(['attachment', '.eml'], universal_newline: true) + tmp.write email.raw_source + + # add temp file to attachment. paperclip will copy the file to the right location + Rails.logger.info "Saved received email to #{tmp.path}" + attachment.file = tmp + tmp.close + saved = attachment.save! + + # delete temp file + tmp.unlink + end + def get_todo_params(email) params = {}