hanklords/flickraw

Encoding::CompatibilityError

Closed this issue · 3 comments

Hi!

I randomly get an encoding error, here is the stack trace:

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/oauth.rb:110:in `block (2 levels) in post_multipart'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/oauth.rb:101:in `each'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/oauth.rb:101:in `block in post_multipart'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/oauth.rb:152:in `block in post'
  from /home/deploy/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:745:in `start'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/oauth.rb:147:in `post'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/oauth.rb:96:in `post_multipart'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/api.rb:145:in `upload_flickr'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/bundler/gems/flickraw-15e07ed90020/lib/flickraw/api.rb:91:in `upload_photo'
  from /home/deploy/apps/app/releases/20130805072010/app/platforms/flickr/photo.rb:16:in `block in share!'
  from /home/deploy/apps/app/releases/20130805072010/app/platforms/platform/photo.rb:42:in `cache_image'
  from /home/deploy/apps/app/releases/20130805072010/app/platforms/flickr/photo.rb:5:in `share!'
  from /home/deploy/apps/app/releases/20130805072010/app/models/share.rb:34:in `share!'
  from (irb):5
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
  from /home/deploy/apps/app/shared/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>' 

I've not managed to track down the line where this happens, or even reproduce the errors. It's just something that happens occasionally but when it happens it happens again given the same parameters.

Here is the part of my code where I call it. I've appended a fore_encoding to every String parameter but the error is still happening.

def share!
  is_public = Rails.env.production? ? 1 : 0

  cache_image do |image|
    uri       = URI.parse(image)
    mime_type = MIME::Types.type_for(uri.path).first.content_type.force_encoding('UTF-8')
    filename  = uri.path.split('/').last.force_encoding('UTF-8')
    file      = File.open(image)

    safety_level = 1
    if @photo.contains_adult_content?
      safety_level = 3
    end

    @base.flickraw.upload_photo UploadIO.new(file, mime_type, filename),
      title: @photo.title.force_encoding('UTF-8'),
      description: @photo.description.force_encoding('UTF-8'),
      tags: @photo.tag_list.each{ |t| t.gsub!(/\s/,'').force_encoding('UTF-8') }.join(' '),
      is_public: is_public,
      content_type: 1,
      safety_level: safety_level
  end
end

Do you have an example text which is raise the error ? These encodings issues are hard to troubleshoot. You may need to clean user input before passing it to flickraw.

I'll try to isolate it. Is it more likely to be the title, description, tag or any other text?

Looks live I've forgotten to open the file in binary mode. Still strange that it worked for some files and doesn't for other ones.

All I needed was to change File.open(image) to File.open(image, 'rb')