import base64
import logging
import traceback


def gen_body(ctype, content):
    """
    Convert image to base64 to be sent as body response.
    """
    try:
        format_ = ctype[ctype.find('/')+1:]
        supported = ['jpeg', 'png', 'gif']
        if format_ not in supported:
            None
        return base64.b64encode(content)
    except Exception as error:
        logging.error('gen_body error: %s' % (error))
        logging.error('gen_body trace: %s' % traceback.format_exc())
        return None