Decode unicode filenames from URL

This commit is contained in:
Fedor Koshel 2017-03-03 13:39:09 +03:00
parent 6a1445e0da
commit 870213c8fa
4 changed files with 48 additions and 1 deletions

View File

@ -16,7 +16,7 @@ module CarrierWave
end
def original_filename
filename = filename_from_header || File.basename(file.base_uri.path)
filename = filename_from_header || filename_from_uri
mime_type = MIME::Types[file.content_type].first
unless File.extname(filename).present? || mime_type.blank?
filename = "#{filename}.#{mime_type.extensions.first}"
@ -55,6 +55,10 @@ module CarrierWave
end
end
def filename_from_uri
URI.decode(File.basename(file.base_uri.path))
end
def method_missing(*args, &block)
file.send(*args, &block)
end

BIN
spec/fixtures/юникод.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 KiB

View File

@ -304,6 +304,25 @@ describe CarrierWave::Mount do
expect(@instance.remote_image_url).to eq("http://www.example.com/test.jpg")
end
describe "URI with unicode symbols" do
before do
stub_request(
:get,
"http://www.example.com/%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.jpg"
).to_return(body: File.read(file_path("юникод.jpg")))
end
it "works correctly" do
@instance.remote_image_url = "http://www.example.com/%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.jpg"
expect(@instance.remote_image_url).to eq("http://www.example.com/%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.jpg")
end
it "decodes it correctly" do
@instance.remote_image_url = "http://www.example.com/%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.jpg"
expect(@instance.image.current_path).to match(/юникод.jpg$/)
end
end
end
describe "#remote_image_url=" do

View File

@ -8,6 +8,9 @@ describe CarrierWave::Uploader::Download do
let(:url) { base_url + "/test.jpg" }
let(:test_file) { File.read(file_path(test_file_name)) }
let(:test_file_name) { "test.jpg" }
let(:unicode_named_file) { File.read(file_path(unicode_filename)) }
let(:unicode_URL) { URI.encode(base_url + "/#{unicode_filename}") }
let(:unicode_filename) { "юникод.jpg" }
let(:authentication_headers) do
{
'Accept'=>'*/*',
@ -44,6 +47,8 @@ describe CarrierWave::Uploader::Download do
stub_request(:get, "www.example.com/authorization_required.jpg").
with(:headers => authentication_headers).
to_return(body: test_file)
stub_request(:get, unicode_URL).to_return(body: unicode_named_file)
end
context "when a file was downloaded" do
@ -77,6 +82,25 @@ describe CarrierWave::Uploader::Download do
end
end
context "with unicode sybmols in URL" do
before do
uploader.download!(unicode_URL)
end
it "caches a file" do
expect(uploader.file).to be_an_instance_of(CarrierWave::SanitizedFile)
end
it "sets the filename to the file's decoded sanitized filename" do
expect(uploader.filename).to eq("#{unicode_filename}")
end
it "moves it to the tmp dir" do
expect(uploader.file.path).to eq(public_path("uploads/tmp/#{cache_id}/#{unicode_filename}"))
expect(uploader.file.exists?).to be_truthy
end
end
context "with directory permissions set" do
let(:permissions) { 0777 }