@@ -412,7 +412,6 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
412
412
verbose : bool
413
413
Whether to show download progress. Defaults to True.
414
414
"""
415
-
416
415
if head_safe :
417
416
response = self ._session .request ("HEAD" , url ,
418
417
timeout = timeout , stream = True ,
@@ -426,23 +425,29 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
426
425
if 'content-length' in response .headers :
427
426
length = int (response .headers ['content-length' ])
428
427
if length == 0 :
429
- log .warn ('URL {0} has length=0' .format (url ))
428
+ log .warning ('URL {0} has length=0' .format (url ))
430
429
else :
431
430
length = None
432
431
433
432
if ((os .path .exists (local_filepath )
434
433
and ('Accept-Ranges' in response .headers )
434
+ and length is not None
435
435
and continuation )):
436
436
open_mode = 'ab'
437
437
438
438
existing_file_length = os .stat (local_filepath ).st_size
439
- if length is not None and existing_file_length >= length :
440
- # all done!
441
- log .info ("Found cached file {0} with expected size {1}."
442
- .format (local_filepath , existing_file_length ))
443
- return
444
- elif existing_file_length == 0 :
439
+ if existing_file_length == 0 :
440
+ log .info (f"Found existing { local_filepath } file with length 0. Overwriting." )
445
441
open_mode = 'wb'
442
+ if head_safe :
443
+ response = self ._session .request (method , url ,
444
+ timeout = timeout , stream = True ,
445
+ auth = auth , ** kwargs )
446
+ response .raise_for_status ()
447
+ elif existing_file_length >= length :
448
+ # all done!
449
+ log .info (f"Found cached file { local_filepath } with size { existing_file_length } = { length } ." )
450
+ return local_filepath
446
451
else :
447
452
log .info ("Continuing download of file {0}, with {1} bytes to "
448
453
"go ({2}%)" .format (local_filepath ,
@@ -454,6 +459,7 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
454
459
end = "{0}" .format (length - 1 ) if length is not None else ""
455
460
self ._session .headers ['Range' ] = "bytes={0}-{1}" .format (existing_file_length ,
456
461
end )
462
+ log .debug (f"Continuing with range={ self ._session .headers ['Range' ]} " )
457
463
458
464
response = self ._session .request (method , url ,
459
465
timeout = timeout , stream = True ,
@@ -466,17 +472,24 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
466
472
statinfo = os .stat (local_filepath )
467
473
if statinfo .st_size != length :
468
474
log .warning (f"Found cached file { local_filepath } with size { statinfo .st_size } "
469
- f"that is different from expected size { length } " )
475
+ f"that is different from expected size { length } . " )
476
+ if continuation :
477
+ log .warning (
478
+ "Continuation was requested but is not possible because "
479
+ "'Accepts-Ranges' is not in the response headers." )
470
480
open_mode = 'wb'
481
+ response = self ._session .request (method , url ,
482
+ timeout = timeout , stream = True ,
483
+ auth = auth , ** kwargs )
484
+ response .raise_for_status ()
471
485
else :
472
- log .info ("Found cached file {0} with expected size {1}."
473
- .format (local_filepath , statinfo .st_size ))
486
+ log .info (f"Found cached file { local_filepath } with expected size { statinfo .st_size } ." )
474
487
response .close ()
475
- return
488
+ return local_filepath
476
489
else :
477
- log . info ( "Found cached file {0}." . format ( local_filepath ))
478
- response . close ()
479
- return
490
+ # This case doesn't appear reachable under normal circumstances
491
+ # It is not covered by tests, and probably indicates a badly-behaved server
492
+ raise ValueError ( f"Found cached file { local_filepath } . Could not verify length." )
480
493
else :
481
494
open_mode = 'wb'
482
495
if head_safe :
@@ -488,7 +501,7 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
488
501
blocksize = astropy .utils .data .conf .download_block_size
489
502
490
503
log .debug (f"Downloading URL { url } to { local_filepath } with size { length } "
491
- f"by blocks of { blocksize } " )
504
+ f"by blocks of { blocksize } with open_mode= { open_mode } " )
492
505
493
506
bytes_read = 0
494
507
@@ -514,7 +527,7 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
514
527
f .write (response .content )
515
528
516
529
response .close ()
517
- return response
530
+ return local_filepath
518
531
519
532
520
533
@deprecated (since = "v0.4.7" , message = ("The suspend_cache function is deprecated,"
0 commit comments