Skip to content

Commit 75c2da7

Browse files
committed
Don't allow trailing space for new files
1 parent 3f66041 commit 75c2da7

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

server/mergin/sync/files.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
from .utils import (
1515
is_file_name_blacklisted,
16-
is_qgis,
1716
is_supported_extension,
1817
is_valid_path,
1918
is_versioned_file,
19+
has_trailing_space,
2020
)
2121
from ..app import DateTimeWithZ, ma
2222

@@ -221,6 +221,14 @@ def validate(self, data, **kwargs):
221221
f"Please remove the file or try compressing it into a ZIP file before uploading.",
222222
)
223223

224+
# new checks must restrict only new files not to block existing projects
225+
for file in data["added"]:
226+
file_path = file["path"]
227+
if has_trailing_space(file_path):
228+
raise ValidationError(
229+
f"Folder name contains a trailing space. Please remove the space in: {file_path}"
230+
)
231+
224232

225233
class ProjectFileSchema(FileSchema):
226234
mtime = DateTimeWithZ()

server/mergin/sync/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,11 @@ def is_valid_path(filepath: str) -> bool:
347347
)
348348

349349

350+
def has_trailing_space(filepath: str) -> bool:
351+
"""Check filepath for trailing spaces that makes the project impossible to download on Windows"""
352+
return all(part == part.rstrip() for part in Path(filepath).parts)
353+
354+
350355
def is_supported_extension(filepath) -> bool:
351356
"""Check whether file's extension is supported."""
352357
ext = os.path.splitext(filepath)[1].lower()

server/mergin/tests/test_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import base64
66
from datetime import datetime
77
import json
8-
import os
98
import pytest
109
from flask import url_for, current_app
1110
from sqlalchemy import desc
@@ -24,6 +23,7 @@
2423
is_valid_path,
2524
get_x_accel_uri,
2625
wkb2wkt,
26+
has_trailing_space,
2727
)
2828
from ..auth.models import LoginHistory, User
2929
from . import json_headers
@@ -228,6 +228,10 @@ def test_is_valid_path(client, filepath, allow):
228228
assert is_valid_path(filepath) == allow
229229

230230

231+
def test_has_trailing_space():
232+
assert has_trailing_space("photos /lutraHQ.jpg") is False
233+
234+
231235
def test_get_x_accell_uri(client):
232236
"""Test get_x_accell_uri"""
233237
client.application.config["LOCAL_PROJECTS"] = "/data/"

0 commit comments

Comments
 (0)