Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding validator for files on converter to cloud project #614

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions qfieldsync/core/cloud_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from qfieldsync.core.preferences import Preferences
from qfieldsync.utils.qgis_utils import open_project
from qfieldsync.utils.file_utils import is_valid_filename, is_valid_path


class CloudConverter(QObject):
Expand Down Expand Up @@ -128,6 +129,19 @@ def convert(self) -> None: # noqa: C901
self.project.removeMapLayer(layer)
continue
else:
# Validate filenames and paths before copying
if not is_valid_filename(layer.name()) or not is_valid_path(
layer.source()
):
SeqLaz marked this conversation as resolved.
Show resolved Hide resolved
self.warning.emit(
self.tr("Cloud Converter"),
self.tr(
"The layer '{}' has an invalid filename or path and was therefore removed from the cloud project."
).format(layer.name()),
)
self.project.removeMapLayer(layer)
continue

layer_source.copy(self.export_dirname, list())
layer.setCustomProperty(
"QFieldSync/cloud_action", layer_source.default_cloud_action
Expand Down
25 changes: 25 additions & 0 deletions qfieldsync/utils/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from enum import Enum
from pathlib import Path
from typing import List, TypedDict, Union
import re

PathLike = Union[Path, str]

Expand Down Expand Up @@ -61,3 +62,27 @@ def path_to_dict(path: PathLike, dirs_only: bool = False) -> DirectoryTreeDict:
node["content"].sort(key=lambda node: node["path"].name)

return node


def is_valid_filename(filename: str) -> bool:
"""
Check if the filename is valid.
"""
pattern = re.compile(
r'\A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|\s|[\.]{2,})[^\\\/:*"?<>|]{1,254}(?<![\s\.])\z'
)
return bool(pattern.match(filename))


def is_valid_path(path: str) -> bool:
SeqLaz marked this conversation as resolved.
Show resolved Hide resolved
"""
Check if the entire path is valid.
"""
try:
path_obj = Path(path)
for part in path_obj.parts:
if not is_valid_filename(part):
return False
return True
except Exception:
return False
Loading