CarrierWave Base64 Image Upload
I ended up solving it using:
def image_params
img_params = params.require(:image).permit(:image, :filename, :date_uploaded, :lat, :lon, :update_ticks, image_comments: [:comment, :date_created, :user_id]).merge(user_id: current_user.id, company_id: current_user.company_id)
img_params.merge(convert_data_uri_to_upload(img_params))
img_params
end
def split_base64(uri_str)
if uri_str.match(%r{^data:(.*?);(.*?),(.*)$})
uri = Hash.new
uri[:type] = $1 # "image/gif"
uri[:encoder] = $2 # "base64"
uri[:data] = $3 # data string
uri[:extension] = $1.split('/')[1] # "gif"
return uri
else
return nil
end
end
def convert_data_uri_to_upload(obj_hash)
if obj_hash[:image].try(:match, %r{^data:(.*?);(.*?),(.*)$})
image_data = split_base64(obj_hash[:image])
image_data_string = image_data[:data]
image_data_binary = Base64.decode64(image_data_string)
temp_img_file = Tempfile.new(obj_hash[:filename])
temp_img_file.binmode
temp_img_file << image_data_binary
temp_img_file.rewind
img_params = {:filename => obj_hash[:filename], :type => image_data[:type], :tempfile => temp_img_file}
uploaded_file = ActionDispatch::Http::UploadedFile.new(img_params)
obj_hash[:url_large] = uploaded_file
obj_hash.delete(:image)
end
return obj_hash
end
use base64 image with Carrierwave
class ImageUploader < CarrierWave::Uploader::Base
class FilelessIO < StringIO
attr_accessor :original_filename
attr_accessor :content_type
end
before :cache, :convert_base64
def convert_base64(file)
if file.respond_to?(:original_filename) &&
file.original_filename.match(/^base64:/)
fname = file.original_filename.gsub(/^base64:/, '')
ctype = file.content_type
decoded = Base64.decode64(file.read)
file.file.tempfile.close!
decoded = FilelessIO.new(decoded)
decoded.original_filename = fname
decoded.content_type = ctype
file.__send__ :file=, decoded
end
file
end
base64 Image uploaded with stringIO carrierwave gem gets corrupt on upload in rails
The image representation is:
data:image/jpeg;base64,/9jblablablabla
Use regexp to get the valuedata:image/jpeg
and /9jblablablabla
image/jpeg
will be your file type
/9jblablablabla
will be the representation of the image.
Decoding wrong source might cause the image file corrupted.
Then, you can use FileTemp to create and save the file. Hope its help others too ..
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/tempfile/rdoc/Tempfile.html
Rails 5 API image uploader with carrierwave-base 64 error
remove :create
from your before_action :set_medium
Related Topics
How to Make a Post Request with Open-Uri
Setting Path for Whenever in Cron So It Can Find Ruby
The Compiler Failed to Generate an Executable File. (Runtimeerror)
How to Have Rspec Test for My Default Scope
How to Use Rspec to Mock Stdin/Stdout to Test Console Reads & Writes
How to Access Nested Elements of a Hash with a Single String Key
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Rails 3.1 - Has_And_Belongs_To_Many Deprecated
What's the Point of the Prefix? Operator in Ruby 1.9
Ruby Class Inheritance: What Is '<<' (Double Less Than)
How to Create Wizard Forms in Ruby on Rails
Why Do I Get an Error Installing the JSON Gem in Ubuntu
How to Create a Nokogiri Case Insensitive Xpath Selector
Optional Arguments with Default Value in Ruby
Sass::Syntaxerror: File to Import Not Found or Unreadable: Compass in Production