def _magic_get_file_type(f, _):
    file_type = magic.from_buffer(f.read(1024), mime=True)
    f.seek(0)
    return file_type.decode('utf-8')


def _guess_file_type(_, filename):
    return mimetypes.guess_type(filename)[0]


try:
    import magic
except ImportError:
    import mimetypes
    get_file_type = _guess_file_type
else:
    get_file_type = _magic_get_file_type

def _magic_get_file_type(f, _):
    file_type = magic.from_buffer(f.read(1024), mime=True)
    f.seek(0)
    return maybe_decode(file_type)


def _guess_file_type(_, filename):
    return mimetypes.guess_type(filename)[0]


# return str on python3.  Don't want to unconditionally
# decode because that results in unicode on python2
def maybe_decode(s):
    if str == bytes:
        return s
    else:
        return s.decode('utf-8')


try:
    import magic
except ImportError:
    import mimetypes
    get_file_type = _guess_file_type
else:
    get_file_type = _magic_get_file_type

Return str rather than bytes for the description strings on python3.
Return str rather than bytes for the description strings on python3.
