From 7774eac3df8db61102045cec17152c91251605de Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sun, 3 Mar 2013 22:35:01 -0300 Subject: [PATCH] Fixed unique suffix placement for URLs with a file extension. --- httpie/downloads.py | 17 +++++++++-------- httpie/humanize.py | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/httpie/downloads.py b/httpie/downloads.py index 8031769b..f73ff426 100644 --- a/httpie/downloads.py +++ b/httpie/downloads.py @@ -63,6 +63,7 @@ class Download(object): else: self.bytes_resumed_from = self.bytes_downloaded = bytes_have # Set ``Range`` header to resume the download + # TODO: detect Range support first? headers['Range'] = '%d-' % bytes_have def start(self, response): @@ -132,18 +133,18 @@ class Download(object): def _get_output_filename(self, url, content_type, suffix=None): + suffix = '' if not suffix else '-' + str(suffix) + fn = urlsplit(url).path.rstrip('/') fn = os.path.basename(fn) if fn else 'index' - if suffix: - fn += '-' + str(suffix) + if '.' in fn: + base, ext = os.path.splitext(fn) + else: + base = fn + ext = mimetypes.guess_extension(content_type.split(';')[0]) or '' - if '.' not in fn: - ext = mimetypes.guess_extension(content_type.split(';')[0]) - if ext: - fn += ext - - return fn + return base + suffix + ext def _on_progress(self, chunk): """ diff --git a/httpie/humanize.py b/httpie/humanize.py index 25f2b651..2818605a 100644 --- a/httpie/humanize.py +++ b/httpie/humanize.py @@ -7,7 +7,7 @@ URL: http://code.activestate.com/recipes/577081/ import doctest -def humanize_bytes(n, precision=1): +def humanize_bytes(n, precision=2): """Return a humanized string representation of a number of bytes. Assumes `from __future__ import division`.