Exemple #1
0
def secure_filename(filename, fallback):
    """Returns a secure version of a filename.

    This removes possibly dangerous characters and also converts the
    filename to plain ASCII for maximum compatibility.

    :param filename: A filename
    :param fallback: The filename to use if there were no safe chars
                     in the original filename.
    """
    if not filename:
        return fallback
    return _secure_filename(unicode_to_ascii(to_unicode(filename))) or fallback
Exemple #2
0
def secure_filename(filename, fallback):
    """Returns a secure version of a filename.

    This removes possibly dangerous characters and also converts the
    filename to plain ASCII for maximum compatibility.

    :param filename: A filename
    :param fallback: The filename to use if there were no safe chars
                     in the original filename.
    """
    if not filename:
        return fallback
    return _secure_filename(unicode_to_ascii(to_unicode(filename))) or fallback
Exemple #3
0
def secure_filename(input_filename: str, spaces=True) -> str:
    """Ensures that `input_filename` is a valid filename.

    Args:
        filenainput_filenameme (str): filename to parse.
        spaces (bool, optional): if False, all spaces are replaced with
            underscores. Defaults to True.

    Returns:
        str: filename parsed.
    """

    filename = _secure_filename(input_filename)
    if not spaces:
        return filename

    spaced_filename = filename.replace("_", " ").strip()
    if spaced_filename.split(".")[0].upper() in WIN_DEVS:
        warn("Couldn't allow spaces parsing ", FilenameWarning)
        return _secure_filename(filename)

    return spaced_filename
Exemple #4
0
def secure_filename(filename, fallback):
    """Return a secure version of a filename.

    This removes possibly dangerous characters and also converts the
    filename to plain ASCII for maximum compatibility. It should only
    be used for file system storage, since especially filenames written
    in asian languages likely become useless when stripping anything
    that's not ASCII; use :func:`secure_client_filename` for client-facing
    filenames.

    :param filename: A filename
    :param fallback: The filename to use if there were no safe chars
                     in the original filename.
    """
    if not filename:
        return fallback
    return _secure_filename(str_to_ascii(filename)) or fallback