diff options
author | Louise Crow <louise.crow@gmail.com> | 2015-05-14 17:11:24 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2015-05-15 16:25:15 +0100 |
commit | f198ca6944cb5ad5b81efbe42233837b8b773fbb (patch) | |
tree | cf2076727ab19219d052cd86dc1ab09af55d373e /lib | |
parent | 203eea18feeaec3dc9a3e8e8af3b83de085b53ac (diff) |
Force attachment filenames to utf-8 before trying to save them
In a database with encoding SQL-ASCII, an invalid utf-8 filename
can be saved but will cause an "invalid byte sequence in UTF-8"
when the filename is prepared for display. In a database with a
UTF-8 encoding, saving the string will cause an error like
"ActiveRecord::StatementInvalid (PG::Error: ERROR: invalid byte
sequence for encoding "UTF8""
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mail_handler/backends/mail_backend.rb | 7 | ||||
-rw-r--r-- | lib/normalize_string.rb | 9 |
2 files changed, 12 insertions, 4 deletions
diff --git a/lib/mail_handler/backends/mail_backend.rb b/lib/mail_handler/backends/mail_backend.rb index ae3077a72..9b647e2fa 100644 --- a/lib/mail_handler/backends/mail_backend.rb +++ b/lib/mail_handler/backends/mail_backend.rb @@ -64,7 +64,12 @@ module MailHandler # Return a copy of the file name for the mail part def get_part_file_name(part) part_file_name = part.filename - part_file_name.nil? ? nil : part_file_name.dup + part_file_name = part_file_name.nil? ? nil : part_file_name.dup + if part_file_name + part_file_name = CGI.unescape(part_file_name) + part_file_name = convert_string_to_utf8(part_file_name, part.charset) + end + part_file_name end # Get the body of a mail part diff --git a/lib/normalize_string.rb b/lib/normalize_string.rb index de847cd16..1205a1d9c 100644 --- a/lib/normalize_string.rb +++ b/lib/normalize_string.rb @@ -76,9 +76,12 @@ def convert_string_to_utf8(s, suggested_character_encoding=nil) begin result = normalize_string_to_utf8 s, suggested_character_encoding rescue EncodingNormalizationError - result = s.force_encoding("utf-8").encode("utf-8", :invalid => :replace, - :undef => :replace, - :replace => "") if String.method_defined?(:encode) + result = s + if String.method_defined?(:encode) + result = s.force_encoding("utf-8").encode("utf-8", :invalid => :replace, + :undef => :replace, + :replace => "") + end end result end |