From 2d943ca3e2869ec7372b41c536755ce29519332e Mon Sep 17 00:00:00 2001 From: Reid Sunderland Date: Tue, 27 Jun 2023 15:23:43 -0500 Subject: [PATCH] Fix for cases where downloaded file size is different than expected. For #709 (#710) When a file was downloaded successfully, but the size was different than in the message, or the message didn't include a file size, still rename the file and patch content_type. --- sarracenia/flow/__init__.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/sarracenia/flow/__init__.py b/sarracenia/flow/__init__.py index 41a78eb42..f3acbdc76 100644 --- a/sarracenia/flow/__init__.py +++ b/sarracenia/flow/__init__.py @@ -1835,13 +1835,6 @@ def download(self, msg, options) -> bool: if not self.o.dry_run: if accelerated: self.proto[self.scheme].update_file(new_inflight_path) - if (new_inflight_path != new_file): - if os.path.isfile(new_file): - os.remove(new_file) - os.rename(new_inflight_path, new_file) - # older versions don't include the contentType, so patch it here. - if 'contentType' not in msg: - msg['contentType'] = magic.from_file(new_file,mime=True) elif len_written < 0: logger.error("failed to download %s" % new_file) return False @@ -1865,8 +1858,18 @@ def download(self, msg, options) -> bool: 'incomplete download only %d of expected %d bytes for %s' % (len_written, block_length, new_inflight_path)) return False - - msg['size'] = len_written + # when len_written is different than block_length + msg['size'] = len_written + + # if we haven't returned False by this point, assuming download was successful + if (new_inflight_path != new_file): + if os.path.isfile(new_file): + os.remove(new_file) + os.rename(new_inflight_path, new_file) + + # older versions don't include the contentType, so patch it here. + if 'contentType' not in msg: + msg['contentType'] = magic.from_file(new_file,mime=True) self.metrics['flow']['transferRxBytes'] += len_written self.metrics['flow']['transferRxFiles'] += 1