Skip to content

Commit 1f813d9

Browse files
committed
Merge pull request rails#1713 from dmathieu/3-1-cherry
Cherry picking encoding for 3.1
2 parents 9267a43 + 055a88d commit 1f813d9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

actionpack/lib/action_dispatch/http/upload.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class UploadedFile
44
attr_accessor :original_filename, :content_type, :tempfile, :headers
55

66
def initialize(hash)
7-
@original_filename = hash[:filename]
7+
@original_filename = encode_filename(hash[:filename])
88
@content_type = hash[:type]
99
@headers = hash[:head]
1010
@tempfile = hash[:tempfile]
@@ -30,6 +30,16 @@ def rewind
3030
def size
3131
@tempfile.size
3232
end
33+
34+
private
35+
def encode_filename(filename)
36+
# Encode the filename in the utf8 encoding, unless it is nil or we're in 1.8
37+
if "ruby".encoding_aware? && filename
38+
filename.force_encoding("UTF-8").encode!
39+
else
40+
filename
41+
end
42+
end
3343
end
3444

3545
module Upload

actionpack/test/dispatch/uploaded_file_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def test_original_filename
1212
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
1313
assert_equal 'foo', uf.original_filename
1414
end
15+
16+
if "ruby".encoding_aware?
17+
def test_filename_should_be_in_utf_8
18+
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
19+
assert_equal "UTF-8", uf.original_filename.encoding.to_s
20+
end
21+
end
1522

1623
def test_content_type
1724
uf = Http::UploadedFile.new(:type => 'foo', :tempfile => Object.new)

0 commit comments

Comments
 (0)