# Path: pgcontents/error.py
# class CorruptedFile(Exception):
#     pass
# 
# class PathOutsideRoot(Exception):
#     pass
# 
# Path: pgcontents/utils/ipycompat.py



from base64 import (
    b64decode,
    b64encode,
)
from datetime import datetime
from functools import wraps
from tornado.web import HTTPError
from .error import CorruptedFile, PathOutsideRoot
from .utils.ipycompat import reads, writes
import mimetypes
import posixpath
        pass
    return bcontent.decode('ascii'), 'base64'


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.

# Path: pgcontents/error.py
# class CorruptedFile(Exception):
#     pass
# 
# class PathOutsideRoot(Exception):
#     pass
# 
# Path: pgcontents/utils/ipycompat.py



# Path: pgcontents/api_utils.py
from base64 import (
    b64decode,
    b64encode,
)
from datetime import datetime
from functools import wraps
from tornado.web import HTTPError
from .error import CorruptedFile, PathOutsideRoot
from .utils.ipycompat import reads, writes
import mimetypes
import posixpath

        pass
    return bcontent.decode('ascii'), 'base64'


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)