Skip to content

Commit fc5ce35

Browse files
committed
Implement suricactus' suggestions
1 parent dc93067 commit fc5ce35

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

src/qfieldcloud_sdk/sdk.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -515,16 +515,18 @@ def download_files(
515515

516516
for file in files_to_download:
517517
local_filename = Path(f'{local_dir}/{file["name"]}')
518+
md5sum = None
519+
if not force_download:
520+
md5sum = file.get("md5sum", None)
518521

519522
try:
520523
self.download_file(
521524
project_id,
522525
download_type,
523526
local_filename,
524527
file["name"],
525-
file.get("md5sum", None),
526528
show_progress,
527-
force_download,
529+
md5sum,
528530
)
529531
file["status"] = FileTransferStatus.SUCCESS
530532
except QfcRequestException as err:
@@ -550,9 +552,8 @@ def download_file(
550552
download_type: FileTransferType,
551553
local_filename: Path,
552554
remote_filename: Path,
553-
remote_md5sum: str,
554555
show_progress: bool,
555-
force_download: bool = False,
556+
remote_md5sum: str = None,
556557
) -> requests.Response:
557558
"""Download a single project file.
558559
@@ -562,7 +563,7 @@ def download_file(
562563
local_filename (Path): Local filename
563564
remote_filename (Path): Remote filename
564565
show_progress (bool): Show progressbar in the console
565-
force_download (bool, optional): Download file even if it already exists locally. Defaults to False.
566+
remote_md5sum (str, optional): The md5sum of the remote file. If is None, the download of the file happens even if it already exists locally. Defaults to None.
566567
567568
Raises:
568569
NotImplementedError: Raised if unknown `download_type` is passed
@@ -571,21 +572,15 @@ def download_file(
571572
requests.Response: the response object
572573
"""
573574

574-
if (not force_download) and local_filename.exists() and remote_md5sum:
575-
with open(local_filename, "rb") as f:
576-
file_hash = hashlib.md5()
577-
chunk = f.read(8192)
578-
while chunk:
579-
file_hash.update(chunk)
580-
chunk = f.read(8192)
581-
if file_hash.hexdigest() == remote_md5sum:
575+
if remote_md5sum and local_filename.exists():
576+
if self._get_md5sum(str(local_filename)) == remote_md5sum:
582577
if show_progress:
583578
print(
584579
f"{remote_filename}: Already present locally. Download skipped."
585580
)
586581
else:
587582
logging.info(
588-
f'Skipping downloading file "{remote_filename}" because it is already present locally'
583+
f'Skipping download of "{remote_filename}" because it is already present locally'
589584
)
590585
return
591586

0 commit comments

Comments
 (0)