summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-07-29 16:17:43 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-07-29 16:17:43 +0200
commit5aa1666cbe5482af7b79d0ee635423500b2fdc88 (patch)
tree033127f788e84a4dd35e3f38dbe0ae90769938bf
parent8b6fd105f67898a516b5e21cad566950e5dac99d (diff)
Write file uploads to temp file while hashing.
This avoids loading the file into memory, which is bad for huge files.
-rwxr-xr-xfbin.py12
1 files changed, 6 insertions, 6 deletions
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)