Пример #1
0
def safe_filename(file_name, sep='_', default=None, extension=None):
    """Create a secure filename for plain file system storage."""
    if file_name is None:
        return decode_path(default)

    file_name = decode_path(file_name)
    file_name = os.path.basename(file_name)
    file_name, _extension = os.path.splitext(file_name)
    file_name = _safe_name(file_name, sep=sep) or decode_path(default)
    if file_name is None:
        return decode_path(default)
    extension = _safe_name(extension or _extension, sep=sep)
    if extension is not None:
        file_name = '.'.join((file_name, extension))
    return file_name
Пример #2
0
def make_directory(*parts):
    """Create a directory, be quiet if it already exists."""
    file_path = join_path(*parts)
    try:
        os.makedirs(file_path)
    except Exception:
        pass
    return decode_path(file_path)
Пример #3
0
def safe_filename(file_name, sep='_', default=None, extension=None):
    """Create a secure filename for plain file system storage."""
    if file_name is None:
        return decode_path(default)

    file_name = decode_path(file_name)
    file_name = os.path.basename(file_name)
    file_name, _extension = os.path.splitext(file_name)
    file_name = _safe_name(file_name, sep=sep)
    if file_name is None:
        return decode_path(default)
    file_name = file_name[:MAX_LENGTH]
    extension = _safe_name(extension or _extension, sep=sep)
    if extension is not None:
        if MAX_LENGTH <= (len(file_name) + len(extension)):
            extension = extension[:(MAX_LENGTH - len(file_name) - 1)]
            file_name = file_name[:(MAX_LENGTH - len(extension) - 1)]
        file_name = '.'.join((file_name, extension))
    return file_name
Пример #4
0
def normalize_extension(extension):
    extension = decode_path(extension)
    if extension is not None:
        if extension.startswith('.'):
            extension = extension[1:]
        if '.' in extension:
            _, extension = os.path.splitext(extension)
        extension = slugify(extension, sep='')
        if extension is not None and len(extension):
            return extension
Пример #5
0
def normalize_extension(extension):
    """Normalise a file name extension."""
    extension = decode_path(extension)
    if extension is None:
        return
    if extension.startswith('.'):
        extension = extension[1:]
    if '.' in extension:
        _, extension = os.path.splitext(extension)
    extension = slugify(extension, sep='')
    if extension is None:
        return
    if len(extension):
        return extension
Пример #6
0
def safe_filename(
    file_name: Optional[str],
    sep: str = "_",
    default: Optional[str] = None,
    extension: Optional[str] = None,
) -> Optional[str]:
    """Create a secure filename for plain file system storage."""
    if file_name is None:
        return decode_path(default)

    file_name = decode_path(file_name)
    if file_name is None:
        return None
    file_name = os.path.basename(file_name)
    file_name, _extension = os.path.splitext(file_name)
    file_name = _safe_name(file_name, sep=sep)
    if file_name is None:
        return decode_path(default)
    file_name = file_name[:MAX_LENGTH]
    extension = _safe_name(extension or _extension, sep=sep)
    if extension is not None:
        file_name = ".".join((file_name, extension))
        file_name = file_name[:MAX_LENGTH]
    return file_name
Пример #7
0
def is_file(file_path):
    """Check if a thing is a file, with null guard."""
    file_path = decode_path(file_path)
    if file_path is None:
        return False
    return os.path.isfile(file_path)
Пример #8
0
def join_path(*args):
    args = [decode_path(part) for part in args if part is not None]
    return os.path.join(*args)