Skip to content

Commit 6e1faf7

Browse files
committed
Add option to skip downloading locally existing files
1 parent fb46daa commit 6e1faf7

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/bin/qfieldcloud-cli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,18 @@ def upload_files(ctx, project_id, project_path, filter_glob, throw_on_error):
293293
"--throw-on-error/--no-throw-on-error",
294294
help="If any project file downloads fails stop downloading the rest. Default: False",
295295
)
296+
@click.option(
297+
"--skip-existing/--no-skip-existing",
298+
help="Skip files if they already exist locally with same sha256 hash. Default: False",
299+
)
296300
@click.pass_context
297-
def download_files(ctx, project_id, local_dir, filter_glob, throw_on_error):
301+
def download_files(ctx, project_id, local_dir, filter_glob, throw_on_error, skip_existing):
298302
"""Download QFieldCloud project files."""
299303

300304
log(f'Downloading project "{project_id}" files to {local_dir}…')
301305

302306
files = ctx.obj["client"].download_project(
303-
project_id, local_dir, filter_glob, throw_on_error, show_progress=True
307+
project_id, local_dir, filter_glob, throw_on_error, skip_existing, show_progress=True,
304308
)
305309

306310
if ctx.obj["format_json"]:

src/qfieldcloud_sdk/sdk.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ def download_project(
278278
local_dir: str,
279279
filter_glob: str = None,
280280
throw_on_error: bool = False,
281+
skip_existing: bool = False,
281282
show_progress: bool = False,
282283
) -> List[Dict]:
283284
"""Download the specified project files into the destination dir.
@@ -297,6 +298,7 @@ def download_project(
297298
local_dir,
298299
filter_glob,
299300
throw_on_error,
301+
skip_existing,
300302
show_progress,
301303
)
302304

@@ -477,6 +479,7 @@ def download_files(
477479
local_dir: str,
478480
filter_glob: str = None,
479481
throw_on_error: bool = False,
482+
skip_existing: bool = False,
480483
show_progress: bool = False,
481484
) -> List[Dict]:
482485
"""Download project files.
@@ -489,6 +492,7 @@ def download_files(
489492
filter_glob (str, optional): Download only files matching the glob pattern. If None download all. Defaults to None.
490493
throw_on_error (bool, optional): Throw if download error occurres. Defaults to False.
491494
show_progress (bool, optional): Show progress bar in the console. Defaults to False.
495+
skip_existing (bool, optional): Skip files if they already exist locally with same sha256 hash. Defaults to False.
492496
493497
Raises:
494498
QFieldCloudException: if throw_on_error is True, throw an error if a download request fails.
@@ -504,6 +508,19 @@ def download_files(
504508
for file in files:
505509
if fnmatch.fnmatch(file["name"], filter_glob):
506510
file["status"] = FileTransferStatus.PENDING
511+
512+
local_filename = Path(f'{local_dir}/{file["name"]}')
513+
if skip_existing and local_filename.exists():
514+
515+
sha256_hash = hashlib.sha256()
516+
with open(local_filename, 'rb') as f:
517+
for byte_block in iter(lambda: f.read(4096),b""):
518+
sha256_hash.update(byte_block)
519+
520+
if file['sha256'] == sha256_hash.hexdigest():
521+
logging.info(f'Skip file "{file["name"]}" because it already exists locally')
522+
continue
523+
507524
files_to_download.append(file)
508525

509526
for file in files_to_download:

0 commit comments

Comments
 (0)