Fixed unique suffix placement for URLs with a file extension.

This commit is contained in:
Jakub Roztocil 2013-03-03 22:35:01 -03:00
parent 8e6c765be2
commit 7774eac3df
2 changed files with 10 additions and 9 deletions

View File

@ -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):
"""

View File

@ -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`.