@@ -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