From f198ca6944cb5ad5b81efbe42233837b8b773fbb Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 14 May 2015 17:11:24 +0100 Subject: 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"" --- lib/normalize_string.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lib/normalize_string.rb') 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 -- cgit v1.2.3