Home > Mobile >  Ruby: Is there a way to specify your encoding in File.write?
Ruby: Is there a way to specify your encoding in File.write?

Time:09-16

TL;DR

How would I specify the mode of encoding on File.write, or how would one save image binary to a file in a similar fashion?

More Details

I'm trying to download an image from a Trello card and then upload that image to S3 so it has an accessible URL. I have been able to download the image from Trello as binary (I believe it is some form of binary), but I have been having issues saving this as a .jpeg using File.write. Every time I attempt that, it gives me this error in my Rails console:

Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8
from /app/app/services/customer_order_status_notifier/card.rb:181:in `write'

And here is the code that triggers that:

def trello_pics
    @trello_pics ||=
    card.attachments.last(config_pics_number)&.map(&:url).map do |url|
      binary = Faraday.get(url, nil, {'Authorization' => "OAuth oauth_consumer_key=\"#{ENV['TRELLO_PUBLIC_KEY']}\", oauth_token=\"#{ENV['TRELLO_TOKEN']}\""}).body
      File.write(FILE_LOCATION, binary) # doesn't work
      run_me
    end
  end

So I figure this must be an issue with the way that File.write converts the input into a file. Is there a way to specify encoding?

CodePudding user response:

AFIK you can't do it at the time of performing the write, but you can do it at the time of creating the File object; here an example of UTF8 encoding:

File.open(FILE_LOCATION, "w:UTF-8") do 
  |f|
  f.write(....)
end

Another possibility would be to use the external_encoding option:

File.open(FILE_LOCATION, "w", external_encoding: Encoding::UTF_8)

Of course this assumes that the data which is written, is a String. If you have (packed) binary data, you would use "wb" for openeing the file, and syswrite instead of write to write the data to the file.

UPDATE As engineersmnky points out in a comment, the arguments for the encoding can also be passed as parameter to the write method itself, for instance

IO::write(FILE_LOCATION, data_to_write, external_encoding: Encoding::UTF_8)
  • Related