diff options
Diffstat (limited to 'pyload/network/HTTPDownload.py')
-rw-r--r-- | pyload/network/HTTPDownload.py | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/pyload/network/HTTPDownload.py b/pyload/network/HTTPDownload.py index 13666195a..8bf822a15 100644 --- a/pyload/network/HTTPDownload.py +++ b/pyload/network/HTTPDownload.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # @author: RaNaN +from __future__ import with_statement + from os import remove, fsync from os.path import dirname from time import sleep, time @@ -81,27 +83,24 @@ class HTTPDownload(object): init = fs_encode(self.info.getChunkName(0)) #: initial chunk name if self.info.getCount() > 1: - fo = open(init, "rb+") #: first chunkfile - for i in range(1, self.info.getCount()): - #input file - fo.seek( - self.info.getChunkRange(i - 1)[1] + 1) #: seek to beginning of chunk, to get rid of overlapping chunks - fname = fs_encode("%s.chunk%d" % (self.filename, i)) - fi = open(fname, "rb") - buf = 32 * 1024 - while True: #: copy in chunks, consumes less memory - data = fi.read(buf) - if not data: - break - fo.write(data) - fi.close() - if fo.tell() < self.info.getChunkRange(i)[1]: - fo.close() - remove(init) - self.info.remove() #: there are probably invalid chunks - raise Exception("Downloaded content was smaller than expected. Try to reduce download connections.") - remove(fname) #: remove chunk - fo.close() + with open(init, "rb+") as fo: #: first chunkfile + for i in xrange(1, self.info.getCount()): + #input file + fo.seek( + self.info.getChunkRange(i - 1)[1] + 1) #: seek to beginning of chunk, to get rid of overlapping chunks + fname = fs_encode("%s.chunk%d" % (self.filename, i)) + with open(fname, "rb") as fi: + buf = 32 * 1024 + while True: #: copy in chunks, consumes less memory + data = fi.read(buf) + if not data: + break + fo.write(data) + if fo.tell() < self.info.getChunkRange(i)[1]: + remove(init) + self.info.remove() #: there are probably invalid chunks + raise Exception("Downloaded content was smaller than expected. Try to reduce download connections.") + remove(fname) #: remove chunk if self.nameDisposition and self.disposition: self.filename = fs_join(dirname(self.filename), self.nameDisposition) @@ -174,7 +173,7 @@ class HTTPDownload(object): init.setRange(self.info.getChunkRange(0)) - for i in range(1, chunks): + for i in xrange(1, chunks): c = HTTPChunk(i, self, self.info.getChunkRange(i), resume) handle = c.getHandle() |