pgcontents/api_utils.py
def from_b64(path, bcontent, format):
    """
    Decode base64 content for a file.

    format:
      If 'text', the contents will be decoded as UTF-8.
      If 'base64', do nothing.
      If not specified, try to decode as UTF-8, and fall back to base64

    Returns a triple of decoded_content, format, and mimetype.
    """
    decoders = {
        'base64': lambda path, bcontent: (bcontent.decode('ascii'), 'base64'),
        'text': _decode_text_from_base64,
        None: _decode_unknown_from_base64,
    }

    try:
        content, real_format = decoders[format](path, bcontent)
    except HTTPError:
        # Pass through HTTPErrors, since we intend for them to bubble all the
        # way back to the API layer.
        raise
    except Exception as e:
        # Anything else should be wrapped in a CorruptedFile, since it likely
        # indicates misconfiguration of encryption.
        raise CorruptedFile(e)

    default_mimes = {
        'text': 'text/plain',
        'base64': 'application/octet-stream',
    }
    mimetype = mimetypes.guess_type(path)[0] or default_mimes[real_format]

    return content, real_format, mimetype
def from_b64(path, bcontent, format):
    """
    Decode base64 content for a file.

    format:
      If 'text', the contents will be decoded as UTF-8.
      If 'base64', do nothing.
      If not specified, try to decode as UTF-8, and fall back to base64

    Returns a triple of decoded_content, format, and mimetype.
    """
    decoders = {
        'base64': lambda path, bcontent: (bcontent.decode('ascii'), 'base64'),
        'text': _decode_text_from_base64,
        None: _decode_unknown_from_base64,
    }

    try:
        content, real_format = decoders[format](path, bcontent)
    except HTTPError:
        # Pass through HTTPErrors, since we intend for them to bubble all the
        # way back to the API layer.
        raise
    except Exception as e:
        # Anything else should be wrapped in a CorruptedFile, since it likely
        # indicates misconfiguration of encryption.
        raise CorruptedFile(e)

    default_mimes = {
        'text': 'text/plain',
        'base64': 'application/octet-stream',
    }
    mimetype = mimetypes.guess_type(path)[0] or default_mimes[real_format]

    return content, real_format, mimetype
def from_b64 ( path , bcontent , format ) : decoders = { 'base64' : lambda path , bcontent : ( bcontent . decode ( 'ascii' ) , 'base64' ) , 'text' : _decode_text_from_base64 , None : _decode_unknown_from_base64 , } try : content , real_format = decoders [ format ] ( path , bcontent ) except HTTPError : # Pass through HTTPErrors, since we intend for them to bubble all the # way back to the API layer. raise except Exception as e : # Anything else should be wrapped in a CorruptedFile, since it likely # indicates misconfiguration of encryption. raise CorruptedFile ( e ) default_mimes = { 'text' : 'text/plain' , 'base64' : 'application/octet-stream' , } mimetype = mimetypes . guess_type ( path ) [ 0 ] or default_mimes [ real_format ] return content , real_format , mimetype
Decode base64 content for a file.

    format:
      If 'text', the contents will be decoded as UTF-8.
      If 'base64', do nothing.
      If not specified, try to decode as UTF-8, and fall back to base64

    Returns a triple of decoded_content, format, and mimetype.
Decode base64 content for a file .