Permalink
Browse files

Fix a potential bug when user turn on AWS logging

This should fix an error when the user has turn on AWS logging while
and upload a file with non-English filename, as `aws-sdk` logger is not
encoding aware.

The problem was that the string returned from another logger methods
is in `US-ASCII`, while the string returned from Paperclip would be in
`UTF-8`. When the logger calls `#join` on those strings, Ruby would
raise an exception.

This solution is not the best, as it involves a monkey-patching, but
should make sure that the upload goes through without any hiccup. For
the long run, I'm going to send a patch to `aws-sdk` and also make
Paperclip require at least `aws-sdk > 1.4.1`
  • Loading branch information...
1 parent 55c686d commit 784717b8715a06d64855322561f8f5f41b350edf @sikachu sikachu committed May 4, 2012
Showing with 29 additions and 0 deletions.
  1. +15 −0 lib/paperclip/storage/s3.rb
  2. +14 −0 test/integration_test.rb
@@ -85,6 +85,21 @@ module S3
def self.extended base
begin
require 'aws-sdk'
+
+ # Overriding AWS::Core::LogFormatter to make sure it return a UTF-8 string
+ if AWS::VERSION >= "1.3.9"
+ AWS::Core::LogFormatter.class_eval do
+ def summarize_hash(hash)
+ hash.map { |key, value| ":#{key}=>#{summarize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
+ end
+ end
+ else
+ AWS::Core::ClientLogging.class_eval do
+ def sanitize_hash(hash)
+ hash.map { |key, value| "#{sanitize_value(key)}=>#{sanitize_value(value)}".force_encoding('UTF-8') }.sort.join(',')
+ end
+ end
+ end
rescue LoadError => e
e.message << " (You may need to install the aws-sdk gem)"
raise e
View
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
require './test/helper'
require 'open-uri'
@@ -635,6 +637,18 @@ def s3_headers_for attachment, style
headers = s3_headers_for(@dummy.avatar, :original)
assert_equal 'image/png', headers['content-type']
end
+
+ context "with non-english character in the file name" do
+ setup do
+
+ @file.stubs(:original_filename).returns("クリップ.png")
+ @dummy.avatar = @file
+ end
+
+ should "not raise any error" do
+ @dummy.save!
+ end
+ end
end
end
end

0 comments on commit 784717b

Please sign in to comment.