Skip to content

Commit 6b95656

Browse files
committed
simplify download diff logic
1 parent 4b1f493 commit 6b95656

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

server/mergin/sync/public_api_controller.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import logging
1010
from dataclasses import asdict
11+
from enum import Enum
1112
from typing import Dict
1213
from datetime import datetime
1314

@@ -80,6 +81,7 @@
8081
generate_location,
8182
is_valid_uuid,
8283
get_device_id,
84+
is_versioned_file,
8385
prepare_download_response,
8486
get_device_id,
8587
wkb2wkt,
@@ -287,6 +289,12 @@ def delete_project(namespace, project_name): # noqa: E501
287289
return NoContent, 200
288290

289291

292+
class DowloadFileAction(Enum):
293+
FULL = "full"
294+
FULL_GPKG = "full_gpkg"
295+
DIFF = "diff"
296+
297+
290298
def download_project_file(
291299
project_name, namespace, file, version=None, diff=None
292300
): # noqa: E501
@@ -307,10 +315,20 @@ def download_project_file(
307315
308316
:rtype: file
309317
"""
310-
project = require_project(namespace, project_name, ProjectPermissions.Read)
311-
if diff and not version:
318+
if not is_versioned_file(file):
319+
action = DowloadFileAction.FULL
320+
elif diff:
321+
action = DowloadFileAction.DIFF
322+
else:
323+
action = DowloadFileAction.FULL_GPKG
324+
325+
if action is DowloadFileAction.DIFF and not version:
312326
abort(400, f"Changeset must be requested for particular file version")
313327

328+
if action is DowloadFileAction.FULL and diff is True:
329+
abort(404, f"No diff in particular file {file})")
330+
331+
project = require_project(namespace, project_name, ProjectPermissions.Read)
314332
lookup_version = (
315333
ProjectVersion.from_v_name(version) if version else project.latest_version
316334
)
@@ -329,18 +347,17 @@ def download_project_file(
329347
if not fh or fh.change == PushChangeType.DELETE.value:
330348
abort(404, f"File {file} not found")
331349

332-
if diff and version:
333-
# get specific version of geodiff file modified in requested version
334-
if not fh.diff:
335-
abort(404, f"No diff in particular file {file} version")
336-
file_path = fh.diff_file.location
337-
else:
338-
file_path = fh.location
350+
# user asked for diff, but there is no diff at that version
351+
if action is DowloadFileAction.DIFF and not fh.diff:
352+
abort(404, f"No diff in particular file {file} version")
339353

354+
file_path = (
355+
fh.diff_file.location if action is DowloadFileAction.DIFF else fh.location
356+
)
340357
abs_path = os.path.join(project.storage.project_dir, file_path)
341358

342359
if not os.path.exists(abs_path):
343-
if version and not diff:
360+
if action is DowloadFileAction.FULL_GPKG:
344361
project.storage.restore_versioned_file(
345362
file, ProjectVersion.from_v_name(version)
346363
)

server/mergin/tests/test_public_api_v2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ def test_create_diff_checkpoint(diff_project):
357357
diff = FileDiff.query.filter_by(
358358
file_path_id=file_path_id, version=16, rank=1
359359
).first()
360-
if os.path.exists(diff.abs_path):
361-
os.remove(diff.abs_path)
360+
assert os.path.exists(diff.abs_path)
361+
os.remove(diff.abs_path)
362362

363363
diff.construct_checkpoint()
364364
assert mock.called

0 commit comments

Comments
 (0)