From 3352d405c92e461cea40ec5bf1f356b843ae45e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 21 Mar 2016 17:36:43 +0100 Subject: [PATCH] Redownload if file size is not what it should be. --- blender_cloud/pillar.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/blender_cloud/pillar.py b/blender_cloud/pillar.py index 501c32f..e66ae80 100644 --- a/blender_cloud/pillar.py +++ b/blender_cloud/pillar.py @@ -144,11 +144,22 @@ async def download_to_file(url, filename, *, """Downloads a file via HTTP(S) directly to the filesystem.""" stored_headers = {} - if os.path.exists(header_store): + if os.path.exists(filename) and os.path.exists(header_store): log.debug('Loading cached headers %r', header_store) try: with open(header_store, 'r') as infile: stored_headers = requests.structures.CaseInsensitiveDict(json.load(infile)) + try: + expected_content_length = int(stored_headers['Content-Length']) + statinfo = os.stat(filename) + if expected_content_length != statinfo.st_size: + log.debug('File size should be %i but is %i; ignoring cache.', + expected_content_length, statinfo.st_size) + stored_headers = {} + except (KeyError, ValueError): + pass + except IOError: + log.debug('IO error ') except Exception as ex: log.warning('Unable to load headers from %r, ignoring cache: %s', header_store, str(ex)) @@ -212,6 +223,7 @@ async def download_to_file(url, filename, *, json.dump({ 'ETag': str(response.headers.get('etag', '')), 'Last-Modified': response.headers.get('Last-Modified'), + 'Content-Length': response.headers.get('Content-Length'), }, outfile, sort_keys=True)