Commit 205e1a73 by Guba Sándor

fix file-size handling

parent 04b3aede
...@@ -26,6 +26,10 @@ class AbortException(Exception): ...@@ -26,6 +26,10 @@ class AbortException(Exception):
pass pass
class FileTooBig(Exception):
pass
class Disk(object): class Disk(object):
''' Storage driver DISK object. ''' Storage driver DISK object.
...@@ -206,7 +210,9 @@ class Disk(object): ...@@ -206,7 +210,9 @@ class Disk(object):
# undocumented zlib feature http://stackoverflow.com/a/2424549 # undocumented zlib feature http://stackoverflow.com/a/2424549
elif ext == 'bz2': elif ext == 'bz2':
decompressor = BZ2Decompressor() decompressor = BZ2Decompressor()
clen = min(int(r.headers.get('content-length', maximum_size)), 1) clen = int(r.headers.get('content-length', maximum_size))
if clen > maximum_size:
raise FileTooBig()
percent = 0 percent = 0
try: try:
with open(disk_path, 'wb') as f: with open(disk_path, 'wb') as f:
...@@ -216,8 +222,7 @@ class Disk(object): ...@@ -216,8 +222,7 @@ class Disk(object):
f.write(chunk) f.write(chunk)
actsize = f.tell() actsize = f.tell()
if actsize > maximum_size: if actsize > maximum_size:
raise Exception("%s file is too big. Maximum size " raise FileTooBig()
"is %s" % url, maximum_size)
new_percent = min(100, round(actsize * 100.0 / clen)) new_percent = min(100, round(actsize * 100.0 / clen))
if new_percent > percent: if new_percent > percent:
percent = new_percent percent = new_percent
...@@ -239,6 +244,10 @@ class Disk(object): ...@@ -239,6 +244,10 @@ class Disk(object):
os.unlink(disk_path) os.unlink(disk_path)
logger.info("Download %s aborted %s removed.", logger.info("Download %s aborted %s removed.",
url, disk_path) url, disk_path)
except FileTooBig:
os.unlink(disk_path)
raise Exception("%s file is too big. Maximum size "
"is %s" % url, maximum_size)
except: except:
os.unlink(disk_path) os.unlink(disk_path)
logger.error("Download %s failed, %s removed.", logger.error("Download %s failed, %s removed.",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment