changeset 3553:1052c105b16f

new request.send_file() call, making it possible to use server-specific optimizations
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 04 May 2008 18:47:03 +0200
parents 7d9b8040e3be
children c0becca3d0d9
files MoinMoin/action/AttachFile.py MoinMoin/request/__init__.py
diffstat 2 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/AttachFile.py	Sat May 03 22:46:14 2008 +0200
+++ b/MoinMoin/action/AttachFile.py	Sun May 04 18:47:03 2008 +0200
@@ -779,8 +779,6 @@
 
 
 def _do_get(pagename, request):
-    import shutil
-
     _ = request.getText
 
     pagename, filename, fpath = _access_file(pagename, request)
@@ -815,7 +813,7 @@
         ])
 
         # send data
-        shutil.copyfileobj(open(fpath, 'rb'), request, 8192)
+        request.send_file(open(fpath, 'rb'))
 
 
 def _do_install(pagename, request):
--- a/MoinMoin/request/__init__.py	Sat May 03 22:46:14 2008 +0200
+++ b/MoinMoin/request/__init__.py	Sun May 04 18:47:03 2008 +0200
@@ -879,6 +879,21 @@
         msg = 'Timing %5d %-6s %4s %-10s %s\n' % (pid, total, indicator, action, self.url)
         self.log(msg)
 
+    def send_file(self, fileobj, bufsize=8192, do_flush=False):
+        """ Send a file to the output stream.
+        
+        @param fileobj: a file-like object (supporting read, close)
+        @param bufsize: size of chunks to read/write
+        @param do_flush: call flush after writing?
+        """
+        while True:
+            buf = fileobj.read(bufsize)
+            if not buf:
+                break
+            self.write(buf)
+            if do_flush:
+                self.flush()
+
     def write(self, *data):
         """ Write to output stream. """
         raise NotImplementedError
@@ -986,7 +1001,7 @@
 
     def flush(self):
         """ Flush output stream. """
-        raise NotImplementedError
+        pass
 
     def check_spider(self):
         """ check if the user agent for current request is a spider/bot """