changeset 1154:e61194bb558e

Pre-compute content_length in send_file (fixes #121)
author Sam Toyer <samATqxcvDOTnet>
date Mon, 12 Dec 2011 19:15:15 +1000
parents 9fe19d20b91b
children 0807f3505c13
files MoinMoin/util/send_file.py
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/util/send_file.py	Tue Dec 13 14:51:39 2011 +0100
+++ b/MoinMoin/util/send_file.py	Mon Dec 12 19:15:15 2011 +1000
@@ -113,6 +113,19 @@
         mimetype = 'application/octet-stream'
 
     headers = Headers()
+
+    # We must compute size the smart way rather than letting
+    # werkzeug turn our iterable into an in-memory sequence
+    # See `_ensure_sequence` in werkzeug/wrappers.py
+    if filename:
+        fsize = os.path.getsize(filename)
+    else:
+        # Seek 0 bytes (0) from the end of the file (2)
+        file.seek(0, 2)
+        fsize = file.tell()
+        file.seek(0, 0)
+    headers.add('Content-Length', fsize)
+
     if as_attachment:
         if attachment_filename is None:
             if not filename: