From 5aa1666cbe5482af7b79d0ee635423500b2fdc88 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 29 Jul 2011 16:17:43 +0200 Subject: Write file uploads to temp file while hashing. This avoids loading the file into memory, which is bad for huge files. --- fbin.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'fbin.py') diff --git a/fbin.py b/fbin.py index e6a11c1..b7450f1 100755 --- a/fbin.py +++ b/fbin.py @@ -159,15 +159,15 @@ class Application(object): filename = form.getvalue('filename') temp = tempfile.NamedTemporaryFile(mode = 'wb', prefix = 'fbin', delete = True) - temp.write(form.getvalue('file')) - temp.flush() + f = form['file'].file m = hashlib.md5() - with open(temp.name) as f: + s = f.read(128) + while len(s): + m.update(s) + temp.write(s) s = f.read(128) - while len(s): - m.update(s) - s = f.read(128) + temp.flush() file_hash = m.hexdigest() f = self.get_file_by_file_hash(file_hash) -- cgit v1.2.3