class WcoEmail::Message

def save_attachment att, filename: "no-filename-specified"

def save_attachment att, filename: "no-filename-specified"
  config = JSON.parse(stub.config)
  if defined?( config['process_images'] ) &&
    false == config['process_images']
    return
  end
  content_type = att.content_type.split(';')[0]
  if content_type.include? 'image'
    photo = ::Wco::Photo.new({
      content_type:      content_type,
      email_message_id:  self.id,
      image_data:        att.body.encoded,
      original_filename: att.content_type_parameters[:name],
    })
    photo.decode_base64_image
    photo.save
  else
    filename   = CGI.escape( att.filename || filename )
    sio = StringIO.new att.body.decoded
    File.open("/tmp/#{filename}", 'w:UTF-8:ASCII-8BIT') do |f|
      f.puts(sio.read)
    end
    asset = ::Wco::Asset.new({
      email_message: self,
      filename:      filename,
      object:        File.open("/tmp/#{filename}"),
    })
    if !asset.save
      self.logs.push "Could not save a wco asset: #{asset.errors.full_messages.join(", ")}"
      self.save
    end
  end
end