jnicklas/carrierwave — Issue #514: Filename with non-ascii letters causes encoding error
Author: Quintus
Created: 2011-11-03 17:40:15+00:00
State: open

Hi there,

I’m running a Rails 3.1 application and am getting an ArgumentError exception saying `invalid byte sequence in US-ASCII`. I’ve found this is caused by what I think is a bug in carrierwave. First, I’ve a Photo model like this:

class Photo < ActiveRecord::Base
mount_uploader :image, ImageUploader

validates :image, :presence => true
end

Now, when a user uploads a file with a naming containing a non-ascii letter, the above error happens when evaluating `photo.image.url` (the uploading itself works just fine), because carrierwave assigns an US-ASCII encoding to the string where UTF-8 should be. Here’s a rails console session illustrating the problem:

irb(main):005:0> photo.image
=> /uploads/photo/image/110/CIMG1372ä.JPG
irb(main):006:0> photo.image.url
=> "/uploads/photo/image/110/CIMG1372\xC3\xA4.JPG"
irb(main):007:0> photo.image.url.encoding
=> #
irb(main):008:0> photo.image.url.force_encoding("UTF-8")
=> "/uploads/photo/image/110/CIMG1372ä.JPG"

As you can see, a workaround is to use String#force_encoding, but I doubt this is the regular usecase.

Some information on my environment:

`ruby -v`: `ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]`
`rails -v`: `Rails 3.1.1`
`gem -v`: `1.8.11`
carrierwave version: 0.5.7
database engine: sqlite3 (The application isn’t online yet, there I’ll be using postgres)
OS: Arch Linux 64 bits

Valete,
Marvin