Sha256: ea3b8af208659591aec920b51e189919334c416c8b7b667e2367a7a2a1f03db6

Contents?: true

Size: 993 Bytes

Versions: 2

Compression:

Stored size: 993 Bytes

Contents

# Helper method to decode a base64 file and create a StringIO file
module ActiveStorageSupport
  module Base64Attach
    module_function

    def attachment_from_data(attachment)
      attachment = attachment.to_h if attachment.is_a?(ActionController::Parameters)

      if attachment.is_a?(Hash)
        attachment = attachment.symbolize_keys
        fill_attachment_data(attachment, attachment.delete(:data))
      end

      attachment
    end

    def fill_attachment_data(attachment, base64_data)
      return unless base64_data.try(:is_a?, String) && base64_data.strip.start_with?('data')

      headers, data = base64_data.split(',')
      decoded_data  = Base64.decode64(data)

      attachment[:io] = StringIO.new(decoded_data)
      attachment[:content_type] ||= content_type(headers)
      attachment[:filename]     ||= Time.current.to_i.to_s
    end

    def content_type(headers)
      headers =~ /^data:(.*?)$/
      Regexp.last_match(1).split(';base64').first
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_storage_base64-1.2.0 lib/active_storage_support/base64_attach.rb
active_storage_base64-1.1.0 lib/active_storage_support/base64_attach.rb