aboutsummaryrefslogtreecommitdiffstats
path: root/lib/normalize_string.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2015-05-14 17:11:24 +0100
committerLouise Crow <louise.crow@gmail.com>2015-05-15 16:25:15 +0100
commitf198ca6944cb5ad5b81efbe42233837b8b773fbb (patch)
treecf2076727ab19219d052cd86dc1ab09af55d373e /lib/normalize_string.rb
parent203eea18feeaec3dc9a3e8e8af3b83de085b53ac (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/normalize_string.rb')
-rw-r--r--lib/normalize_string.rb9
1 files changed, 6 insertions, 3 deletions
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