summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCalum Lind <calumlind+deluge@gmail.com>2011-07-06 21:35:53 +0100
committerCalum Lind <calumlind+deluge@gmail.com>2011-07-07 00:11:00 +0100
commit99358dcbb03f76e8acaee11b04d4c89bb7c9504f (patch)
treebdbdb882b51a14910ae25e5e561ef4733328155c
parent16cc8f6eea9745a8647134b0db35d4732c68d3af (diff)
downloaddeluge-99358dcbb03f76e8acaee11b04d4c89bb7c9504f.tar.gz
deluge-99358dcbb03f76e8acaee11b04d4c89bb7c9504f.tar.bz2
deluge-99358dcbb03f76e8acaee11b04d4c89bb7c9504f.zip
Fix httpdownloader error with existing filename
-rw-r--r--deluge/httpdownloader.py30
1 files changed, 16 insertions, 14 deletions
diff --git a/deluge/httpdownloader.py b/deluge/httpdownloader.py
index 2af5e59cf..3f3467a72 100644
--- a/deluge/httpdownloader.py
+++ b/deluge/httpdownloader.py
@@ -54,6 +54,8 @@ class HTTPDownloader(client.HTTPDownloader):
:type url: string
:param filename: the filename to save the file as
:type filename: string
+ :param force_filename: forces use of the supplied filename, regardless of header content
+ :type force_filename: bool
:param part_callback: a function to be called when a part of data
is received, it's signature should be: func(data, current_length, total_length)
:type part_callback: function
@@ -87,15 +89,20 @@ class HTTPDownloader(client.HTTPDownloader):
self.decoder = zlib.decompressobj(zlib.MAX_WBITS + 32)
if "content-disposition" in headers and not self.force_filename:
- try:
- new_file_name = str(headers["content-disposition"][0]).split(";")[1].split("=")[1]
- new_file_name = sanitise_filename(new_file_name)
- new_file_name = os.path.join(os.path.split(self.fileName)[0], new_file_name)
- except Exception, e:
- log.exception(e)
- else:
- self.fileName = new_file_name
- self.value = new_file_name
+ new_file_name = str(headers["content-disposition"][0]).split(";")[1].split("=")[1]
+ new_file_name = sanitise_filename(new_file_name)
+ new_file_name = os.path.join(os.path.split(self.fileName)[0], new_file_name)
+
+ count = 1
+ fileroot = os.path.splitext(new_file_name)[0]
+ fileext = os.path.splitext(new_file_name)[1]
+ while os.path.isfile(new_file_name):
+ # Increment filename if already exists
+ new_file_name = "%s-%s%s" % (fileroot, count, fileext)
+ count += 1
+
+ self.fileName = new_file_name
+ self.value = new_file_name
elif self.code in (http.MOVED_PERMANENTLY, http.FOUND, http.SEE_OTHER, http.TEMPORARY_REDIRECT):
location = headers["location"][0]
@@ -132,8 +139,6 @@ def sanitise_filename(filename):
:type filename: string
:returns: the sanitised filename
:rtype: string
-
- :raises IOError: when the filename exists
"""
# Remove any quotes
@@ -150,9 +155,6 @@ def sanitise_filename(filename):
# Dodgy server, log it
log.warning("Potentially malicious server: trying to write to file '%s'" % filename)
- if os.path.exists(filename):
- raise IOError, "File '%s' already exists!" % filename
-
return filename
def download_file(url, filename, callback=None, headers=None,