Module: CarrierWave::Base64Uploader

Included in:
Ika::ClassMethods
Defined in:
lib/carrierwave/base64uploader.rb

Instance Method Summary collapse

Instance Method Details

#base64_conversion(uri_str, filename = 'base64') ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/carrierwave/base64uploader.rb', line 4

def base64_conversion(uri_str, filename = 'base64')
  image_data = split_base64(uri_str)
  image_data_string = image_data[:data]
  image_data_binary = Base64.decode64(image_data_string)

  temp_img_file = Tempfile.new(filename)
  temp_img_file.binmode
  temp_img_file << image_data_binary
  temp_img_file.rewind

  img_params = {:filename => "#{filename}", :type => image_data[:type], :tempfile => temp_img_file}
  ActionDispatch::Http::UploadedFile.new(img_params)
end

#split_base64(uri_str) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/carrierwave/base64uploader.rb', line 18

def split_base64(uri_str)
  if uri_str.match(%r{data:(.*?);(.*?),(.*)$})
    uri = Hash.new
    uri[:type] = $1
    uri[:encoder] = $2
    uri[:data] = $3
    uri[:extension] = $1.split('/')[1]
    return uri
  else
    return nil
  end
end