Skip to content

Commit 66cde2d

Browse files
authored
Merge pull request #84 from opengisch/QF-6453-handler-304
fix: do not overwrite with empty file if the server returns HTTP 304 when `If-None-Match` is sent
2 parents c25f003 + b6a806a commit 66cde2d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

qfieldcloud_sdk/sdk.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,8 +1168,11 @@ def download_file(
11681168
```
11691169
"""
11701170

1171+
headers: dict[str, str] = {}
11711172
if remote_etag and local_filename.exists():
1172-
if calc_etag(str(local_filename)) == remote_etag:
1173+
local_etag = calc_etag(str(local_filename))
1174+
1175+
if local_etag == remote_etag:
11731176
if show_progress:
11741177
print(
11751178
f"{remote_filename}: Already present locally. Download skipped."
@@ -1180,14 +1183,22 @@ def download_file(
11801183
)
11811184
return None
11821185

1186+
headers["If-None-Match"] = local_etag
1187+
11831188
if download_type == FileTransferType.PROJECT:
11841189
url = f"files/{project_id}/{remote_filename}"
11851190
elif download_type == FileTransferType.PACKAGE:
11861191
url = f"packages/{project_id}/latest/files/{remote_filename}"
11871192
else:
11881193
raise NotImplementedError()
11891194

1190-
resp = self._request("GET", url, stream=True)
1195+
resp = self._request("GET", url, stream=True, headers=headers)
1196+
1197+
# Since we are sending the `If-None-Match` header to check the `ETag` of the remote file,
1198+
# we shall expect HTTP 304 in case the local and remote `ETag` match.
1199+
# The early return will prevent overwriting the file with empty contents of the 304 response.
1200+
if resp.status_code == 304:
1201+
return None
11911202

11921203
if not local_filename.parent.exists():
11931204
local_filename.parent.mkdir(parents=True)

0 commit comments

Comments
 (0)