Class: Carrierwave::Base64::Base64StringIO
- Inherits:
-
StringIO
- Object
- StringIO
- Carrierwave::Base64::Base64StringIO
- Defined in:
- lib/carrierwave/base64/base64_string_io.rb
Overview
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.
Instance Attribute Summary collapse
-
#file_extension ⇒ String
readonly
The file extension for the uploaded file.
-
#file_name ⇒ String
readonly
The file name without extension.
Instance Method Summary collapse
-
#initialize(encoded_file, file_name) ⇒ StringIO
constructor
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.
-
#original_filename ⇒ String
Returns a file name with extension, based on the MIME type specified in the base64 encoded string.
Constructor Details
#initialize(encoded_file, file_name) ⇒ StringIO
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.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/carrierwave/base64/base64_string_io.rb', line 26 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 |
Instance Attribute Details
#file_extension ⇒ String (readonly)
Returns the file extension for the uploaded file.
14 15 16 |
# File 'lib/carrierwave/base64/base64_string_io.rb', line 14 def file_extension @file_extension end |
#file_name ⇒ String (readonly)
Returns the file name without extension.
11 12 13 |
# File 'lib/carrierwave/base64/base64_string_io.rb', line 11 def file_name @file_name end |
Instance Method Details
#original_filename ⇒ String
Returns a file name with extension, based on the MIME type specified in the base64 encoded string.
43 44 45 |
# File 'lib/carrierwave/base64/base64_string_io.rb', line 43 def original_filename File.basename("#{@file_name}.#{@file_extension}") end |