Skip to content
This repository
Browse code

encode the uploaded file's name in the default external encoding - Cl…

…oses #869
  • Loading branch information...
commit 383d56b5ee5da7d6e34e75677e96e0f342aa470d 1 parent 954359b
Damien Mathieu dmathieu authored
13 actionpack/lib/action_dispatch/http/upload.rb
@@ -4,7 +4,7 @@ class UploadedFile
4 4 attr_accessor :original_filename, :content_type, :tempfile, :headers
5 5
6 6 def initialize(hash)
7   - @original_filename = hash[:filename]
  7 + @original_filename = encode_filename(hash[:filename])
8 8 @content_type = hash[:type]
9 9 @headers = hash[:head]
10 10 @tempfile = hash[:tempfile]
@@ -30,6 +30,17 @@ def rewind
30 30 def size
31 31 @tempfile.size
32 32 end
  33 +
  34 + private
  35 + def encode_filename(filename)
  36 + # Encode the filename in the default_external encoding, unless it is nil or we're in 1.8
  37 + if "ruby".encoding_aware? && filename
  38 + encoding = Encoding.default_external
  39 + filename.force_encoding(encoding)
  40 + else
  41 + filename
  42 + end
  43 + end
33 44 end
34 45
35 46 module Upload
12 actionpack/test/dispatch/uploaded_file_test.rb
@@ -12,6 +12,18 @@ def test_original_filename
12 12 uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
13 13 assert_equal 'foo', uf.original_filename
14 14 end
  15 +
  16 + if "ruby".encoding_aware?
  17 + def test_filename_should_be_in_default_encoding
  18 + Encoding.default_external = "UTF-16LE"
  19 + uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
  20 + assert "UTF-16LE", uf.original_filename.encoding.to_s
  21 +
  22 + Encoding.default_external = "UTF-8"
  23 + uf = Http::UploadedFile.new(:filename => 'foo', :tempfile => Object.new)
  24 + assert "UTF-8", uf.original_filename.encoding.to_s
  25 + end
  26 + end
15 27
16 28 def test_content_type
17 29 uf = Http::UploadedFile.new(:type => 'foo', :tempfile => Object.new)

0 comments on commit 383d56b

Please sign in to comment.
Something went wrong with that request. Please try again.