Created
March 13, 2014 23:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where to write this code? how FilelessIO ,ConvertBase64ImageService classes are called