refactor adding attachments in message_gateway

and remove gsub magic with correct tempfile parameter
This commit is contained in:
Reinier Balt 2014-07-17 12:56:22 +02:00
parent ed9f429d4a
commit 25600e39f7

View file

@ -18,14 +18,7 @@ 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
Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}"
@ -41,8 +34,8 @@ class MessageGateway < ActionMailer::Base
attachment = todo.attachments.build
# create temp file
tmp = Tempfile.new(['attachment', '.eml'], {universal_newline: true})
tmp.write email.raw_source.gsub(/\r/, "")
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}"
@ -50,11 +43,6 @@ class MessageGateway < ActionMailer::Base
tmp.close
saved = attachment.save!
# enable write permissions on group, since MessageGateway could be run under different
# user than Tracks (i.e. apache versus mail)
dir = File.open(File.dirname(attachment.file.path))
dir.chmod(0770)
# delete temp file
tmp.unlink
end