Core: Ensure cached files aren't saved if incomplete

This commit is contained in:
Mike Auty
2025-02-23 20:50:35 +00:00
parent c269086402
commit 2ff83c4434
+24 -27
View File
@@ -178,42 +178,39 @@ class ResourceAccessor:
+ ".cache",
)
try:
content_length = int(fp.info().get("Content-Length", -1))
except (AttributeError, ValueError):
# If our fp doesn't have an info member, carry on gracefully
content_length = -1
if not os.path.exists(temp_filename):
vollog.debug(f"Caching file at: {temp_filename}")
cache_file_size = -1
try:
content_length = fp.info().get("Content-Length", -1)
except AttributeError:
# If our fp doesn't have an info member, carry on gracefully
content_length = -1
with open(temp_filename, "wb") as cache_file:
count = 0
block = fp.read(block_size)
while block:
count += len(block)
if self._progress_callback:
self._progress_callback(
count * 100 / max(count, int(content_length)),
f"Reading file {url}",
)
cache_file.write(block)
with open(temp_filename, "wb") as cache_file:
count = 0
block = fp.read(block_size)
while block:
count += len(block)
if self._progress_callback:
self._progress_callback(
count * 100 / max(count, int(content_length)),
f"Reading file {url}",
)
cache_file.write(block)
block = fp.read(block_size)
cache_file.seek(0, os.SEEK_END)
cache_file_size = cache_file.tell()
finally:
if cache_file_size < content_length:
os.remove(temp_filename)
raise ValueError("Cached file did not download completely")
else:
vollog.debug(
f"Trying to use already cached file at: {temp_filename}"
)
count = 0
fp.seek(0, os.SEEK_END)
expected_filesize = fp.tell()
stop = False
while count < constants.DOWNLAOD_TIMEOUT and not stop:
time.sleep(1)
if os.stat(temp_filename).st_size == expected_filesize:
stop = True
if not stop:
raise ValueError(
f"Cached file existed, but was not the correct filesize, even after {constants.DOWNLOAD_TIMEOUT} seconds"
)
# Re-open the cache with a different mode
# Since we don't want people thinking they're able to save to the cache file,