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 to upload on SDK #66

Merged
merged 9 commits into from
Oct 3, 2024
17 changes: 16 additions & 1 deletion qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from requests.adapters import HTTPAdapter, Retry

from .interfaces import QfcException, QfcRequest, QfcRequestException
from .utils import calc_etag, log
from .utils import calc_etag, log, is_valid_windows_filename

logger = logging.getLogger(__file__)

Expand Down Expand Up @@ -408,6 +408,14 @@ def upload_files(

local_files = self.list_local_files(project_path, filter_glob)

# Validate file names
for file in local_files:
is_valid, invalid_chars = is_valid_windows_filename(file["name"])
if not is_valid:
raise ValueError(
f"Invalid file name: {file['name']}. Have invalid characters: {invalid_chars}"
)

# we should always upload all package files
if upload_type == FileTransferType.PACKAGE:
force = True
Expand Down Expand Up @@ -482,6 +490,13 @@ def upload_file(
Returns:
SeqLaz marked this conversation as resolved.
Show resolved Hide resolved
The response object from the upload request.
"""
# Validate file name
is_valid, invalid_chars = is_valid_windows_filename(local_filename.name)
if not is_valid:
raise ValueError(
f"Invalid file name: {local_filename.name}. Have invalid characters: {invalid_chars}"
)

with open(local_filename, "rb") as local_file:
upload_file = local_file
if show_progress:
Expand Down
13 changes: 13 additions & 0 deletions qfieldcloud_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import os
import sys
import re
from typing import Tuple


def print_json(data):
Expand Down Expand Up @@ -69,3 +71,14 @@ def calc_etag(filename: str, part_size: int = 8 * 1024 * 1024) -> str:
final_md5sum = hashlib.md5(b"".join(md5sums))

return "{}-{}".format(final_md5sum.hexdigest(), len(md5sums))


def is_valid_windows_filename(filename: str) -> Tuple[bool, str]:
"""Check if the filename contains forbidden characters for Windows and return them."""
# Forbidden characters in Windows
# https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
invalid_chars = r'[<>:"/\\|?*]'
matches = re.findall(invalid_chars, filename)
if matches:
return False, "".join(set(matches))
return True, ""