0

I am using Ruby On Rails, and accessing a API which is returning me baseencoded string and a checksum for a png type image.

However when i try to create a image from this, its not getting created and i see a message saying image might be corrupt. I might be doing this correctly.

Here is a little more description about the encoded string and checksum

encoded string is form of a Base64-encoded, GZipcompressed
string.

checksum is An MD5 hash to validate the above data, in the form of a
Base64-encoded string.

Here is my approach

    file = File.new(path,"wb")
    file.write Base64.decode64("encodedtext")
    file.close

am i doing anything wrong?

However i am not sure what role the checksum plays, can anyone please suggest what i might be doing wrong, Thanks.

opensource-developer
  • 2,826
  • 4
  • 38
  • 88

1 Answers1

1

I think you will need to uncompress the data before you decode it, otherwise you're attempting to decode the compressed data and hence giving you errors.

You may have to install the Zlib gem for this solution.

require 'zlib'

decoded_string = Base64.decode64(encodedtext)
gzipped_string = Zlib::GzipReader.new(decoded_string)

file = File.new(path, "wb")
file.write gzipped_text.read
file.close

You can find more help with GZip here and here

Community
  • 1
  • 1
RossMc
  • 426
  • 2
  • 6
  • do i need to uncompress it? I am not sure how to un compress it? can you suggest? – opensource-developer Oct 07 '15 at 15:00
  • You can edit your answer and let me know how to uncompress it in code – opensource-developer Oct 07 '15 at 15:01
  • I suspect that you need to do this the other way round, i.e. base64 decode first, then decompress. This way doesn’t make much sense (but I can’t know for sure without seeing the actual data). – matt Oct 07 '15 at 15:46
  • I think you're right. It's probably more likely to be Base64 encoded during transmission. I couldn't be sure either. I'll edit the snippet. – RossMc Oct 08 '15 at 08:42