@@ -317,9 +317,6 @@ def _get_file_length_from_http_response(response: requests.Response) -> Optional
317
317
318
318
This function extracts the file size from the HTTP response headers, either from the
319
319
`Content-Range` or `Content-Length` header, if available (in that order).
320
- The HTTP response object containing the headers.
321
- `int` or `None`: The length of the file in bytes if the information is available,
322
- otherwise `None`.
323
320
324
321
Args:
325
322
response (`requests.Response`):
@@ -329,6 +326,15 @@ def _get_file_length_from_http_response(response: requests.Response) -> Optional
329
326
`int` or `None`: The length of the file in bytes, or None if not available.
330
327
"""
331
328
329
+ # If HTTP response contains compressed body (e.g. gzip), the `Content-Length` header will
330
+ # contain the length of the compressed body, not the uncompressed file size.
331
+ # And at the start of transmission there's no way to know the uncompressed file size for gzip,
332
+ # thus we return None in that case.
333
+ content_encoding = response .headers .get ("Content-Encoding" , "identity" ).lower ()
334
+ if content_encoding != "identity" :
335
+ # gzip/br/deflate/zstd etc
336
+ return None
337
+
332
338
content_range = response .headers .get ("Content-Range" )
333
339
if content_range is not None :
334
340
return int (content_range .rsplit ("/" )[- 1 ])
@@ -422,11 +428,7 @@ def http_get(
422
428
)
423
429
424
430
hf_raise_for_status (r )
425
- content_length = _get_file_length_from_http_response (r )
426
-
427
- # NOTE: 'total' is the total number of bytes to download, not the number of bytes in the file.
428
- # If the file is compressed, the number of bytes in the saved file will be higher than 'total'.
429
- total = resume_size + int (content_length ) if content_length is not None else None
431
+ total : Optional [int ] = _get_file_length_from_http_response (r )
430
432
431
433
if displayed_filename is None :
432
434
displayed_filename = url
0 commit comments