Module: ActiveStorageSupport::Base64Attach

Defined in:
lib/active_storage_support/base64_attach.rb

Class Method Summary collapse

Class Method Details

.attachment_from_data(attachment) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_storage_support/base64_attach.rb', line 6

def attachment_from_data(attachment)
  if attachment.is_a?(Hash)
    base64_data = attachment.delete(:data)
    if base64_data.try(:is_a?, String) && base64_data =~ /^data:(.*?);(.*?),(.*)$/

      attachment[:io] = StringIO.new(Base64.decode64(Regexp.last_match(3)))
      attachment[:content_type] = Regexp.last_match(1) unless attachment[:content_type]
      attachment[:filename] = Time.current.to_i.to_s unless attachment[:filename]
    end
  end

  attachment
end