import six


def safe_filename(filename, extension=None):
    """
    Returns a filename with FAT32-, NTFS- and HFS+-illegal characters removed.

    Unicode or bytestring datatype of filename is preserved.

    >>> safe_filename(u'spam*?: 𐍃𐍀𐌰𐌼-&.txt')
    u'spam 𐍃𐍀𐌰𐌼-&.txt'
    """
    filename = filename if isinstance(filename, six.text_type) else filename.decode('utf8')
    if extension is not None:
        filename = "{}.{}".format(filename, extension)
    unsafe_chars = ':*?"<>|/\\\r\n'
    for c in unsafe_chars:
        filename = filename.replace(c, '')
    return filename