Skip to content

Commit

Permalink
Replacing validator with the pathvalidate library
Browse files Browse the repository at this point in the history
  • Loading branch information
SeqLaz committed Sep 23, 2024
1 parent 71cd49b commit 25b3d79
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
20 changes: 13 additions & 7 deletions qfieldcloud_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from requests.adapters import HTTPAdapter, Retry

from .interfaces import QfcException, QfcRequest, QfcRequestException
from .utils import calc_etag, log, is_valid_windows_filename
from .utils import calc_etag, log
from pathvalidate import is_valid_filename, is_valid_filepath, ValidationError


logger = logging.getLogger(__file__)

Expand Down Expand Up @@ -410,10 +412,12 @@ def upload_files(

# Validate file names
for file in local_files:
is_valid, invalid_chars = is_valid_windows_filename(file["name"])
if not is_valid:
try:
is_valid_filename(file["name"], platform="auto")
is_valid_filepath(file["absolute_filename"], platform="auto")
except ValidationError as e:
raise ValueError(
f"Invalid file name: {file['name']}. Have invalid characters: {invalid_chars}"
f"Invalid file name or path: {file['name']}. Error: {e}"
)

# we should always upload all package files
Expand Down Expand Up @@ -491,10 +495,12 @@ def upload_file(
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:
try:
is_valid_filename(local_filename.name, platform="auto")
is_valid_filepath(str(local_filename), platform="auto")
except ValidationError as e:
raise ValueError(
f"Invalid file name: {local_filename.name}. Have invalid characters: {invalid_chars}"
f"Invalid file name or path: {local_filename.name}. Error: {e}"
)

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


def print_json(data):
Expand Down Expand Up @@ -71,14 +69,3 @@ 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, ""

0 comments on commit 25b3d79

Please sign in to comment.