class Dragonfly::OpenSSL::Encoder

Public Instance Methods

encode(temp_object, format, options = {}) click to toggle source
# File lib/dragonfly-openssl/encoder.rb, line 7
def encode(temp_object, format, options = {})
  throw :unable_to_handle unless [:encrypt, :decrypt].include?(format)
  throw :unable_to_handle unless key

  options[:meta] ||= {}

  original_filename = temp_object.original_filename

  temp_file = Tempfile.new(original_filename)
  temp_file.binmode
  temp_file.close

  dec = (format == :decrypt) ? '-d ' : ''

  %x{openssl enc -aes-256-cbc #{dec} -in "#{temp_object.path}" -out "#{temp_file.path}" -pass pass:"#{key}"}

  content = ::Dragonfly::TempObject.new(File.new(temp_file.path))
  meta = {
      name: original_filename,
      encrypted: (format != :decrypt)
  }.merge(options[:meta])

  [ content, meta ]
end