class Carrierwave::Base64::Base64StringIO

Class that decodes a base64 string, builds a StringIO for the decoded bytes, and extracts the file MIME type to build a file name with extension.

Attributes

file_extension[R]

@return [String] the file extension for the uploaded file

file_name[R]

@return [String] the file name without extension

Public Class Methods

new(encoded_file, file_name) click to toggle source

Returns a StringIO with decoded bytes from the base64 encoded string and builds a file name with extension for the uploaded file, based on the MIME type specified in the base64 encoded string.

@param encoded_file [String] the base64 encoded file contents @param file_name [String] the file name without extention

@raise [ArgumentError] If the base64 encoded string is empty

@return [StringIO] StringIO with decoded bytes

Calls superclass method
# File lib/carrierwave/base64/base64_string_io.rb, line 25
def initialize(encoded_file, file_name)
  description, encoded_bytes = encoded_file.split(',')

  raise ArgumentError unless encoded_bytes
  raise ArgumentError if encoded_bytes.eql?('(null)')

  @file_name = file_name
  bytes = ::Base64.decode64 encoded_bytes
  @file_extension = get_file_extension description, bytes

  super bytes
end

Public Instance Methods

original_filename() click to toggle source

Returns a file name with extension, based on the MIME type specified in the base64 encoded string.

@return [String] File name with extention

# File lib/carrierwave/base64/base64_string_io.rb, line 42
def original_filename
  File.basename("#{@file_name}.#{@file_extension}")
end

Private Instance Methods

get_file_extension(description, bytes) click to toggle source

Determine content type from input, with provided type as fallback

# File lib/carrierwave/base64/base64_string_io.rb, line 49
def get_file_extension(description, bytes)
  detected_type = Marcel::Magic.by_magic(bytes)
  content_type = (detected_type && detected_type.type) ||
                 description.split(';base64').first
  mime_type = MIME::Types[content_type].last
  unless mime_type
    raise Carrierwave::Base64::UnknownMimeTypeError,
          "Unknown MIME type: #{content_type}"
  end
  mime_type.preferred_extension
end