diff options
author | Louise Crow <louise.crow@gmail.com> | 2012-12-05 18:19:18 +0000 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2012-12-05 18:19:18 +0000 |
commit | 3729f2053d4c04396d440a9c368bed174e9c9605 (patch) | |
tree | 0a678909d381f3fad83a9673fd3adcc73d0b6058 | |
parent | 3a55a3eb5601bbb3ae83d32bc92ffdd9a27961c4 (diff) |
Use the character set of the attachment to encode the string that we're pulling out of the file before converting it to our default encoding.
-rw-r--r-- | app/models/foi_attachment.rb | 14 | ||||
-rw-r--r-- | app/models/incoming_message.rb | 3 |
2 files changed, 17 insertions, 0 deletions
diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb index 7c4c3226f..288c7c1b5 100644 --- a/app/models/foi_attachment.rb +++ b/app/models/foi_attachment.rb @@ -67,9 +67,22 @@ class FoiAttachment < ActiveRecord::Base file.write d } update_display_size! + encode_cached_body! @cached_body = d end + # If the original mail part had a charset, it's some kind of string, so assume that + # it should be handled as a string in the stated charset, not a bytearray, and then + # convert it our default encoding. For ruby 1.8 this is a noop. + def encode_cached_body! + if RUBY_VERSION.to_f >= 1.9 + if charset + @cached_body.force_encoding(charset) + @cached_body = @cached_body.encode(Encoding.default_internal, charset) + end + end + end + def body if @cached_body.nil? tries = 0 @@ -90,6 +103,7 @@ class FoiAttachment < ActiveRecord::Base self.incoming_message.parse_raw_email!(force) retry end + encode_cached_body! end return @cached_body end diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index 3b2f09035..d976d318b 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -597,7 +597,10 @@ class IncomingMessage < ActiveRecord::Base attachments = [] attachment_attributes.each do |attrs| attachment = self.foi_attachments.find_or_create_by_hexdigest(:hexdigest => attrs[:hexdigest]) + body = attrs.delete(:body) attachment.update_attributes(attrs) + # Set the body separately as its handling can depend on the value of charset + attachment.body = body attachment.save! attachments << attachment.id end |