Sha256: a64e4f00c631716574d229e5d3498faf10df9daa149051db5ed3c8e22fa180e0

Contents?: true

Size: 702 Bytes

Versions: 4

Compression:

Stored size: 702 Bytes

Contents

module Carrierwave
  module Base64
    class Base64StringIO < StringIO
      class ArgumentError < StandardError; end

      attr_accessor :image_format

      def initialize(encoded_image)
        description, encoded_bytes = encoded_image.split(",")

        raise ArgumentError unless encoded_bytes

        @image_format = get_image_format description
        bytes = ::Base64.decode64 encoded_bytes

        super bytes
      end

      def original_filename
        File.basename("image.#{@image_format}")
      end

      private

      def get_image_format(description)
        regex = /\Adata:image\/([a-z]+);base64\z/i
        regex.match(description).try(:[], 1)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
carrierwave-base64-1.3 lib/carrierwave/base64/base64_string_io.rb
carrierwave-base64-1.2 lib/carrierwave/base64/base64_string_io.rb
carrierwave-base64-1.1 lib/carrierwave/base64/base64_string_io.rb
carrierwave-base64-1.0 lib/carrierwave/base64/base64_string_io.rb