Skip to content

Instantly share code, notes, and snippets.

@locnguyen
Created March 13, 2014 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save locnguyen/9539062 to your computer and use it in GitHub Desktop.
Save locnguyen/9539062 to your computer and use it in GitHub Desktop.
Service object for converting Base64 encoded image to save with Carrier Wave in Ruby apps
# Needed for Rails 3+
class FilelessIO < StringIO
attr_accessor :original_filename
end
class ConvertBase64ImageService
def call(base64_encoded_image, filename)
split_encoded = split_base64(base64_encoded_image)
io = FilelessIO.new(split_encoded[:data])
io.original_filename = filename
io
end
private
def split_base64(encoded)
if encoded.match(%r{^data:(.*?);(.*?),(.*)$})
{
type: $1,
encoder: $2,
data: Base64.decode64($3),
extension: $1.split('/')[1]
}
end
end
end
@sreenak
Copy link

sreenak commented Jun 19, 2015

where to write this code? how FilelessIO ,ConvertBase64ImageService classes are called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment