diff --git a/app/models/message_gateway.rb b/app/models/message_gateway.rb index 11ea3167..a3eeb154 100644 --- a/app/models/message_gateway.rb +++ b/app/models/message_gateway.rb @@ -1,3 +1,5 @@ +require 'tempfile' + class MessageGateway < ActionMailer::Base def receive(email) @@ -10,8 +12,26 @@ class MessageGateway < ActionMailer::Base todo_builder = TodoFromRichMessage.new(user, context.id, todo_params[:description], todo_params[:notes]) todo = todo_builder.construct - todo.save! - Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}" + + saved = todo.save! + + 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 + + if saved + Rails.logger.info "Saved email as todo for user #{user.login} in context #{context.name}" + end + end + todo end diff --git a/config/initializers/paperclip.rb b/config/initializers/paperclip.rb new file mode 100644 index 00000000..11e77531 --- /dev/null +++ b/config/initializers/paperclip.rb @@ -0,0 +1,10 @@ +#config/initilizers/paperclip.rb +require 'paperclip/media_type_spoof_detector' + +module Paperclip + class MediaTypeSpoofDetector + def spoofed? + false + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 11323624..9c1144fc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -90,6 +90,7 @@ Rails.application.routes.draw do get 'convert_to_project' # TODO: convert to PUT/POST delete 'remove_predecessor' # TODO: convert to PUT/POST post 'change_context' + get 'attachment' end collection do get 'done' diff --git a/test/controllers/message_gateway_test.rb b/test/controllers/message_gateway_test.rb index d64f6adc..a6628c50 100644 --- a/test/controllers/message_gateway_test.rb +++ b/test/controllers/message_gateway_test.rb @@ -83,4 +83,22 @@ class MessageGatewayTest < ActiveSupport::TestCase assert_not_nil(invalid_context_todo) assert_equal(@inbox, invalid_context_todo.context) end + + def test_receiving_email_adds_attachment + attachment_count = Attachment.count + + load_message('sample_mms.txt') + + message_todo = Todo.where(:description => "This is the subject").first + assert_not_nil(message_todo) + + assert_equal attachment_count+1, Attachment.count + assert_equal 1,message_todo.attachments.count + + orig = File.read(File.join(Rails.root, 'test', 'fixtures', 'sample_mms.txt')) + attachment = File.read(message_todo.attachments.first.file.path) + + assert_equal orig, attachment + end + end