summaryrefslogtreecommitdiffstats
path: root/pyload/network/HTTPDownload.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyload/network/HTTPDownload.py')
-rw-r--r--pyload/network/HTTPDownload.py41
1 files changed, 20 insertions, 21 deletions
diff --git a/pyload/network/HTTPDownload.py b/pyload/network/HTTPDownload.py
index 13666195a..77f2ea657 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 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))
+ 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)