Skip to content

Commit

Permalink
encode the uploaded file's name in the default external encoding - Cl…
Browse files Browse the repository at this point in the history
…oses #869
  • Loading branch information
dmathieu committed Jun 15, 2011
1 parent 9267a43 commit f6fe174
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion actionpack/lib/action_dispatch/http/upload.rb
Expand Up @@ -4,7 +4,7 @@ class UploadedFile
attr_accessor :original_filename, :content_type, :tempfile, :headers

def initialize(hash)
@original_filename = hash[:filename]
@original_filename = encode_filename(hash[:filename])
@content_type = hash[:type]
@headers = hash[:head]
@tempfile = hash[:tempfile]
Expand All @@ -30,6 +30,17 @@ def rewind
def size
@tempfile.size
end

private
def encode_filename(filename)
# Encode the filename in the default_external encoding, unless it is nil or we're in 1.8
if "ruby".encoding_aware? && filename
encoding = Encoding.default_external
filename.force_encoding(encoding)
else
filename
end
end
end

module Upload
Expand Down
12 changes: 12 additions & 0 deletions actionpack/test/dispatch/uploaded_file_test.rb
Expand Up @@ -12,6 +12,18 @@ def test_original_filename
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
assert_equal 'foo', uf.original_filename
end

if "ruby".encoding_aware?
def test_filename_should_be_in_default_encoding
Encoding.default_external = "UTF-16LE"
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
assert "UTF-16LE", uf.original_filename.encoding.to_s

Encoding.default_external = "UTF-8"
uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
assert "UTF-8", uf.original_filename.encoding.to_s
end
end

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

0 comments on commit f6fe174

Please sign in to comment.